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.List; |
25 | |
|
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class ConfigScannerAgent extends AbstractAgent |
41 | |
{ |
42 | |
public static final String AGENT_NAME = "config-scanner"; |
43 | |
|
44 | |
|
45 | |
|
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 | |
|
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 | |
|
92 | 0 | } |
93 | |
|
94 | |
public void dispose() |
95 | |
{ |
96 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|