View Javadoc

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