KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > agents > Mx4jAgent


1 /*
2  * $Id: Mx4jAgent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.management.agents;
12
13 import java.net.URI JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.management.InstanceNotFoundException JavaDoc;
18 import javax.management.MBeanException JavaDoc;
19 import javax.management.MBeanRegistrationException JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.MBeanServerFactory JavaDoc;
22 import javax.management.MalformedObjectNameException JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.ReflectionException JavaDoc;
25
26 import mx4j.log.CommonsLogger;
27 import mx4j.log.Log;
28 import mx4j.tools.adaptor.http.HttpAdaptor;
29 import mx4j.tools.adaptor.http.XSLTProcessor;
30 import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactory;
31 import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactoryMBean;
32
33 import org.apache.commons.logging.LogFactory;
34 import org.mule.config.i18n.Message;
35 import org.mule.config.i18n.Messages;
36 import org.mule.management.support.AutoDiscoveryJmxSupportFactory;
37 import org.mule.management.support.JmxSupport;
38 import org.mule.management.support.JmxSupportFactory;
39 import org.mule.umo.UMOException;
40 import org.mule.umo.lifecycle.InitialisationException;
41 import org.mule.umo.manager.UMOAgent;
42 import org.mule.util.BeanUtils;
43 import org.mule.util.StringUtils;
44 import org.mule.util.SystemUtils;
45
46 /**
47  * <code>Mx4jAgent</code> configures an Mx4J Http Adaptor for Jmx management,
48  * statistics and configuration viewing of a Mule instance..
49  */

50 public class Mx4jAgent implements UMOAgent
51 {
52     public static final String JavaDoc HTTP_ADAPTER_OBJECT_NAME = "name=Mx4jHttpAdapter";
53
54     private static final org.apache.commons.logging.Log logger = LogFactory.getLog(Mx4jAgent.class);
55
56     private String JavaDoc name = "MX4J Agent";
57     private String JavaDoc jmxAdaptorUrl = "http://localhost:9999";
58     private HttpAdaptor adaptor;
59     private MBeanServer JavaDoc mBeanServer;
60     private ObjectName JavaDoc adaptorName;
61
62     // Adaptor overrides
63
private String JavaDoc login = null;
64
65     private String JavaDoc password = null;
66
67     private String JavaDoc authenticationMethod = "basic";
68
69     private String JavaDoc xslFilePath = null;
70
71     private String JavaDoc pathInJar = null;
72
73     private boolean cacheXsl = true;
74
75     // SSL/TLS socket factory config
76
private Map JavaDoc socketFactoryProperties = new HashMap JavaDoc();
77
78     private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory();
79     private JmxSupport jmxSupport;
80
81     protected HttpAdaptor createAdaptor() throws Exception JavaDoc
82     {
83
84         Log.redirectTo(new CommonsLogger());
85         URI JavaDoc uri = new URI JavaDoc(StringUtils.stripToEmpty(jmxAdaptorUrl));
86         adaptor = new HttpAdaptor(uri.getPort(), uri.getHost());
87
88         // Set the XSLT Processor with any local overrides
89
XSLTProcessor processor = new XSLTProcessor();
90
91         if (StringUtils.isNotBlank(xslFilePath))
92         {
93             processor.setFile(xslFilePath.trim());
94         }
95
96         if (StringUtils.isNotBlank(pathInJar))
97         {
98             processor.setPathInJar(pathInJar.trim());
99         }
100
101         processor.setUseCache(cacheXsl);
102
103         adaptor.setProcessor(processor);
104
105         // Set endpoint authentication if required
106
if (login != null)
107         {
108             adaptor.addAuthorization(login, password);
109             adaptor.setAuthenticationMethod(authenticationMethod);
110         }
111
112         if (socketFactoryProperties != null && !socketFactoryProperties.isEmpty())
113         {
114             SSLAdaptorServerSocketFactoryMBean factory;
115             if (SystemUtils.isIbmJDK())
116             {
117                 factory = new IBMSslAdapterServerSocketFactory();
118             }
119             else
120             {
121                 // BEA are using Sun's JSSE, so no extra checks necessary
122
factory = new SSLAdaptorServerSocketFactory();
123             }
124             BeanUtils.populateWithoutFail(factory, socketFactoryProperties, true);
125             adaptor.setSocketFactory(factory);
126         }
127
128         return adaptor;
129     }
130
131     /* @see org.mule.umo.lifecycle.Initialisable#initialise() */
132     public void initialise() throws InitialisationException
133     {
134         try
135         {
136             jmxSupport = jmxSupportFactory.newJmxSupport();
137             mBeanServer = (MBeanServer JavaDoc) MBeanServerFactory.findMBeanServer(null).get(0);
138
139             adaptor = createAdaptor();
140             adaptorName = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":" + HTTP_ADAPTER_OBJECT_NAME);
141
142             unregisterMBeansIfNecessary();
143             mBeanServer.registerMBean(adaptor, adaptorName);
144         }
145         catch (Exception JavaDoc e)
146         {
147             throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "mx4j agent"), e, this);
148         }
149     }
150
151     /* @see org.mule.umo.lifecycle.Startable#start() */
152     public void start() throws UMOException
153     {
154         try
155         {
156             mBeanServer.invoke(adaptorName, "start", null, null);
157         }
158         catch (InstanceNotFoundException JavaDoc e)
159         {
160             throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Mx4j agent"),
161                 adaptorName, e);
162         }
163         catch (MBeanException JavaDoc e)
164         {
165             throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Mx4j agent"),
166                 adaptorName, e);
167         }
168         catch (ReflectionException JavaDoc e)
169         {
170             // ignore
171
}
172     }
173
174     /* @see org.mule.umo.lifecycle.Stoppable#stop() */
175     public void stop() throws UMOException
176     {
177         try
178         {
179             mBeanServer.invoke(adaptorName, "stop", null, null);
180         }
181         catch (InstanceNotFoundException JavaDoc e)
182         {
183             throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Mx4j agent"),
184                 adaptorName, e);
185         }
186         catch (MBeanException JavaDoc e)
187         {
188             throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Mx4j agent"),
189                 adaptorName, e);
190         }
191         catch (ReflectionException JavaDoc e)
192         {
193             // ignore
194
}
195     }
196
197     /**
198      * Unregister all Mx4j MBeans if there are any left over the old deployment
199      */

200     protected void unregisterMBeansIfNecessary()
201         throws MalformedObjectNameException JavaDoc, InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
202     {
203         if (mBeanServer.isRegistered(adaptorName))
204         {
205             mBeanServer.unregisterMBean(adaptorName);
206         }
207     }
208
209     /* @see org.mule.umo.lifecycle.Disposable#dispose() */
210     public void dispose()
211     {
212         try
213         {
214             stop();
215         }
216         catch (Exception JavaDoc e)
217         {
218             logger.warn("Failed to stop Mx4jAgent: " + e.getMessage());
219         }
220         finally
221         {
222             try
223             {
224                 unregisterMBeansIfNecessary();
225             }
226             catch (Exception JavaDoc e)
227             {
228                 logger.error("Couldn't unregister MBean: "
229                              + (adaptorName != null ? adaptorName.getCanonicalName() : "null"), e);
230             }
231         }
232     }
233
234     /* @see org.mule.umo.manager.UMOAgent#registered() */
235     public void registered()
236     {
237         // nothing to do
238
}
239
240     /* @see org.mule.umo.manager.UMOAgent#unregistered() */
241     public void unregistered()
242     {
243         // nothing to do
244
}
245
246     // /////////////////////////////////////////////////////////////////////////
247
// Getters and setters
248
// /////////////////////////////////////////////////////////////////////////
249

250     /* @see org.mule.umo.manager.UMOAgent#getDescription() */
251     public String JavaDoc getDescription()
252     {
253         return "MX4J Http adaptor: " + jmxAdaptorUrl;
254     }
255
256     /* @see org.mule.umo.manager.UMOAgent#getName() */
257     public String JavaDoc getName()
258     {
259         return this.name;
260     }
261
262     /* @see org.mule.umo.manager.UMOAgent#setName(java.lang.String) */
263     public void setName(String JavaDoc name)
264     {
265         this.name = name;
266     }
267
268     /** @return Returns the jmxAdaptorUrl. */
269     public String JavaDoc getJmxAdaptorUrl()
270     {
271         return jmxAdaptorUrl;
272     }
273
274     /** @param jmxAdaptorUrl The jmxAdaptorUrl to set. */
275     public void setJmxAdaptorUrl(String JavaDoc jmxAdaptorUrl)
276     {
277         this.jmxAdaptorUrl = jmxAdaptorUrl;
278     }
279
280     public Map JavaDoc getSocketFactoryProperties()
281     {
282         return socketFactoryProperties;
283     }
284
285     public void setSocketFactoryProperties(Map JavaDoc socketFactoryProperties)
286     {
287         this.socketFactoryProperties = socketFactoryProperties;
288     }
289
290     public String JavaDoc getLogin()
291     {
292         return login;
293     }
294
295     public void setLogin(String JavaDoc login)
296     {
297         this.login = login;
298     }
299
300     public String JavaDoc getPassword()
301     {
302         return password;
303     }
304
305     public void setPassword(String JavaDoc password)
306     {
307         this.password = password;
308     }
309
310     public String JavaDoc getAuthenticationMethod()
311     {
312         return authenticationMethod;
313     }
314
315     public void setAuthenticationMethod(String JavaDoc authenticationMethod)
316     {
317         this.authenticationMethod = authenticationMethod;
318     }
319
320     public String JavaDoc getXslFilePath()
321     {
322         return xslFilePath;
323     }
324
325     public void setXslFilePath(String JavaDoc xslFilePath)
326     {
327         this.xslFilePath = xslFilePath;
328     }
329
330     public String JavaDoc getPathInJar()
331     {
332         return pathInJar;
333     }
334
335     public void setPathInJar(String JavaDoc pathInJar)
336     {
337         this.pathInJar = pathInJar;
338     }
339
340     public boolean isCacheXsl()
341     {
342         return cacheXsl;
343     }
344
345     public void setCacheXsl(boolean cacheXsl)
346     {
347         this.cacheXsl = cacheXsl;
348     }
349 }
350
Popular Tags