View Javadoc

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  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      private static final Log logger = LogFactory.getLog(ConfigScannerAgent.class);
48  
49      private String configDirName = null;
50  
51      private File configDir = null;
52  
53      private int sleepInterval = 5000;
54  
55      private boolean doStop = false;
56  
57      private ScannerThread scannerThread = null;
58  
59      public ConfigScannerAgent()
60      {
61          super(AGENT_NAME);
62      }
63  
64      public String getConfigDirName()
65      {
66          return configDirName;
67      }
68  
69      public void setConfigDirName(String configDirName)
70      {
71          this.configDirName = configDirName;
72      }
73  
74      /**
75       * Should be a 1 line description of the agent
76       */
77      @Override
78      public String getDescription()
79      {
80          return "Mule Config Scanner scanning for files in " + configDirName;
81      }
82  
83      public void start() throws MuleException
84      {
85          scannerThread = new ScannerThread();
86          scannerThread.start();
87      }
88  
89      public void stop() throws MuleException
90      {
91          // do nothing
92      }
93  
94      public void dispose()
95      {
96          // do nothing
97      }
98  
99  
100     public void initialise() throws InitialisationException
101     {
102         if (configDirName == null)
103         {
104             String workDir = muleContext.getConfiguration().getWorkingDirectory();
105             configDirName = workDir + "/conf";
106         }
107 
108         try 
109         {
110             configDir = FileUtils.openDirectory(configDirName);
111         }
112         catch (IOException ioe)
113         {
114             throw new InitialisationException(ioe, this);
115         }
116     }
117 
118     @Override
119     public String toString()
120     {
121         return getDescription();
122     }
123 
124     public void setDoStop(boolean doStop) 
125     {
126         this.doStop = doStop;
127     }
128 
129     class ScannerThread extends Thread
130     {
131         int errorCount = 0;
132         int errorThreshold = 3;
133         List<String> processedFiles = new ArrayList<String>();
134 
135         @Override
136         public void run()
137         {
138             while (true)
139             {
140                 if (doStop || errorCount >= errorThreshold)
141                 {
142                     break;
143                 }
144 
145                 try
146                 {
147                     File[] configFiles = configDir.listFiles();
148                     for (int i = 0; i < configFiles.length; i++)
149                     {
150                         File configFile = configFiles[i];
151                         String path = configFile.getCanonicalPath();
152 
153                         if (processedFiles.contains(path))
154                         {
155                             // TODO: probably shouldn't delete here
156                             configFile.delete();
157                             if (configFile.exists())
158                             {
159                                 processedFiles.remove(processedFiles.indexOf(path));
160                             }
161                         }
162                         else
163                         {
164                             logger.info(path);
165                             processConfigFile(path);
166                             // TODO: probably shouldn't delete here
167                             configFile.delete();
168                             processedFiles.add(path);
169                         }
170                     }
171                 } 
172                 catch (IOException ioe)
173                 {
174                     logger.error("Unable to check directory: " + ioe.toString());
175                     errorCount++;
176                 }
177 
178                 try
179                 {
180                     sleep(sleepInterval);
181                 }
182                 catch (InterruptedException e)
183                 {
184                 }
185             }
186         }
187     }
188 
189     private void processConfigFile(String configFile)
190     {
191         try
192         {
193             Class<?> cfgBuilderClass = ClassUtils.loadClass(
194                 "org.mule.config.spring.SpringXmlConfigurationBuilder", MuleServer.class);
195             ConfigurationBuilder cfgBuilder = (ConfigurationBuilder) ClassUtils.instanciateClass(
196                 cfgBuilderClass, configFile);
197 
198             if (!cfgBuilder.isConfigured())
199             {
200                 // TODO Update after MULE-1988
201                 cfgBuilder.configure(muleContext);
202             }
203         }
204         catch (Exception e)
205         {
206             logger.error(e);
207         }
208     }
209 
210 }
211