1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.management.agent; |
12 | |
|
13 | |
import org.mule.AbstractAgent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.lifecycle.InitialisationException; |
16 | |
import org.mule.config.MuleManifest; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.config.i18n.MessageFactory; |
19 | |
import org.mule.module.management.mbean.MBeanServerFactory; |
20 | |
import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory; |
21 | |
import org.mule.module.management.support.JmxSupport; |
22 | |
import org.mule.module.management.support.JmxSupportFactory; |
23 | |
import org.mule.module.xml.util.XMLUtils; |
24 | |
import org.mule.util.BeanUtils; |
25 | |
import org.mule.util.ClassUtils; |
26 | |
import org.mule.util.StringUtils; |
27 | |
import org.mule.util.SystemUtils; |
28 | |
|
29 | |
import java.net.URI; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
import javax.management.InstanceNotFoundException; |
34 | |
import javax.management.MBeanException; |
35 | |
import javax.management.MBeanRegistrationException; |
36 | |
import javax.management.MBeanServer; |
37 | |
import javax.management.MalformedObjectNameException; |
38 | |
import javax.management.ObjectName; |
39 | |
import javax.management.ReflectionException; |
40 | |
import javax.xml.transform.TransformerFactoryConfigurationError; |
41 | |
|
42 | |
import mx4j.log.CommonsLogger; |
43 | |
import mx4j.log.Log; |
44 | |
import mx4j.tools.adaptor.http.HttpAdaptor; |
45 | |
import mx4j.tools.adaptor.http.XSLTProcessor; |
46 | |
import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactory; |
47 | |
import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactoryMBean; |
48 | |
|
49 | |
import org.apache.commons.logging.LogFactory; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class Mx4jAgent extends AbstractAgent |
58 | |
{ |
59 | |
public static final String HTTP_ADAPTER_OBJECT_NAME = "name=Mx4jHttpAdapter"; |
60 | |
|
61 | 8 | protected static final String DEFAULT_PATH_IN_JAR = StringUtils.replaceChars(ClassUtils.getPackageName(Mx4jAgent.class), '.', '/') + |
62 | |
"/http/xsl"; |
63 | |
|
64 | 8 | private static final org.apache.commons.logging.Log logger = LogFactory.getLog(Mx4jAgent.class); |
65 | |
|
66 | |
private static final String PROTOCOL_PREFIX = "http://"; |
67 | |
public static final String DEFAULT_HOSTNAME = "localhost"; |
68 | |
public static final int DEFAULT_PORT = 9999; |
69 | |
public static final String DEFAULT_JMX_ADAPTOR_URL = PROTOCOL_PREFIX + DEFAULT_HOSTNAME + ":" + DEFAULT_PORT; |
70 | |
|
71 | |
private String jmxAdaptorUrl; |
72 | |
private String host; |
73 | |
private String port; |
74 | |
|
75 | |
private HttpAdaptor adaptor; |
76 | |
private MBeanServer mBeanServer; |
77 | |
private ObjectName adaptorName; |
78 | |
|
79 | |
|
80 | |
private String login; |
81 | |
|
82 | |
private String password; |
83 | |
|
84 | 10 | private String authenticationMethod = "basic"; |
85 | |
|
86 | |
|
87 | 10 | private String xslFilePath = System.getProperty("mule.home") + "/lib/mule/mule-module-management-" + |
88 | |
MuleManifest.getProductVersion() + ".jar"; |
89 | |
|
90 | 10 | private String pathInJar = DEFAULT_PATH_IN_JAR; |
91 | |
|
92 | 10 | private boolean cacheXsl = true; |
93 | |
|
94 | |
|
95 | 10 | private Map socketFactoryProperties = new HashMap(); |
96 | |
|
97 | 10 | private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance(); |
98 | |
private JmxSupport jmxSupport; |
99 | |
|
100 | |
|
101 | |
public Mx4jAgent() |
102 | |
{ |
103 | 10 | super("jmx-mx4j-adaptor"); |
104 | 10 | } |
105 | |
|
106 | |
protected HttpAdaptor createAdaptor() throws Exception |
107 | |
{ |
108 | |
|
109 | 10 | Log.redirectTo(new CommonsLogger()); |
110 | 10 | URI uri = new URI(StringUtils.stripToEmpty(jmxAdaptorUrl)); |
111 | 10 | adaptor = new HttpAdaptor(uri.getPort(), uri.getHost()); |
112 | |
|
113 | |
|
114 | |
XSLTProcessor processor; |
115 | |
try |
116 | |
{ |
117 | 10 | processor = new XSLTProcessor(); |
118 | |
} |
119 | 0 | catch (TransformerFactoryConfigurationError e) |
120 | |
{ |
121 | 0 | System.setProperty("javax.xml.transform.TransformerFactory", XMLUtils.TRANSFORMER_FACTORY_JDK5); |
122 | 0 | processor = new XSLTProcessor(); |
123 | 10 | } |
124 | |
|
125 | 10 | if (StringUtils.isNotBlank(xslFilePath)) |
126 | |
{ |
127 | 10 | processor.setFile(xslFilePath.trim()); |
128 | |
} |
129 | |
|
130 | 10 | if (StringUtils.isNotBlank(pathInJar)) |
131 | |
{ |
132 | 10 | processor.setPathInJar(pathInJar.trim()); |
133 | |
} |
134 | |
|
135 | 10 | processor.setUseCache(cacheXsl); |
136 | |
|
137 | 10 | adaptor.setProcessor(processor); |
138 | |
|
139 | |
|
140 | 10 | if (login != null) |
141 | |
{ |
142 | 0 | adaptor.addAuthorization(login, password); |
143 | 0 | adaptor.setAuthenticationMethod(authenticationMethod); |
144 | |
} |
145 | |
|
146 | 10 | if (socketFactoryProperties != null && !socketFactoryProperties.isEmpty()) |
147 | |
{ |
148 | |
SSLAdaptorServerSocketFactoryMBean factory; |
149 | 0 | if (SystemUtils.isIbmJDK()) |
150 | |
{ |
151 | 0 | factory = new IBMSslAdapterServerSocketFactory(); |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | |
|
156 | 0 | factory = new SSLAdaptorServerSocketFactory(); |
157 | |
} |
158 | 0 | BeanUtils.populateWithoutFail(factory, socketFactoryProperties, true); |
159 | 0 | adaptor.setSocketFactory(factory); |
160 | |
} |
161 | |
|
162 | 10 | return adaptor; |
163 | |
} |
164 | |
|
165 | |
public void initialise() throws InitialisationException |
166 | |
{ |
167 | |
try |
168 | |
{ |
169 | 10 | jmxSupport = jmxSupportFactory.getJmxSupport(); |
170 | 10 | mBeanServer = MBeanServerFactory.getOrCreateMBeanServer(); |
171 | |
|
172 | 10 | if (StringUtils.isBlank(jmxAdaptorUrl)) |
173 | |
{ |
174 | 6 | if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port)) |
175 | |
{ |
176 | 0 | jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port; |
177 | |
} |
178 | |
else |
179 | |
{ |
180 | 6 | jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL; |
181 | |
} |
182 | |
} |
183 | |
|
184 | 10 | adaptor = createAdaptor(); |
185 | 10 | adaptorName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + HTTP_ADAPTER_OBJECT_NAME); |
186 | |
|
187 | 10 | unregisterMBeansIfNecessary(); |
188 | 10 | mBeanServer.registerMBean(adaptor, adaptorName); |
189 | |
} |
190 | 0 | catch (Exception e) |
191 | |
{ |
192 | 0 | throw new InitialisationException(CoreMessages.failedToStart("mx4j agent"), e, this); |
193 | 10 | } |
194 | 10 | } |
195 | |
|
196 | |
public void start() throws MuleException |
197 | |
{ |
198 | 8 | if (mBeanServer == null) |
199 | |
{ |
200 | 0 | throw new InitialisationException(MessageFactory.createStaticMessage("mBeanServer has not yet been created"), this); |
201 | |
} |
202 | |
|
203 | |
try |
204 | |
{ |
205 | 8 | mBeanServer.invoke(adaptorName, "start", null, null); |
206 | |
} |
207 | 0 | catch (InstanceNotFoundException e) |
208 | |
{ |
209 | 0 | throw new JmxManagementException( |
210 | |
CoreMessages.failedToStart("Mx4j agent"), adaptorName, e); |
211 | |
} |
212 | 0 | catch (MBeanException e) |
213 | |
{ |
214 | 0 | throw new JmxManagementException( |
215 | |
CoreMessages.failedToStart("Mx4j agent"), adaptorName, e); |
216 | |
} |
217 | 0 | catch (ReflectionException e) |
218 | |
{ |
219 | |
|
220 | 8 | } |
221 | 8 | } |
222 | |
|
223 | |
public void stop() throws MuleException |
224 | |
{ |
225 | 16 | if (mBeanServer == null) |
226 | |
{ |
227 | 0 | return; |
228 | |
} |
229 | |
try |
230 | |
{ |
231 | 16 | mBeanServer.invoke(adaptorName, "stop", null, null); |
232 | |
} |
233 | 0 | catch (InstanceNotFoundException e) |
234 | |
{ |
235 | 0 | throw new JmxManagementException( |
236 | |
CoreMessages.failedToStop("Mx4j agent"), adaptorName, e); |
237 | |
} |
238 | 0 | catch (MBeanException e) |
239 | |
{ |
240 | 0 | throw new JmxManagementException( |
241 | |
CoreMessages.failedToStop("Mx4j agent"), adaptorName, e); |
242 | |
} |
243 | 0 | catch (ReflectionException e) |
244 | |
{ |
245 | |
|
246 | 16 | } |
247 | 16 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
protected void unregisterMBeansIfNecessary() |
253 | |
throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException |
254 | |
{ |
255 | 18 | if (mBeanServer != null && mBeanServer.isRegistered(adaptorName)) |
256 | |
{ |
257 | 10 | mBeanServer.unregisterMBean(adaptorName); |
258 | |
} |
259 | 18 | } |
260 | |
|
261 | |
|
262 | |
public void dispose() |
263 | |
{ |
264 | |
try |
265 | |
{ |
266 | 8 | stop(); |
267 | |
} |
268 | 0 | catch (Exception e) |
269 | |
{ |
270 | 0 | logger.warn("Failed to stop Mx4jAgent: " + e.getMessage()); |
271 | |
} |
272 | |
finally |
273 | |
{ |
274 | 0 | try |
275 | |
{ |
276 | 8 | unregisterMBeansIfNecessary(); |
277 | |
} |
278 | 0 | catch (Exception e) |
279 | |
{ |
280 | 0 | logger.error("Couldn't unregister MBean: " |
281 | |
+ (adaptorName != null ? adaptorName.getCanonicalName() : "null"), e); |
282 | 8 | } |
283 | 0 | } |
284 | 8 | } |
285 | |
|
286 | |
|
287 | |
public void registered() |
288 | |
{ |
289 | |
|
290 | 0 | } |
291 | |
|
292 | |
|
293 | |
public void unregistered() |
294 | |
{ |
295 | |
|
296 | 0 | } |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public String getDescription() |
304 | |
{ |
305 | 0 | return "MX4J Http adaptor: " + jmxAdaptorUrl; |
306 | |
} |
307 | |
|
308 | |
|
309 | |
public String getJmxAdaptorUrl() |
310 | |
{ |
311 | 2 | return jmxAdaptorUrl; |
312 | |
} |
313 | |
|
314 | |
|
315 | |
public void setJmxAdaptorUrl(String jmxAdaptorUrl) |
316 | |
{ |
317 | 4 | this.jmxAdaptorUrl = jmxAdaptorUrl; |
318 | 4 | } |
319 | |
|
320 | |
public Map getSocketFactoryProperties() |
321 | |
{ |
322 | 0 | return socketFactoryProperties; |
323 | |
} |
324 | |
|
325 | |
public void setSocketFactoryProperties(Map socketFactoryProperties) |
326 | |
{ |
327 | 0 | this.socketFactoryProperties = socketFactoryProperties; |
328 | 0 | } |
329 | |
|
330 | |
public String getLogin() |
331 | |
{ |
332 | 0 | return login; |
333 | |
} |
334 | |
|
335 | |
public void setLogin(String login) |
336 | |
{ |
337 | 0 | this.login = login; |
338 | 0 | } |
339 | |
|
340 | |
public String getPassword() |
341 | |
{ |
342 | 0 | return password; |
343 | |
} |
344 | |
|
345 | |
public void setPassword(String password) |
346 | |
{ |
347 | 0 | this.password = password; |
348 | 0 | } |
349 | |
|
350 | |
public String getAuthenticationMethod() |
351 | |
{ |
352 | 0 | return authenticationMethod; |
353 | |
} |
354 | |
|
355 | |
public void setAuthenticationMethod(String authenticationMethod) |
356 | |
{ |
357 | 4 | this.authenticationMethod = authenticationMethod; |
358 | 4 | } |
359 | |
|
360 | |
public String getXslFilePath() |
361 | |
{ |
362 | 0 | return xslFilePath; |
363 | |
} |
364 | |
|
365 | |
public void setXslFilePath(String xslFilePath) |
366 | |
{ |
367 | 0 | this.xslFilePath = xslFilePath; |
368 | 0 | } |
369 | |
|
370 | |
public String getPathInJar() |
371 | |
{ |
372 | 0 | return pathInJar; |
373 | |
} |
374 | |
|
375 | |
public void setPathInJar(String pathInJar) |
376 | |
{ |
377 | 0 | this.pathInJar = pathInJar; |
378 | 0 | } |
379 | |
|
380 | |
public boolean isCacheXsl() |
381 | |
{ |
382 | 0 | return cacheXsl; |
383 | |
} |
384 | |
|
385 | |
public void setCacheXsl(boolean cacheXsl) |
386 | |
{ |
387 | 4 | this.cacheXsl = cacheXsl; |
388 | 4 | } |
389 | |
|
390 | |
|
391 | |
public String getHost() |
392 | |
{ |
393 | 0 | return host; |
394 | |
} |
395 | |
|
396 | |
public void setHost(String host) |
397 | |
{ |
398 | 0 | this.host = host; |
399 | 0 | } |
400 | |
|
401 | |
public String getPort() |
402 | |
{ |
403 | 0 | return port; |
404 | |
} |
405 | |
|
406 | |
public void setPort(String port) |
407 | |
{ |
408 | 0 | this.port = port; |
409 | 0 | } |
410 | |
} |