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