1 10 11 package org.mule; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.Date ; 16 import java.util.List ; 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 36 public class MuleServer implements Runnable 37 { 38 41 protected static final String CLASSNAME_DEFAULT_CONFIG_BUILDER = "org.mule.config.builders.MuleXmlConfigurationBuilder"; 42 43 48 protected static final String CLASSNAME_SPRING_CONFIG_BUILDER = "org.mule.extras.spring.config.SpringConfigurationBuilder"; 49 50 53 private static Log logger = LogFactory.getLog(MuleServer.class); 54 55 public static final String DEFAULT_CONFIGURATION = "mule-config.xml"; 56 57 60 private static String shutdownMessage = null; 61 62 65 private String configurationResources = null; 66 67 71 private static String configBuilderClassName = null; 72 73 77 private static String startupPropertiesFile = null; 78 79 84 public static void main(String [] args) 85 { 86 MuleServer server = new MuleServer(); 87 88 String config = SystemUtils.getCommandLineOption("-config", args); 89 if (config == null) 91 { 92 logger.warn("A configuration file was not set, using default: " + DEFAULT_CONFIGURATION); 93 URL 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 String cfgBuilderClassName = SystemUtils.getCommandLineOption("-builder", args); 113 if (cfgBuilderClassName != null) 114 { 115 try 116 { 117 if (cfgBuilderClassName.equalsIgnoreCase("spring")) 119 { 120 cfgBuilderClassName = CLASSNAME_SPRING_CONFIG_BUILDER; 121 } 122 setConfigBuilderClassName(cfgBuilderClassName); 123 } 124 catch (Exception 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 String 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 configResources) 149 { 150 setConfigurationResources(configResources); 151 } 152 153 159 public void start(boolean ownThread) 160 { 161 if (ownThread) 162 { 163 Thread serverThread = new Thread (this, "MuleServer"); 164 serverThread.setDaemon(true); 165 serverThread.start(); 166 } 167 else 168 { 169 run(); 170 } 171 } 172 173 177 public void run() 178 { 179 try 180 { 181 initialize(); 182 } 183 catch (Throwable e) 184 { 185 shutdown(e); 186 } 187 } 188 189 198 public static void setConfigBuilderClassName(String builderClassName) throws ClassNotFoundException 199 { 200 if (builderClassName != null) 201 { 202 Class 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 ("Not a usable ConfigurationBuilder class: " 210 + builderClassName); 211 } 212 } 213 else 214 { 215 MuleServer.configBuilderClassName = null; 216 } 217 } 218 219 225 public static String getConfigBuilderClassName() 226 { 227 if (configBuilderClassName != null) 228 { 229 return configBuilderClassName; 230 } 231 else 232 { 233 return CLASSNAME_DEFAULT_CONFIG_BUILDER; 234 } 235 } 236 237 243 protected void initialize() throws Exception 244 { 245 logger.info("Mule Server starting..."); 246 247 250 if (System.getSecurityManager() == null) 252 { 253 } 256 257 Class 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 278 void shutdown(Throwable 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 msgs = new ArrayList (); 291 msgs.add(msg.getMessage()); 292 Throwable 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 (MuleManager.getInstance().getStartDate()))); 297 msgs.add(new Message(Messages.SERVER_SHUTDOWN_AT_X, new Date ().toString())); 298 299 shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80); 300 logger.fatal(shutdownMessage); 301 System.exit(0); 302 } 303 304 307 void shutdown() 308 { 309 logger.info("Mule server shutting dow due to normal shutdown request"); 310 List msgs = new ArrayList (); 311 msgs.add(new Message(Messages.NORMAL_SHUTDOWN).getMessage()); 312 msgs.add(new Message(Messages.SERVER_STARTED_AT_X, new Date (MuleManager.getInstance().getStartDate())).getMessage()); 313 msgs.add(new Message(Messages.SERVER_SHUTDOWN_AT_X, new Date ().toString()).getMessage()); 314 shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80); 315 316 System.exit(0); 317 318 } 319 320 324 329 public String getConfigurationResources() 330 { 331 return configurationResources; 332 } 333 334 339 public void setConfigurationResources(String configurationResources) 340 { 341 this.configurationResources = configurationResources; 342 } 343 344 public static String getStartupPropertiesFile() 345 { 346 return startupPropertiesFile; 347 } 348 349 public static void setStartupPropertiesFile(String startupPropertiesFile) 350 { 351 MuleServer.startupPropertiesFile = startupPropertiesFile; 352 } 353 } 354 | Popular Tags |