View Javadoc

1   /*
2    * $Id: MuleServerWrapper.java 10789 2008-02-12 20:04:43Z dfeist $
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.module.boot;
12  
13  import org.mule.MuleServer;
14  
15  import org.tanukisoftware.wrapper.WrapperListener;
16  import org.tanukisoftware.wrapper.WrapperManager;
17  
18  public class MuleServerWrapper implements WrapperListener
19  {
20      private MuleServer mule;
21  
22      /*---------------------------------------------------------------
23       * Constructors
24       *-------------------------------------------------------------*/
25      public MuleServerWrapper()
26      {
27          super();
28      }
29  
30      /*---------------------------------------------------------------
31       * WrapperListener Methods
32       *-------------------------------------------------------------*/
33      /**
34       * The start method is called when the WrapperManager is signaled by the native
35       * wrapper code that it can start its application. This method call is expected
36       * to return, so a new thread should be launched if necessary.
37       * 
38       * @param args List of arguments used to initialize the application.
39       * @return Any error code if the application should exit on completion of the
40       *         start method. If there were no problems then this method should return
41       *         null.
42       */
43      public Integer start(String[] args)
44      {
45          try
46          {
47              mule = new MuleServer(args);
48              mule.start(false, false);
49              return null;
50          }
51          catch (Exception e)
52          {
53              e.printStackTrace();
54              return new Integer(1);
55          }
56      }
57  
58      /**
59       * Called when the application is shutting down. The Wrapper assumes that this
60       * method will return fairly quickly. If the shutdown code code could potentially
61       * take a long time, then WrapperManager.signalStopping() should be called to
62       * extend the timeout period. If for some reason, the stop method can not return,
63       * then it must call WrapperManager.stopped() to avoid warning messages from the
64       * Wrapper.
65       * 
66       * @param exitCode The suggested exit code that will be returned to the OS when
67       *            the JVM exits.
68       * @return The exit code to actually return to the OS. In most cases, this should
69       *         just be the value of exitCode, however the user code has the option of
70       *         changing the exit code if there are any problems during shutdown.
71       */
72      public int stop(int exitCode)
73      {
74          mule.shutdown();
75          return exitCode;
76      }
77  
78      /**
79       * Called whenever the native wrapper code traps a system control signal against
80       * the Java process. It is up to the callback to take any actions necessary.
81       * Possible values are: WrapperManager.WRAPPER_CTRL_C_EVENT,
82       * WRAPPER_CTRL_CLOSE_EVENT, WRAPPER_CTRL_LOGOFF_EVENT, or
83       * WRAPPER_CTRL_SHUTDOWN_EVENT
84       * 
85       * @param event The system control signal.
86       */
87      public void controlEvent(int event)
88      {
89          if (WrapperManager.isControlledByNativeWrapper())
90          {
91              // The Wrapper will take care of this event
92          }
93          else
94          {
95              // We are not being controlled by the Wrapper, so
96              // handle the event ourselves.
97              if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT)
98                  || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT)
99                  || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT))
100             {
101                 WrapperManager.stop(0);
102             }
103         }
104     }
105 }