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