Coverage Report - org.mule.agent.ConfigScannerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigScannerAgent
0%
0/40
0%
0/4
1.8
ConfigScannerAgent$ScannerThread
0%
0/26
0%
0/10
1.8
 
 1  
 /*
 2  
  * $Id: ConfigScannerAgent.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.agent;
 12  
 
 13  
 import org.mule.AbstractAgent;
 14  
 import org.mule.MuleServer;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.config.ConfigurationBuilder;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.util.ClassUtils;
 19  
 import org.mule.util.FileUtils;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collections;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 /**
 31  
  * EXPERIMENTAL!!!
 32  
  *
 33  
  * This agent scans a defined directory for dropped configuration files. It
 34  
  * defaults to .mule/conf.
 35  
  *
 36  
  * Once configuration files are ready, they should be moved instead of deleted.
 37  
  * We need a strategy for this.
 38  
  *
 39  
  * This agent should also respond to wire transfers (tcp, multicast).
 40  
  */
 41  0
 public class ConfigScannerAgent extends AbstractAgent
 42  
 {
 43  
     public static final String AGENT_NAME = "Mule Config Scanner";
 44  
 
 45  
     /**
 46  
      * logger used by this class
 47  
      */
 48  0
     private static final Log logger = LogFactory.getLog(ConfigScannerAgent.class);
 49  
 
 50  0
     private String configDirName = null;
 51  
 
 52  0
     private File configDir = null;
 53  
 
 54  0
     private int sleepInterval = 5000;
 55  
 
 56  0
     private boolean doStop = false;
 57  
 
 58  0
     private ScannerThread scannerThread = null;
 59  
 
 60  
     public ConfigScannerAgent()
 61  
     {
 62  0
         super(AGENT_NAME);
 63  0
     }
 64  
 
 65  
     public String getConfigDirName()
 66  
     {
 67  0
         return configDirName;
 68  
     }
 69  
 
 70  
     public void setConfigDirName(String configDirName)
 71  
     {
 72  0
         this.configDirName = configDirName;
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Should be a 1 line description of the agent
 77  
      * 
 78  
      * @return
 79  
      */
 80  
     public String getDescription()
 81  
     {
 82  0
         return getName() + " scanning for files in " + configDirName;
 83  
     }
 84  
 
 85  
     public void start() throws MuleException
 86  
     {
 87  0
         scannerThread = new ScannerThread();
 88  0
         scannerThread.start();
 89  0
     }
 90  
 
 91  
     public void stop() throws MuleException
 92  
     {
 93  
         // do nothing
 94  0
     }
 95  
 
 96  
     public void dispose()
 97  
     {
 98  
         // do nothing
 99  0
     }
 100  
 
 101  
     public void registered()
 102  
     {
 103  
         // do nothing
 104  0
     }
 105  
 
 106  
     public void unregistered()
 107  
     {
 108  
         // do nothing
 109  0
     }
 110  
 
 111  
     public void initialise() throws InitialisationException
 112  
     {
 113  0
         if (configDirName == null)
 114  
         {
 115  0
             String workDir = muleContext.getConfiguration().getWorkingDirectory();
 116  0
             configDirName = workDir + "/conf";
 117  
         }
 118  
 
 119  
         try 
 120  
         {
 121  0
             configDir = FileUtils.openDirectory(configDirName);
 122  
         }
 123  0
         catch (IOException ioe)
 124  
         {
 125  0
             throw new InitialisationException(ioe, this);
 126  0
         }
 127  0
     }
 128  
 
 129  
     public List getDependentAgents()
 130  
     {
 131  0
         return Collections.EMPTY_LIST;
 132  
     }
 133  
 
 134  
     public String toString()
 135  
     {
 136  0
         return getDescription();
 137  
     }
 138  
 
 139  
     public void setDoStop(boolean doStop) 
 140  
     {
 141  0
         this.doStop = doStop;
 142  0
     }
 143  
 
 144  0
     class ScannerThread extends Thread
 145  
     {
 146  0
         int errorCount = 0;
 147  0
         int errorThreshold = 3;
 148  0
         List processedFiles = new ArrayList();
 149  
 
 150  
         public void run()
 151  
         {
 152  
             while (true)
 153  
             {
 154  0
                 if (doStop || errorCount >= errorThreshold)
 155  
                 {
 156  0
                     break;
 157  
                 }
 158  
 
 159  
                 try
 160  
                 {
 161  0
                     File[] configFiles = configDir.listFiles();
 162  0
                     for (int i = 0; i < configFiles.length; i++)
 163  
                     {
 164  0
                         File configFile = configFiles[i];
 165  0
                         String path = configFile.getCanonicalPath();
 166  
 
 167  0
                         if (processedFiles.contains(path))
 168  
                         {
 169  
                             // TODO: probably shouldn't delete here
 170  0
                             configFile.delete();
 171  0
                             if (configFile.exists())
 172  
                             {
 173  0
                                 processedFiles.remove(processedFiles.indexOf(path));
 174  
                             }
 175  
                         }
 176  
                         else
 177  
                         {
 178  0
                             logger.info(path);
 179  0
                             processConfigFile(path);
 180  
                             // TODO: probably shouldn't delete here
 181  0
                             configFile.delete();
 182  0
                             processedFiles.add(path);
 183  
                         }
 184  
                     }
 185  
                 } 
 186  0
                 catch (IOException ioe)
 187  
                 {
 188  0
                     logger.error("Unable to check directory: " + ioe.toString());
 189  0
                     errorCount++;
 190  0
                 }
 191  
 
 192  
                 try
 193  
                 {
 194  0
                     sleep(sleepInterval);
 195  
                 }
 196  0
                 catch (InterruptedException e)
 197  
                 {
 198  0
                 }
 199  
             }
 200  0
         }
 201  
     }
 202  
 
 203  
     private void processConfigFile(String configFile)
 204  
     {
 205  
         try
 206  
         {
 207  0
             Class cfgBuilderClass = ClassUtils.loadClass(
 208  
                 "org.mule.config.spring.SpringXmlConfigurationBuilder", MuleServer.class);
 209  0
             ConfigurationBuilder cfgBuilder = (ConfigurationBuilder) ClassUtils.instanciateClass(
 210  
                 cfgBuilderClass, new Object[]{configFile});
 211  
 
 212  0
             if (!cfgBuilder.isConfigured())
 213  
             {
 214  
                 // TODO Update after MULE-1988
 215  0
                 cfgBuilder.configure(muleContext);
 216  
             }
 217  
         }
 218  0
         catch (Exception e)
 219  
         {
 220  0
             logger.error(e);
 221  0
         }
 222  0
     }
 223  
 
 224  
 }
 225