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