KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > MuleServer


1 /*
2  * $Id: MuleServer.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.mule.config.ConfigurationBuilder;
21 import org.mule.config.ExceptionHelper;
22 import org.mule.config.i18n.Message;
23 import org.mule.config.i18n.Messages;
24 import org.mule.umo.UMOException;
25 import org.mule.util.ClassUtils;
26 import org.mule.util.IOUtils;
27 import org.mule.util.StringMessageUtils;
28 import org.mule.util.SystemUtils;
29
30 /**
31  * <code>MuleServer</code> is a simple application that represents a local Mule
32  * Server daemon. It is initialised with a mule-config.xml file.
33  *
34  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
35  */

36 public class MuleServer implements Runnable JavaDoc
37 {
38     /**
39      * Don't use a class object so the core doesn't depend on mule-module-builders.
40      */

41     protected static final String JavaDoc CLASSNAME_DEFAULT_CONFIG_BUILDER = "org.mule.config.builders.MuleXmlConfigurationBuilder";
42
43     /**
44      * Required to support the '-config spring' shortcut. Don't use a class object so
45      * the core doesn't depend on mule-module-spring. TODO this may not be a problem
46      * for Mule 2.x
47      */

48     protected static final String JavaDoc CLASSNAME_SPRING_CONFIG_BUILDER = "org.mule.extras.spring.config.SpringConfigurationBuilder";
49
50     /**
51      * logger used by this class
52      */

53     private static Log logger = LogFactory.getLog(MuleServer.class);
54
55     public static final String JavaDoc DEFAULT_CONFIGURATION = "mule-config.xml";
56
57     /**
58      * the message to display when the server shuts down
59      */

60     private static String JavaDoc shutdownMessage = null;
61
62     /**
63      * one or more configuration urls or filenames separated by commas
64      */

65     private String JavaDoc configurationResources = null;
66
67     /**
68      * A FQN of the #configBuilder class, required in case MuleServer is
69      * reinitialised.
70      */

71     private static String JavaDoc configBuilderClassName = null;
72
73     /**
74      * A properties file to be read at startup. This can be useful for setting
75      * properties which depend on the run-time environment (dev, test, production).
76      */

77     private static String JavaDoc startupPropertiesFile = null;
78
79     /**
80      * Application entry point.
81      *
82      * @param args command-line args
83      */

84     public static void main(String JavaDoc[] args)
85     {
86         MuleServer server = new MuleServer();
87
88         String JavaDoc config = SystemUtils.getCommandLineOption("-config", args);
89         // Try default if no config file was given.
90
if (config == null)
91         {
92             logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION);
93             // try to load the config as a file as well
94
URL JavaDoc configUrl = IOUtils.getResourceAsUrl(DEFAULT_CONFIGURATION, MuleServer.class, true);
95             if (configUrl != null)
96             {
97                 config = configUrl.toExternalForm();
98             }
99         }
100         if (config != null)
101         {
102             server.setConfigurationResources(config);
103         }
104         else
105         {
106             Message message = new Message(Messages.CONFIG_NOT_FOUND_USAGE);
107             System.err.println(message.toString());
108             System.exit(1);
109         }
110
111         // Configuration builder
112
String JavaDoc cfgBuilderClassName = SystemUtils.getCommandLineOption("-builder", args);
113         if (cfgBuilderClassName != null)
114         {
115             try
116             {
117                 // Provide a shortcut for Spring: "-builder spring"
118
if (cfgBuilderClassName.equalsIgnoreCase("spring"))
119                 {
120                     cfgBuilderClassName = CLASSNAME_SPRING_CONFIG_BUILDER;
121                 }
122                 setConfigBuilderClassName(cfgBuilderClassName);
123             }
124             catch (Exception JavaDoc e)
125             {
126                 logger.fatal(e);
127                 final Message message = new Message(Messages.FAILED_LOAD_X, "Builder: " + cfgBuilderClassName);
128                 System.err.println(StringMessageUtils.getBoilerPlate("FATAL: " + message.toString()));
129                 System.exit(1);
130             }
131         }
132
133         // Startup properties
134
String JavaDoc propertiesFile = SystemUtils.getCommandLineOption("-props", args);
135         if (propertiesFile != null)
136         {
137             setStartupPropertiesFile(propertiesFile);
138         }
139
140         server.start(false);
141     }
142
143     public MuleServer()
144     {
145         super();
146     }
147
148     public MuleServer(String JavaDoc configResources)
149     {
150         setConfigurationResources(configResources);
151     }
152
153     /**
154      * Start the mule server
155      *
156      * @param ownThread determines if the server will run in its own daemon thread or
157      * the current calling thread
158      */

159     public void start(boolean ownThread)
160     {
161         if (ownThread)
162         {
163             Thread JavaDoc serverThread = new Thread JavaDoc(this, "MuleServer");
164             serverThread.setDaemon(true);
165             serverThread.start();
166         }
167         else
168         {
169             run();
170         }
171     }
172
173     /**
174      * Overloaded the [main] thread run method. This calls initialise and shuts down
175      * if an exception occurs
176      */

177     public void run()
178     {
179         try
180         {
181             initialize();
182         }
183         catch (Throwable JavaDoc e)
184         {
185             shutdown(e);
186         }
187     }
188
189     /**
190      * Sets the configuration builder to use for this server. Note that if a builder
191      * is not set and the server's start method is called the default is an instance
192      * of <code>MuleXmlConfigurationBuilder</code>.
193      *
194      * @param builderClassName the configuration builder FQN to use
195      * @throws ClassNotFoundException if the class with the given name can not be
196      * loaded
197      */

198     public static void setConfigBuilderClassName(String JavaDoc builderClassName) throws ClassNotFoundException JavaDoc
199     {
200         if (builderClassName != null)
201         {
202             Class JavaDoc cls = ClassUtils.loadClass(builderClassName, MuleServer.class);
203             if (ConfigurationBuilder.class.isAssignableFrom(cls))
204             {
205                 MuleServer.configBuilderClassName = builderClassName;
206             }
207             else
208             {
209                 throw new IllegalArgumentException JavaDoc("Not a usable ConfigurationBuilder class: "
210                                                    + builderClassName);
211             }
212         }
213         else
214         {
215             MuleServer.configBuilderClassName = null;
216         }
217     }
218
219     /**
220      * Returns the class name of the configuration builder used to create this
221      * MuleServer.
222      *
223      * @return FQN of the current config builder
224      */

225     public static String JavaDoc getConfigBuilderClassName()
226     {
227         if (configBuilderClassName != null)
228         {
229             return configBuilderClassName;
230         }
231         else
232         {
233             return CLASSNAME_DEFAULT_CONFIG_BUILDER;
234         }
235     }
236
237     /**
238      * Initializes this daemon. Derived classes could add some extra behaviour if
239      * they wish.
240      *
241      * @throws Exception if failed to initialize
242      */

243     protected void initialize() throws Exception JavaDoc
244     {
245         logger.info("Mule Server starting...");
246
247         // TODO Why is this disabled?
248
// registerShutdownHook();
249

250         // install an RMI security manager in case we expose any RMI objects
251
if (System.getSecurityManager() == null)
252         {
253             // TODO Why is this disabled?
254
// System.setSecurityManager(new RMISecurityManager());
255
}
256
257         // create a new ConfigurationBuilder that is disposed afterwards
258
Class JavaDoc cfgBuilderClass = ClassUtils.loadClass(getConfigBuilderClassName(), MuleServer.class);
259         ConfigurationBuilder cfgBuilder = (ConfigurationBuilder)cfgBuilderClass.newInstance();
260
261         if (!cfgBuilder.isConfigured())
262         {
263             if (configurationResources == null)
264             {
265                 logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION);
266                 configurationResources = DEFAULT_CONFIGURATION;
267             }
268             cfgBuilder.configure(configurationResources, getStartupPropertiesFile());
269         }
270         logger.info("Mule Server initialized.");
271     }
272
273     /**
274      * Will shut down the server displaying the cause and time of the shutdown
275      *
276      * @param e the exception that caused the shutdown
277      */

278     void shutdown(Throwable JavaDoc e)
279     {
280         Message msg = new Message(Messages.FATAL_ERROR_WHILE_RUNNING);
281         UMOException muleException = ExceptionHelper.getRootMuleException(e);
282         if (muleException != null)
283         {
284             logger.fatal(muleException.getDetailedMessage());
285         }
286         else
287         {
288             logger.fatal(msg.toString() + " " + e.getMessage(), e);
289         }
290         List JavaDoc msgs = new ArrayList JavaDoc();
291         msgs.add(msg.getMessage());
292         Throwable JavaDoc root = ExceptionHelper.getRootException(e);
293         msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")");
294         msgs.add(" ");
295         msgs.add(new Message(Messages.FATAL_ERROR_SHUTDOWN));
296         msgs.add(new Message(Messages.SERVER_STARTED_AT_X, new Date JavaDoc(MuleManager.getInstance().getStartDate())));
297         msgs.add(new Message(Messages.SERVER_SHUTDOWN_AT_X, new Date JavaDoc().toString()));
298
299         shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80);
300         logger.fatal(shutdownMessage);
301         System.exit(0);
302     }
303
304     /**
305      * shutdown the server. This just displays the time the server shut down
306      */

307     void shutdown()
308     {
309         logger.info("Mule server shutting dow due to normal shutdown request");
310         List JavaDoc msgs = new ArrayList JavaDoc();
311         msgs.add(new Message(Messages.NORMAL_SHUTDOWN).getMessage());
312         msgs.add(new Message(Messages.SERVER_STARTED_AT_X, new Date JavaDoc(MuleManager.getInstance().getStartDate())).getMessage());
313         msgs.add(new Message(Messages.SERVER_SHUTDOWN_AT_X, new Date JavaDoc().toString()).getMessage());
314         shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80);
315
316         System.exit(0);
317
318     }
319
320     // /////////////////////////////////////////////////////////////////
321
// Getters and setters
322
// /////////////////////////////////////////////////////////////////
323

324     /**
325      * Getter for property messengerURL.
326      *
327      * @return Value of property messengerURL.
328      */

329     public String JavaDoc getConfigurationResources()
330     {
331         return configurationResources;
332     }
333
334     /**
335      * Setter for property messengerURL.
336      *
337      * @param configurationResources New value of property configurationResources.
338      */

339     public void setConfigurationResources(String JavaDoc configurationResources)
340     {
341         this.configurationResources = configurationResources;
342     }
343
344     public static String JavaDoc getStartupPropertiesFile()
345     {
346         return startupPropertiesFile;
347     }
348
349     public static void setStartupPropertiesFile(String JavaDoc startupPropertiesFile)
350     {
351         MuleServer.startupPropertiesFile = startupPropertiesFile;
352     }
353 }
354
Popular Tags