1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class ConfigScannerAgent extends AbstractAgent |
42 | |
{ |
43 | |
public static final String AGENT_NAME = "Mule Config Scanner"; |
44 | |
|
45 | |
|
46 | |
|
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 | |
|
77 | |
|
78 | |
|
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 | |
|
94 | 0 | } |
95 | |
|
96 | |
public void dispose() |
97 | |
{ |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
public void registered() |
102 | |
{ |
103 | |
|
104 | 0 | } |
105 | |
|
106 | |
public void unregistered() |
107 | |
{ |
108 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|