Coverage Report - org.mule.MuleServer
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleServer
0%
0/117
0%
0/36
2.4
MuleServer$1
N/A
N/A
2.4
MuleServer$ShutdownThread
0%
0/4
0%
0/2
2.4
 
 1  
 /*
 2  
  * $Id: MuleServer.java 10659 2008-02-01 10:05:39Z dirk.olmes $
 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 CPAL v1.0
 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 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  
  * <code>MuleServer</code> is a simple application that represents a local Mule
 36  
  * Server daemon. It is initialised with a mule-config.xml file.
 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  
      * Don't use a class object so the core doesn't depend on mule-module-builders.
 53  
      */
 54  
     protected static final String CLASSNAME_DEFAULT_CONFIG_BUILDER = "org.mule.config.builders.MuleXmlConfigurationBuilder";
 55  
 
 56  
     /**
 57  
      * Required to support the '-config spring' shortcut. Don't use a class object so
 58  
      * the core doesn't depend on mule-module-spring. TODO this may not be a problem
 59  
      * for Mule 2.x
 60  
      */
 61  
     protected static final String CLASSNAME_SPRING_CONFIG_BUILDER = "org.mule.extras.spring.config.SpringConfigurationBuilder";
 62  
 
 63  
     /**
 64  
      * logger used by this class
 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  
      * the message to display when the server shuts down
 72  
      */
 73  0
     private static String shutdownMessage = null;
 74  
 
 75  
     /**
 76  
      * one or more configuration urls or filenames separated by commas
 77  
      */
 78  0
     private String configurationResources = null;
 79  
 
 80  
     /**
 81  
      * A FQN of the #configBuilder class, required in case MuleServer is
 82  
      * reinitialised.
 83  
      */
 84  0
     private static String configBuilderClassName = null;
 85  
 
 86  
     /**
 87  
      * A properties file to be read at startup. This can be useful for setting
 88  
      * properties which depend on the run-time environment (dev, test, production).
 89  
      */
 90  0
     private static String startupPropertiesFile = null;
 91  
 
 92  
     /**
 93  
      * Application entry point.
 94  
      *
 95  
      * @param args command-line args
 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  
         // print version string
 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  
         // set our own UrlStreamHandlerFactory to become more independent of system properties
 122  0
         MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory();
 123  
 
 124  0
         String config = (String) options.get("config");
 125  
         // Try default if no config file was given.
 126  0
         if (config == null)
 127  
         {
 128  0
             logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION);
 129  
             // try to load the config as a file as well
 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  
         // Configuration builder
 149  0
         String cfgBuilderClassName = (String) options.get("builder");
 150  0
         if (cfgBuilderClassName != null)
 151  
         {
 152  
             try
 153  
             {
 154  
                 // Provide a shortcut for Spring: "-builder spring"
 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  
         // Startup properties
 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  
      * Start the mule server
 193  
      *
 194  
      * @param ownThread determines if the server will run in its own daemon thread or
 195  
      *                  the current calling thread
 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  
      * Overloaded the [main] thread run method. This calls initialise and shuts down
 213  
      * if an exception occurs
 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  
      * Sets the configuration builder to use for this server. Note that if a builder
 229  
      * is not set and the server's start method is called the default is an instance
 230  
      * of <code>MuleXmlConfigurationBuilder</code>.
 231  
      *
 232  
      * @param builderClassName the configuration builder FQN to use
 233  
      * @throws ClassNotFoundException if the class with the given name can not be
 234  
      *                                loaded
 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  
      * Returns the class name of the configuration builder used to create this
 259  
      * MuleServer.
 260  
      *
 261  
      * @return FQN of the current config builder
 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  
      * Initializes this daemon. Derived classes could add some extra behaviour if
 277  
      * they wish.
 278  
      *
 279  
      * @throws Exception if failed to initialize
 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  
         // create a new ConfigurationBuilder that is disposed afterwards
 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  
      * Will shut down the server displaying the cause and time of the shutdown
 305  
      *
 306  
      * @param e the exception that caused the shutdown
 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  
         // make sure that Mule is shutdown correctly.
 333  0
         MuleManager.getInstance().dispose();
 334  0
         System.exit(0);
 335  0
     }
 336  
 
 337  
     /**
 338  
      * shutdown the server. This just displays the time the server shut down
 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  
         // make sure that Mule is shutdown correctly.
 351  0
         MuleManager.getInstance().dispose();
 352  0
         System.exit(0);
 353  
 
 354  0
     }
 355  
 
 356  
     // /////////////////////////////////////////////////////////////////
 357  
     // Getters and setters
 358  
     // /////////////////////////////////////////////////////////////////
 359  
 
 360  
     /**
 361  
      * Getter for property messengerURL.
 362  
      *
 363  
      * @return Value of property messengerURL.
 364  
      */
 365  
     public String getConfigurationResources()
 366  
     {
 367  0
         return configurationResources;
 368  
     }
 369  
 
 370  
     /**
 371  
      * Setter for property messengerURL.
 372  
      *
 373  
      * @param configurationResources New value of property configurationResources.
 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  
      * This class is installed only for MuleServer running as commandline app. A clean Mule
 392  
      * shutdown can be achieved by disposing the {@link MuleManager}.
 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  
 }