Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* $Id: MuleServer.java 20954 2011-01-11 12:44:49Z esteban.robles $
* --------------------------------------------------------------------------------------
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/package org.mule;
import org.mule.api.DefaultMuleException;
import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.config.ConfigurationBuilder;
import org.mule.api.config.ConfigurationException;
import org.mule.api.config.MuleConfiguration;
import org.mule.config.ExceptionHelper;
import org.mule.config.StartupContext;
import org.mule.config.builders.SimpleConfigurationBuilder;
import org.mule.config.PropertiesMuleConfigurationFactory;
import org.mule.config.i18n.CoreMessages;
import org.mule.config.i18n.Message;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.mule.util.ClassUtils;
import org.mule.util.IOUtils;
import org.mule.util.MuleUrlStreamHandlerFactory;
import org.mule.util.PropertiesUtils;
import org.mule.util.StringMessageUtils;
import org.mule.util.SystemUtils;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* MuleServer is a simple application that represents a local Mule
* Server daemon. It is initialised with a mule-config.xml file.
*/publicclassMuleServerimplementsRunnable{
publicstaticfinal String CLI_OPTIONS[][] = {
{"builder", "true", "Configuration Builder Type"},
{"config", "true", "Configuration File"},
{"appconfig", "true", "Application configuration File"},
{"idle", "false", "Whether to run in idle (unconfigured) mode"},
{"main", "true", "Main Class"},
{"mode", "true", "Run Mode"},
{"props", "true", "Startup Properties"},
{"production", "false", "Production Mode"},
{"debug", "false", "Configure Mule for JPDA remote debugging."}
};
/**
* Don't use a class object so the core doesn't depend on mule-module-spring-config.
*/protectedstaticfinal String CLASSNAME_DEFAULT_CONFIG_BUILDER = "org.mule.config.builders.AutoConfigurationBuilder";
/**
* This builder sets up the configuration for an idle Mule node - a node that
* doesn't do anything initially but is fed configuration during runtime
*/protectedstaticfinal String CLASSNAME_DEFAULT_IDLE_CONFIG_BUILDER = "org.mule.config.builders.MuleIdleConfigurationBuilder";
/**
* Required to support the '-config spring' shortcut. Don't use a class object so
* the core doesn't depend on mule-module-spring.
* for Mule 2.x
*/protectedstaticfinal String CLASSNAME_SPRING_CONFIG_BUILDER = "org.mule.config.spring.SpringXmlConfigurationBuilder";
/**
* If the annotations module is on the classpath, also enable annotations config builder
*/publicstaticfinal String CLASSNAME_ANNOTATIONS_CONFIG_BUILDER = "org.mule.config.AnnotationsConfigurationBuilder";
/**
* logger used by this class
*/privatestaticfinal Log logger = LogFactory.getLog(MuleServer.class);
publicstaticfinal String DEFAULT_CONFIGURATION = "mule-config.xml";
publicstaticfinal String DEFAULT_APP_CONFIGURATION = "mule-app.properties";
/**
* one or more configuration urls or filenames separated by commas
*/private String configurationResources = null;
private String appConfigurationResource = null;
/**
* A FQN of the #configBuilder class, required in case MuleServer is
* reinitialised.
*/privatestatic String configBuilderClassName = null;
/**
* A properties file to be read at startup. This can be useful for setting
* properties which depend on the run-time environment (dev, test, production).
*/privatestatic String startupPropertiesFile = null;
/**
* The Runtime shutdown thread used to dispose this server
*/privatestatic MuleShutdownHook muleShutdownHook;
/**
* The MuleContext should contain anything which does not belong in the Registry.
* There is one MuleContext per Mule instance. Assuming it has been created, a
* handle to the local MuleContext can be obtained from anywhere by calling
* MuleServer.getMuleContext()
*/protectedstatic MuleContext muleContext = null;
/**
* Application entry point.
*
* @param args command-line args
* @throws Exception if there is an exception creating the MuleServer
*/publicstaticvoidmain(String[] args)throws Exception
{
MuleServer server = new MuleServer(args);
server.start(false, true);
}
publicMuleServer(){
init(new String[]{});
}
publicMuleServer(String configResources){
// setConfigurationResources(configResources);
init(new String[]{"-config", configResources});
}
/**
* Configure the server with command-line arguments.
* @param args Command line args passed in from the {@link #main(String[])} method
* @throws IllegalArgumentException if an argument is passed in that is not recognised by the Mule Server
*/publicMuleServer(String[] args)throws IllegalArgumentException
{
init(args);
}
protectedvoidinit(String[] args)throws IllegalArgumentException
{
Map commandlineOptions;
try
{
commandlineOptions = SystemUtils.getCommandLineOptions(args, CLI_OPTIONS);
}
catch (DefaultMuleException me)
{
thrownew IllegalArgumentException(me.toString());
}
// set our own UrlStreamHandlerFactory to become more independent of system// properties
MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory();
String config = (String) commandlineOptions.get("config");
// Try default if no config file was given.if (config == null && !commandlineOptions.containsKey("idle"))
{
logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION);
// try to load the config as a file as well
URL configUrl = IOUtils.getResourceAsUrl(DEFAULT_CONFIGURATION, MuleServer.class, true, false);
if (configUrl != null)
{
config = configUrl.toExternalForm();
}
else
{
System.out.println(CoreMessages.configNotFoundUsage());
System.exit(-1);
}
}
if (config != null)
{
setConfigurationResources(config);
}
String appconfig = (String) commandlineOptions.get("appconfig");
this.appConfigurationResource = appconfig;
// Configuration builder
String cfgBuilderClassName = (String) commandlineOptions.get("builder");
if (commandlineOptions.containsKey("idle"))
{
setConfigurationResources("IDLE");
cfgBuilderClassName = CLASSNAME_DEFAULT_IDLE_CONFIG_BUILDER;
}
// Configuration builderif (cfgBuilderClassName != null)
{
try
{
// Provide a shortcut for Spring: "-builder spring"if (cfgBuilderClassName.equalsIgnoreCase("spring"))
{
cfgBuilderClassName = CLASSNAME_SPRING_CONFIG_BUILDER;
}
setConfigBuilderClassName(cfgBuilderClassName);
}
catch (Exception e)
{
logger.fatal(e);
final Message message = CoreMessages.failedToLoad("Builder: " + cfgBuilderClassName);
System.err.println(StringMessageUtils.getBoilerPlate("FATAL: " + message.toString()));
System.exit(1);
}
}
// Startup properties
String propertiesFile = (String) commandlineOptions.get("props");
if (propertiesFile != null)
{
setStartupPropertiesFile(propertiesFile);
}
StartupContext.get().setStartupOptions(commandlineOptions);
}
/**
* Start the mule server
*
* @param ownThread determines if the server will run in its own daemon thread or
* the current calling thread
* @param registerShutdownHook whether to register the default Mule Server shutdown hock. this will shut down mule cleanly if
* the JVM is shutdown. The only reason not to register this hook is to override it with a custom version
*/publicvoidstart(boolean ownThread, boolean registerShutdownHook){
if (registerShutdownHook)
{
registerShutdownHook();
}
if (ownThread)
{
Thread serverThread = new Thread(this, "MuleServer");
serverThread.setDaemon(true);
serverThread.start();
}
else
{
run();
}
}
/**
* Overloaded the [main] thread run method. This calls initialise and shuts down
* if an exception occurs
*/publicvoidrun(){
try
{
logger.info("Mule Server initializing...");
initialize();
logger.info("Mule Server starting...");
muleContext.start();
}
catch (Throwable e)
{
shutdown(e);
}
}
/**
* Sets the configuration builder to use for this server. Note that if a builder
* is not set and the server's start method is called the default is an instance
* of SpringXmlConfigurationBuilder.
*
* @param builderClassName the configuration builder FQN to use
* @throws ClassNotFoundException if the class with the given name can not be
* loaded
*/publicstaticvoidsetConfigBuilderClassName(String builderClassName)throws ClassNotFoundException
{
if (builderClassName != null)
{
Class cls = ClassUtils.loadClass(builderClassName, MuleServer.class);
if (ConfigurationBuilder.class.isAssignableFrom(cls))
{
MuleServer.configBuilderClassName = builderClassName;
}
else
{
thrownew IllegalArgumentException("Not a usable ConfigurationBuilder class: "
+ builderClassName);
}
}
else
{
MuleServer.configBuilderClassName = null;
}
}
/**
* Returns the class name of the configuration builder used to create this
* MuleServer.
*
* @return FQN of the current config builder
*/publicstatic String getConfigBuilderClassName(){
if (configBuilderClassName != null)
{
return configBuilderClassName;
}
else
{
return CLASSNAME_DEFAULT_CONFIG_BUILDER;
}
}
/**
* Initializes this daemon. Derived classes could add some extra behaviour if
* they wish.
*
* @throws Exception if failed to initialize
*/publicvoidinitialize()throws Exception
{
if (configurationResources == null)
{
logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION);
configurationResources = DEFAULT_CONFIGURATION;
}
ConfigurationBuilder cfgBuilder;
try
{
// create a new ConfigurationBuilder that is disposed afterwards
cfgBuilder = (ConfigurationBuilder) ClassUtils.instanciateClass(getConfigBuilderClassName(),
new Object[]{configurationResources}, MuleServer.class);
}
catch (Exception e)
{
thrownew ConfigurationException(CoreMessages.failedToLoad(getConfigBuilderClassName()), e);
}
if (!cfgBuilder.isConfigured())
{
List builders = new ArrayList(2);
// If the annotations module is on the classpath, add the annotations config builder to the list// This will enable annotations config for this instance//We need to add this builder before spring so that we can use Mule annotations in Springif (ClassUtils.isClassOnPath(CLASSNAME_ANNOTATIONS_CONFIG_BUILDER, getClass()))
{
Object configBuilder = ClassUtils.instanciateClass(
CLASSNAME_ANNOTATIONS_CONFIG_BUILDER, ClassUtils.NO_ARGS, getClass());
builders.add((ConfigurationBuilder) configBuilder);
}
Properties startupProperties = null;
if (getStartupPropertiesFile() != null)
{
startupProperties = PropertiesUtils.loadProperties(getStartupPropertiesFile(), getClass());
}
builders.add(new SimpleConfigurationBuilder(startupProperties));
builders.add(cfgBuilder);
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
String muleAppConfig = this.appConfigurationResource == null
? PropertiesMuleConfigurationFactory.getMuleAppConfiguration(this.configurationResources)
: this.appConfigurationResource;
MuleConfiguration configuration = new PropertiesMuleConfigurationFactory(muleAppConfig).createConfiguration();
muleContext = muleContextFactory.createMuleContext(cfgBuilder, new Properties(), configuration);
}
}
/**
* Will shut down the server displaying the cause and time of the shutdown
*
* @param e the exception that caused the shutdown
*/publicvoidshutdown(Throwable e){
doShutdown();
unregisterShutdownHook();
Message msg = CoreMessages.fatalErrorWhileRunning();
MuleException muleException = ExceptionHelper.getRootMuleException(e);
int exitCode = 1;
if (muleException != null)
{
logger.fatal(muleException.getDetailedMessage());
exitCode = muleException.getExceptionCode();
}
else
{
logger.fatal(msg.toString() + " " + e.getMessage(), e);
}
List msgs = new ArrayList();
msgs.add(msg.getMessage());
Throwable root = ExceptionHelper.getRootException(e);
msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")");
msgs.add(" ");
msgs.add(CoreMessages.fatalErrorInShutdown().getMessage());
String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80);
logger.fatal(shutdownMessage);
System.exit(exitCode);
}
/**
* shutdown the server. This just displays the time the server shut down
*/publicvoidshutdown(){
logger.info("Mule server shutting down due to normal shutdown request");
unregisterShutdownHook();
doShutdown();
System.exit(0);
}
protectedvoiddoShutdown(){
if (muleContext != null)
{
muleContext.dispose();
muleContext = null;
}
}
public Log getLogger(){
return logger;
}
publicvoidregisterShutdownHook(){
if (muleShutdownHook == null)
{
muleShutdownHook = new MuleShutdownHook();
}
else
{
Runtime.getRuntime().removeShutdownHook(muleShutdownHook);
}
Runtime.getRuntime().addShutdownHook(muleShutdownHook);
}
publicvoidunregisterShutdownHook(){
if (muleShutdownHook != null)
{
Runtime.getRuntime().removeShutdownHook(muleShutdownHook);
}
}
// /////////////////////////////////////////////////////////////////// Getters and setters// //////////////////////////////////////////////////////////////////**
* Getter for property messengerURL.
*
* @return Value of property messengerURL.
*/public String getConfigurationResources(){
return configurationResources;
}
/**
* Setter for property configurationResources.
*
* @param configurationResources New value of property configurationResources.
*/publicvoidsetConfigurationResources(String configurationResources){
this.configurationResources = configurationResources;
}
publicstatic String getStartupPropertiesFile(){
return startupPropertiesFile;
}
publicstaticvoidsetStartupPropertiesFile(String startupPropertiesFile){
MuleServer.startupPropertiesFile = startupPropertiesFile;
}
public MuleContext getMuleContext(){
return muleContext;
}
/**
* This class is installed only for MuleServer running as commandline app. A
* clean Mule shutdown can be achieved by disposing the
* {@link org.mule.DefaultMuleContext}.
*/privateclassMuleShutdownHookextendsThread{
publicMuleShutdownHook(){
super();
}
@Overridepublicvoidrun(){
doShutdown();
System.exit(0);
}
}
}