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