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