1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule; |
12 | |
|
13 | |
import org.mule.api.DefaultMuleException; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.config.ConfigurationBuilder; |
17 | |
import org.mule.api.config.ConfigurationException; |
18 | |
import org.mule.config.ExceptionHelper; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.config.i18n.Message; |
21 | |
import org.mule.context.DefaultMuleContextFactory; |
22 | |
import org.mule.util.ClassUtils; |
23 | |
import org.mule.util.IOUtils; |
24 | |
import org.mule.util.MuleUrlStreamHandlerFactory; |
25 | |
import org.mule.util.PropertiesUtils; |
26 | |
import org.mule.util.StringMessageUtils; |
27 | |
import org.mule.util.SystemUtils; |
28 | |
|
29 | |
import java.net.URL; |
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Collections; |
32 | |
import java.util.Date; |
33 | |
import java.util.List; |
34 | |
import java.util.Map; |
35 | |
import java.util.Properties; |
36 | |
|
37 | |
import org.apache.commons.logging.Log; |
38 | |
import org.apache.commons.logging.LogFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class MuleServer implements Runnable |
45 | |
{ |
46 | 2 | public static final String CLI_OPTIONS[][] = { |
47 | |
{"builder", "true", "Configuration Builder Type"}, |
48 | |
{"config", "true", "Configuration File"}, |
49 | |
{"idle", "false", "Whether to run in idle (unconfigured) mode"}, |
50 | |
{"main", "true", "Main Class"}, |
51 | |
{"mode", "true", "Run Mode"}, |
52 | |
{"props", "true", "Startup Properties"}, |
53 | |
{"debug", "false", "Configure Mule for JPDA remote debugging."} |
54 | |
}; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
protected static final String CLASSNAME_DEFAULT_CONFIG_BUILDER = "org.mule.config.builders.AutoConfigurationBuilder"; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
protected static final String CLASSNAME_DEFAULT_IDLE_CONFIG_BUILDER = "org.mule.config.builders.MuleIdleConfigurationBuilder"; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
protected static final String CLASSNAME_SPRING_CONFIG_BUILDER = "org.mule.config.spring.SpringXmlConfigurationBuilder"; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 2 | private static final Log logger = LogFactory.getLog(MuleServer.class); |
78 | |
|
79 | |
public static final String DEFAULT_CONFIGURATION = "mule-config.xml"; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 0 | private String configurationResources = null; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 2 | private static String configBuilderClassName = null; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 2 | private static String startupPropertiesFile = null; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private static MuleShutdownHook muleShutdownHook; |
102 | |
|
103 | 0 | protected Map options = Collections.EMPTY_MAP; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 2 | protected static MuleContext muleContext = null; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public static void main(String[] args) throws Exception |
119 | |
{ |
120 | 0 | MuleServer server = new MuleServer(args); |
121 | 0 | server.start(false, true); |
122 | |
|
123 | 0 | } |
124 | |
|
125 | |
public MuleServer() |
126 | 0 | { |
127 | 0 | init(new String[]{}); |
128 | 0 | } |
129 | |
|
130 | |
public MuleServer(String configResources) |
131 | 0 | { |
132 | |
|
133 | 0 | init(new String[]{"-config", configResources}); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public MuleServer(String[] args) throws IllegalArgumentException |
140 | 0 | { |
141 | 0 | init(args); |
142 | 0 | } |
143 | |
|
144 | |
protected void init(String[] args) throws IllegalArgumentException |
145 | |
{ |
146 | |
Map options; |
147 | |
|
148 | |
try |
149 | |
{ |
150 | 0 | muleShutdownHook = new MuleShutdownHook(logger); |
151 | 0 | registerShutdownHook(muleShutdownHook); |
152 | 0 | options = SystemUtils.getCommandLineOptions(args, CLI_OPTIONS); |
153 | |
} |
154 | 0 | catch (DefaultMuleException me) |
155 | |
{ |
156 | 0 | throw new IllegalArgumentException(me.toString()); |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | 0 | MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory(); |
162 | |
|
163 | 0 | String config = (String) options.get("config"); |
164 | |
|
165 | 0 | if (config == null && !options.containsKey("idle")) |
166 | |
{ |
167 | 0 | logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION); |
168 | |
|
169 | 0 | URL configUrl = IOUtils.getResourceAsUrl(DEFAULT_CONFIGURATION, MuleServer.class, true, false); |
170 | 0 | if (configUrl != null) |
171 | |
{ |
172 | 0 | config = configUrl.toExternalForm(); |
173 | |
} |
174 | |
else |
175 | |
{ |
176 | 0 | System.out.println(CoreMessages.configNotFoundUsage()); |
177 | 0 | System.exit(-1); |
178 | |
} |
179 | |
} |
180 | |
|
181 | 0 | if (config != null) |
182 | |
{ |
183 | 0 | setConfigurationResources(config); |
184 | |
} |
185 | |
|
186 | |
|
187 | 0 | String cfgBuilderClassName = (String) options.get("builder"); |
188 | |
|
189 | 0 | if (options.containsKey("idle")) |
190 | |
{ |
191 | 0 | setConfigurationResources("IDLE"); |
192 | 0 | cfgBuilderClassName = CLASSNAME_DEFAULT_IDLE_CONFIG_BUILDER; |
193 | |
} |
194 | |
|
195 | |
|
196 | 0 | if (cfgBuilderClassName != null) |
197 | |
{ |
198 | |
try |
199 | |
{ |
200 | |
|
201 | 0 | if (cfgBuilderClassName.equalsIgnoreCase("spring")) |
202 | |
{ |
203 | 0 | cfgBuilderClassName = CLASSNAME_SPRING_CONFIG_BUILDER; |
204 | |
} |
205 | 0 | setConfigBuilderClassName(cfgBuilderClassName); |
206 | |
} |
207 | 0 | catch (Exception e) |
208 | |
{ |
209 | 0 | logger.fatal(e); |
210 | 0 | final Message message = CoreMessages.failedToLoad("Builder: " + cfgBuilderClassName); |
211 | 0 | System.err.println(StringMessageUtils.getBoilerPlate("FATAL: " + message.toString())); |
212 | 0 | System.exit(1); |
213 | 0 | } |
214 | |
} |
215 | |
|
216 | |
|
217 | 0 | String propertiesFile = (String) options.get("props"); |
218 | 0 | if (propertiesFile != null) |
219 | |
{ |
220 | 0 | setStartupPropertiesFile(propertiesFile); |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public void start(boolean ownThread, boolean registerShutdownHook) |
231 | |
{ |
232 | 0 | if (registerShutdownHook) |
233 | |
{ |
234 | 0 | registerShutdownHook(muleShutdownHook); |
235 | |
} |
236 | 0 | if (ownThread) |
237 | |
{ |
238 | 0 | Thread serverThread = new Thread(this, "MuleServer"); |
239 | 0 | serverThread.setDaemon(true); |
240 | 0 | serverThread.start(); |
241 | 0 | } |
242 | |
else |
243 | |
{ |
244 | 0 | run(); |
245 | |
} |
246 | 0 | } |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public void run() |
253 | |
{ |
254 | |
try |
255 | |
{ |
256 | 0 | logger.info("Mule Server initializing..."); |
257 | 0 | initialize(); |
258 | 0 | logger.info("Mule Server starting..."); |
259 | 0 | muleContext.start(); |
260 | |
} |
261 | 0 | catch (Throwable e) |
262 | |
{ |
263 | 0 | shutdown(e); |
264 | 0 | } |
265 | 0 | } |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public static void setConfigBuilderClassName(String builderClassName) throws ClassNotFoundException |
277 | |
{ |
278 | 0 | if (builderClassName != null) |
279 | |
{ |
280 | 0 | Class cls = ClassUtils.loadClass(builderClassName, MuleServer.class); |
281 | 0 | if (ConfigurationBuilder.class.isAssignableFrom(cls)) |
282 | |
{ |
283 | 0 | MuleServer.configBuilderClassName = builderClassName; |
284 | |
} |
285 | |
else |
286 | |
{ |
287 | 0 | throw new IllegalArgumentException("Not a usable ConfigurationBuilder class: " |
288 | |
+ builderClassName); |
289 | |
} |
290 | 0 | } |
291 | |
else |
292 | |
{ |
293 | 0 | MuleServer.configBuilderClassName = null; |
294 | |
} |
295 | 0 | } |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public static String getConfigBuilderClassName() |
304 | |
{ |
305 | 0 | if (configBuilderClassName != null) |
306 | |
{ |
307 | 0 | return configBuilderClassName; |
308 | |
} |
309 | |
else |
310 | |
{ |
311 | 0 | return CLASSNAME_DEFAULT_CONFIG_BUILDER; |
312 | |
} |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public void initialize() throws Exception |
321 | |
{ |
322 | 0 | Runtime.getRuntime().addShutdownHook(new ShutdownThread()); |
323 | |
|
324 | 0 | if (configurationResources == null) |
325 | |
{ |
326 | 0 | logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION); |
327 | 0 | configurationResources = DEFAULT_CONFIGURATION; |
328 | |
} |
329 | |
|
330 | |
ConfigurationBuilder cfgBuilder; |
331 | |
|
332 | |
try |
333 | |
{ |
334 | |
|
335 | 0 | cfgBuilder = (ConfigurationBuilder) ClassUtils.instanciateClass(getConfigBuilderClassName(), |
336 | |
new Object[]{configurationResources}, MuleServer.class); |
337 | |
} |
338 | 0 | catch (Exception e) |
339 | |
{ |
340 | 0 | throw new ConfigurationException(CoreMessages.failedToLoad(getConfigBuilderClassName()), e); |
341 | 0 | } |
342 | |
|
343 | 0 | if (!cfgBuilder.isConfigured()) |
344 | |
{ |
345 | 0 | Properties startupProperties= null; |
346 | 0 | if (getStartupPropertiesFile() != null) |
347 | |
{ |
348 | 0 | startupProperties = PropertiesUtils.loadProperties(getStartupPropertiesFile(), getClass()); |
349 | |
} |
350 | 0 | DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); |
351 | 0 | muleContext = muleContextFactory.createMuleContext(cfgBuilder, startupProperties); |
352 | |
} |
353 | 0 | } |
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
public void shutdown(Throwable e) |
361 | |
{ |
362 | 0 | Message msg = CoreMessages.fatalErrorWhileRunning(); |
363 | 0 | MuleException muleException = ExceptionHelper.getRootMuleException(e); |
364 | 0 | if (muleException != null) |
365 | |
{ |
366 | 0 | logger.fatal(muleException.getDetailedMessage()); |
367 | |
} |
368 | |
else |
369 | |
{ |
370 | 0 | logger.fatal(msg.toString() + " " + e.getMessage(), e); |
371 | |
} |
372 | 0 | List msgs = new ArrayList(); |
373 | 0 | msgs.add(msg.getMessage()); |
374 | 0 | Throwable root = ExceptionHelper.getRootException(e); |
375 | 0 | msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")"); |
376 | 0 | msgs.add(" "); |
377 | 0 | msgs.add(CoreMessages.fatalErrorInShutdown()); |
378 | 0 | if (muleContext != null) |
379 | |
{ |
380 | 0 | msgs.add(CoreMessages.serverStartedAt(muleContext.getStartDate())); |
381 | |
} |
382 | 0 | msgs.add(CoreMessages.serverShutdownAt(new Date())); |
383 | |
|
384 | 0 | String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80); |
385 | 0 | logger.fatal(shutdownMessage); |
386 | |
|
387 | |
|
388 | 0 | if (muleContext != null) |
389 | |
{ |
390 | 0 | muleContext.dispose(); |
391 | |
} |
392 | 0 | System.exit(0); |
393 | 0 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
public void shutdown() |
399 | |
{ |
400 | 0 | logger.info("Mule server shutting dow due to normal shutdown request"); |
401 | 0 | List msgs = new ArrayList(); |
402 | 0 | msgs.add(CoreMessages.normalShutdown()); |
403 | 0 | msgs.add(CoreMessages.serverStartedAt(muleContext.getStartDate()).getMessage()); |
404 | 0 | msgs.add(CoreMessages.serverShutdownAt(new Date()).getMessage()); |
405 | 0 | String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80); |
406 | 0 | logger.info(shutdownMessage); |
407 | |
|
408 | |
|
409 | 0 | muleContext.dispose(); |
410 | 0 | System.exit(0); |
411 | 0 | } |
412 | |
|
413 | |
public Log getLogger() |
414 | |
{ |
415 | 0 | return logger; |
416 | |
} |
417 | |
|
418 | |
public void registerShutdownHook(MuleShutdownHook muleShutdownHook) |
419 | |
{ |
420 | 0 | Runtime.getRuntime().removeShutdownHook(muleShutdownHook); |
421 | 0 | Runtime.getRuntime().addShutdownHook(muleShutdownHook); |
422 | 0 | } |
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
public String getConfigurationResources() |
434 | |
{ |
435 | 0 | return configurationResources; |
436 | |
} |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
public void setConfigurationResources(String configurationResources) |
444 | |
{ |
445 | 0 | this.configurationResources = configurationResources; |
446 | 0 | } |
447 | |
|
448 | |
public static String getStartupPropertiesFile() |
449 | |
{ |
450 | 0 | return startupPropertiesFile; |
451 | |
} |
452 | |
|
453 | |
public static void setStartupPropertiesFile(String startupPropertiesFile) |
454 | |
{ |
455 | 0 | MuleServer.startupPropertiesFile = startupPropertiesFile; |
456 | 0 | } |
457 | |
|
458 | |
public static MuleContext getMuleContext() |
459 | |
{ |
460 | 51970 | return muleContext; |
461 | |
} |
462 | |
|
463 | |
public static void setMuleContext(MuleContext muleContext) |
464 | |
{ |
465 | 3754 | MuleServer.muleContext = muleContext; |
466 | 3754 | } |
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | 0 | private static class ShutdownThread extends Thread |
474 | |
{ |
475 | |
public void run() |
476 | |
{ |
477 | 0 | if (muleContext !=null && !muleContext.isDisposed() && !muleContext.isDisposing()) |
478 | |
{ |
479 | 0 | muleContext.dispose(); |
480 | |
} |
481 | 0 | } |
482 | |
} |
483 | |
} |