Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MuleServerWrapper |
|
| 2.25;2.25 |
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 | 0 | super(); |
35 | 0 | } |
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 | 0 | ClassLoader muleSystemCl = new MuleSystemClassLoader(); |
55 | ||
56 | 0 | Thread.currentThread().setContextClassLoader(muleSystemCl); |
57 | ||
58 | 0 | Class muleClass = Thread.currentThread().getContextClassLoader().loadClass("org.mule.MuleServer"); |
59 | 0 | Constructor c = muleClass.getConstructor(String[].class); |
60 | 0 | mule = c.newInstance(new Object[] {args}); |
61 | 0 | Method startMethod = muleClass.getMethod("start", boolean.class, boolean.class); |
62 | 0 | startMethod.invoke(mule, false, false); |
63 | 0 | return null; |
64 | } | |
65 | 0 | catch (Exception e) |
66 | { | |
67 | 0 | e.printStackTrace(); |
68 | 0 | 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 | 0 | Method shutdownMethod = mule.getClass().getMethod("shutdown"); |
91 | 0 | shutdownMethod.invoke(mule); |
92 | } | |
93 | 0 | catch (Throwable t) |
94 | { | |
95 | // ignore | |
96 | 0 | } |
97 | ||
98 | 0 | 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 | 0 | 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 | 0 | if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT) |
121 | || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT) | |
122 | || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT)) | |
123 | { | |
124 | 0 | WrapperManager.stop(0); |
125 | } | |
126 | } | |
127 | 0 | } |
128 | ||
129 | } |