View Javadoc

1   /*
2    * $Id: JdmkAgent.java 11517 2008-03-31 21:34:19Z dirk.olmes $
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 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.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.i18n.CoreMessages;
17  import org.mule.util.ClassUtils;
18  import org.mule.util.StringUtils;
19  
20  import java.net.URI;
21  
22  import javax.management.InstanceNotFoundException;
23  import javax.management.MBeanException;
24  import javax.management.MBeanServer;
25  import javax.management.MBeanServerFactory;
26  import javax.management.ObjectName;
27  import javax.management.ReflectionException;
28  
29  /**
30   * <code>JdmkAgent</code> configures an Jdmk Http Adaptor for Jmx management,
31   * statistics and configuration viewing of a Mule instance.
32  * <p/>
33   * TODO MULE-1353 
34   */
35  public class JdmkAgent extends AbstractAgent
36  {
37      /** A FQN of the adaptor class to instantiate via reflection. */
38      public static final String CLASSNAME_ADAPTER = "com.sun.jdmk.comm.HtmlAdaptorServer";
39  
40      private static final String PROTOCOL_PREFIX = "http://";
41      public static final String DEFAULT_HOSTNAME = "localhost";
42      public static final int DEFAULT_PORT = 9092;
43      public static final String DEFAULT_JMX_ADAPTOR_URL = PROTOCOL_PREFIX + DEFAULT_HOSTNAME + ":" + DEFAULT_PORT;
44  
45      private String jmxAdaptorUrl;
46      private String host;
47      private String port;
48  
49      private MBeanServer mBeanServer;
50      private ObjectName adaptorName;
51  
52  
53      public JdmkAgent()
54      {
55          super("jdmk-agent");
56      }
57  
58      protected Object createAdaptor() throws Exception
59      {
60          final URI uri = new URI(jmxAdaptorUrl);
61          final int port = uri.getPort();
62          return ClassUtils.instanciateClass(CLASSNAME_ADAPTER,
63                                             new Object[] {new Integer(port)}, this.getClass());
64      }
65  
66      public String getDescription()
67      {
68          return "Jdmk Http adaptor: " + jmxAdaptorUrl;
69      }
70  
71      public void start() throws MuleException
72      {
73          try
74          {
75              mBeanServer.invoke(adaptorName, "start", null, null);
76          }
77          catch (InstanceNotFoundException e)
78          {
79              throw new JmxManagementException(
80                  CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
81          }
82          catch (MBeanException e)
83          {
84              throw new JmxManagementException(
85                  CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
86          }
87          catch (ReflectionException e)
88          {
89              // ignore
90          }
91      }
92  
93      public void stop() throws MuleException
94      {
95          if (mBeanServer == null)
96          {
97              return;
98          }
99          
100         try
101         {
102             mBeanServer.invoke(adaptorName, "stop", null, null);
103         }
104         catch (InstanceNotFoundException e)
105         {
106             throw new JmxManagementException(
107                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
108         }
109         catch (MBeanException e)
110         {
111             throw new JmxManagementException(
112                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
113         }
114         catch (ReflectionException e)
115         {
116             // ignore
117         }
118     }
119 
120     public void dispose()
121     {
122         try
123         {
124             stop();
125         }
126         catch (Exception e)
127         {
128             // TODO: log an exception
129         }
130     }
131 
132     public void registered()
133     {
134         // nothing to do
135     }
136 
137     public void unregistered()
138     {
139         // nothing to do
140     }
141 
142     public void initialise() throws InitialisationException
143     {
144         try
145         {
146             mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
147             final Object adaptor = createAdaptor();
148             if (StringUtils.isBlank(jmxAdaptorUrl))
149             {
150                 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
151                 {
152                     jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port;
153                 }
154                 else
155                 {
156                     jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL;
157                 }
158             }
159             // TODO use Jmx support classes
160             adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName());
161             mBeanServer.registerMBean(adaptor, adaptorName);
162         }
163         catch (Exception e)
164         {
165             throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this);
166         }
167     }
168 
169     /**
170      * @return Returns the jmxAdaptorUrl.
171      */
172     public String getJmxAdaptorUrl()
173     {
174         return jmxAdaptorUrl;
175     }
176 
177     /**
178      * @param jmxAdaptorUrl The jmxAdaptorUrl to set.
179      */
180     public void setJmxAdaptorUrl(String jmxAdaptorUrl)
181     {
182         this.jmxAdaptorUrl = jmxAdaptorUrl;
183     }
184 
185 
186     public String getHost()
187     {
188         return host;
189     }
190 
191     public void setHost(String host)
192     {
193         this.host = host;
194     }
195 
196     public String getPort()
197     {
198         return port;
199     }
200 
201     public void setPort(String port)
202     {
203         this.port = port;
204     }
205 }