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