1 /* 2 * $Id: MuleServerWrapper.java 20321 2010-11-24 15:21:24Z dfeist $ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.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.module.boot; 12 13 import java.lang.reflect.Constructor; 14 import java.lang.reflect.Method; 15 16 import org.tanukisoftware.wrapper.WrapperListener; 17 import org.tanukisoftware.wrapper.WrapperManager; 18 19 public class MuleServerWrapper implements WrapperListener 20 { 21 22 /** 23 * We can't reference MuleServer class literal here, as it will fail to resolve at runtime. 24 * Instead, make all calls anonymous through reflection, so we can safely pump up our new classloader 25 * and make it the default one for downstream applications. 26 */ 27 private Object mule; 28 29 /*--------------------------------------------------------------- 30 * Constructors 31 *-------------------------------------------------------------*/ 32 public MuleServerWrapper() 33 { 34 super(); 35 } 36 37 /*--------------------------------------------------------------- 38 * WrapperListener Methods 39 *-------------------------------------------------------------*/ 40 /** 41 * The start method is called when the WrapperManager is signaled by the native 42 * wrapper code that it can start its application. This method call is expected 43 * to return, so a new thread should be launched if necessary. 44 * 45 * @param args List of arguments used to initialize the application. 46 * @return Any error code if the application should exit on completion of the 47 * start method. If there were no problems then this method should return 48 * null. 49 */ 50 public Integer start(String[] args) 51 { 52 try 53 { 54 ClassLoader muleSystemCl = new MuleSystemClassLoader(); 55 56 Thread.currentThread().setContextClassLoader(muleSystemCl); 57 58 Class muleClass = Thread.currentThread().getContextClassLoader().loadClass("org.mule.MuleServer"); 59 Constructor c = muleClass.getConstructor(String[].class); 60 mule = c.newInstance(new Object[] {args}); 61 Method startMethod = muleClass.getMethod("start", boolean.class, boolean.class); 62 startMethod.invoke(mule, false, false); 63 return null; 64 } 65 catch (Exception e) 66 { 67 e.printStackTrace(); 68 return Integer.valueOf(1); 69 } 70 } 71 72 /** 73 * Called when the application is shutting down. The Wrapper assumes that this 74 * method will return fairly quickly. If the shutdown code code could potentially 75 * take a long time, then WrapperManager.signalStopping() should be called to 76 * extend the timeout period. If for some reason, the stop method can not return, 77 * then it must call WrapperManager.stopped() to avoid warning messages from the 78 * Wrapper. 79 * 80 * @param exitCode The suggested exit code that will be returned to the OS when 81 * the JVM exits. 82 * @return The exit code to actually return to the OS. In most cases, this should 83 * just be the value of exitCode, however the user code has the option of 84 * changing the exit code if there are any problems during shutdown. 85 */ 86 public int stop(int exitCode) 87 { 88 try 89 { 90 Method shutdownMethod = mule.getClass().getMethod("shutdown"); 91 shutdownMethod.invoke(mule); 92 } 93 catch (Throwable t) 94 { 95 // ignore 96 } 97 98 return exitCode; 99 } 100 101 /** 102 * Called whenever the native wrapper code traps a system control signal against 103 * the Java process. It is up to the callback to take any actions necessary. 104 * Possible values are: WrapperManager.WRAPPER_CTRL_C_EVENT, 105 * WRAPPER_CTRL_CLOSE_EVENT, WRAPPER_CTRL_LOGOFF_EVENT, or 106 * WRAPPER_CTRL_SHUTDOWN_EVENT 107 * 108 * @param event The system control signal. 109 */ 110 public void controlEvent(int event) 111 { 112 if (WrapperManager.isControlledByNativeWrapper()) 113 { 114 // The Wrapper will take care of this event 115 } 116 else 117 { 118 // We are not being controlled by the Wrapper, so 119 // handle the event ourselves. 120 if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT) 121 || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT) 122 || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT)) 123 { 124 WrapperManager.stop(0); 125 } 126 } 127 } 128 129 }