1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.launcher; |
12 | |
|
13 | |
import org.mule.api.DefaultMuleException; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.config.ExceptionHelper; |
16 | |
import org.mule.config.StartupContext; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.config.i18n.Message; |
19 | |
import org.mule.util.MuleUrlStreamHandlerFactory; |
20 | |
import org.mule.util.StringMessageUtils; |
21 | |
import org.mule.util.SystemUtils; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class MuleContainer |
34 | |
{ |
35 | 0 | public static final String CLI_OPTIONS[][] = { |
36 | |
{"builder", "true", "Configuration Builder Type"}, |
37 | |
{"config", "true", "Configuration File"}, |
38 | |
{"idle", "false", "Whether to run in idle (unconfigured) mode"}, |
39 | |
{"main", "true", "Main Class"}, |
40 | |
{"mode", "true", "Run Mode"}, |
41 | |
{"props", "true", "Startup Properties"}, |
42 | |
{"production", "false", "Production Mode"}, |
43 | |
{"debug", "false", "Configure Mule for JPDA remote debugging."}, |
44 | |
{"app", "true", "Application to start"} |
45 | |
}; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | private static final Log logger = LogFactory.getLog(MuleContainer.class); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private static String startupPropertiesFile = null; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private static MuleShutdownHook muleShutdownHook; |
62 | |
|
63 | |
protected DeploymentService deploymentService; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public static void main(String[] args) throws Exception |
71 | |
{ |
72 | 0 | MuleContainer container = new MuleContainer(args); |
73 | 0 | container.start(true); |
74 | 0 | } |
75 | |
|
76 | |
public MuleContainer() |
77 | 0 | { |
78 | 0 | init(new String[0]); |
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public MuleContainer(String[] args) throws IllegalArgumentException |
85 | 0 | { |
86 | 0 | init(args); |
87 | 0 | } |
88 | |
|
89 | |
protected void init(String[] args) throws IllegalArgumentException |
90 | |
{ |
91 | |
Map<String, Object> commandlineOptions; |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 0 | commandlineOptions = SystemUtils.getCommandLineOptions(args, CLI_OPTIONS); |
96 | |
} |
97 | 0 | catch (DefaultMuleException me) |
98 | |
{ |
99 | 0 | throw new IllegalArgumentException(me.toString()); |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | 0 | MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory(); |
105 | |
|
106 | |
|
107 | 0 | String propertiesFile = (String) commandlineOptions.get("props"); |
108 | 0 | if (propertiesFile != null) |
109 | |
{ |
110 | 0 | setStartupPropertiesFile(propertiesFile); |
111 | |
} |
112 | 0 | StartupContext.get().setStartupOptions(commandlineOptions); |
113 | 0 | } |
114 | |
|
115 | |
public void start(boolean registerShutdownHook) |
116 | |
{ |
117 | 0 | if (registerShutdownHook) |
118 | |
{ |
119 | 0 | registerShutdownHook(); |
120 | |
} |
121 | |
|
122 | 0 | final MuleContainerStartupSplashScreen splashScreen = new MuleContainerStartupSplashScreen(); |
123 | 0 | splashScreen.doBody(); |
124 | 0 | logger.info(splashScreen.toString()); |
125 | |
|
126 | |
try |
127 | |
{ |
128 | |
|
129 | 0 | deploymentService = new DeploymentService(); |
130 | 0 | deploymentService.start(); |
131 | |
} |
132 | 0 | catch (Throwable e) |
133 | |
{ |
134 | 0 | shutdown(e); |
135 | 0 | } |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public void shutdown(Throwable e) |
144 | |
{ |
145 | 0 | Message msg = CoreMessages.fatalErrorWhileRunning(); |
146 | 0 | MuleException muleException = ExceptionHelper.getRootMuleException(e); |
147 | 0 | if (muleException != null) |
148 | |
{ |
149 | 0 | logger.fatal(muleException.getDetailedMessage()); |
150 | |
} |
151 | |
else |
152 | |
{ |
153 | 0 | logger.fatal(msg.toString() + " " + e.getMessage(), e); |
154 | |
} |
155 | 0 | List<String> msgs = new ArrayList<String>(); |
156 | 0 | msgs.add(msg.getMessage()); |
157 | 0 | Throwable root = ExceptionHelper.getRootException(e); |
158 | 0 | msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")"); |
159 | 0 | msgs.add(" "); |
160 | 0 | msgs.add(CoreMessages.fatalErrorInShutdown().getMessage()); |
161 | 0 | String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 80); |
162 | 0 | logger.fatal(shutdownMessage); |
163 | |
|
164 | 0 | unregisterShutdownHook(); |
165 | 0 | doShutdown(); |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void shutdown() |
172 | |
{ |
173 | 0 | logger.info("Mule container shutting down due to normal shutdown request"); |
174 | |
|
175 | 0 | unregisterShutdownHook(); |
176 | 0 | doShutdown(); |
177 | 0 | } |
178 | |
|
179 | |
protected void doShutdown() |
180 | |
{ |
181 | 0 | if (deploymentService != null) |
182 | |
{ |
183 | 0 | deploymentService.stop(); |
184 | |
} |
185 | |
|
186 | 0 | System.exit(0); |
187 | 0 | } |
188 | |
|
189 | |
public Log getLogger() |
190 | |
{ |
191 | 0 | return logger; |
192 | |
} |
193 | |
|
194 | |
public void registerShutdownHook() |
195 | |
{ |
196 | 0 | if (muleShutdownHook == null) |
197 | |
{ |
198 | 0 | muleShutdownHook = new MuleShutdownHook(); |
199 | |
} |
200 | |
else |
201 | |
{ |
202 | 0 | Runtime.getRuntime().removeShutdownHook(muleShutdownHook); |
203 | |
} |
204 | 0 | Runtime.getRuntime().addShutdownHook(muleShutdownHook); |
205 | 0 | } |
206 | |
|
207 | |
public void unregisterShutdownHook() |
208 | |
{ |
209 | 0 | if (muleShutdownHook != null) |
210 | |
{ |
211 | 0 | Runtime.getRuntime().removeShutdownHook(muleShutdownHook); |
212 | |
} |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public static String getStartupPropertiesFile() |
221 | |
{ |
222 | 0 | return startupPropertiesFile; |
223 | |
} |
224 | |
|
225 | |
public static void setStartupPropertiesFile(String startupPropertiesFile) |
226 | |
{ |
227 | 0 | MuleContainer.startupPropertiesFile = startupPropertiesFile; |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
private class MuleShutdownHook extends Thread |
236 | |
{ |
237 | |
public MuleShutdownHook() |
238 | 0 | { |
239 | 0 | super("Mule.shutdown.hook"); |
240 | 0 | } |
241 | |
|
242 | |
@Override |
243 | |
public void run() |
244 | |
{ |
245 | 0 | doShutdown(); |
246 | 0 | } |
247 | |
} |
248 | |
} |
249 | |
|