View Javadoc

1   /*
2    * $Id: JdmkAgent.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 initialise() throws InitialisationException
133     {
134         try
135         {
136             mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
137             final Object adaptor = createAdaptor();
138             if (StringUtils.isBlank(jmxAdaptorUrl))
139             {
140                 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
141                 {
142                     jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port;
143                 }
144                 else
145                 {
146                     jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL;
147                 }
148             }
149             // TODO use Jmx support classes
150             adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName());
151             mBeanServer.registerMBean(adaptor, adaptorName);
152         }
153         catch (Exception e)
154         {
155             throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this);
156         }
157     }
158 
159     /**
160      * @return Returns the jmxAdaptorUrl.
161      */
162     public String getJmxAdaptorUrl()
163     {
164         return jmxAdaptorUrl;
165     }
166 
167     /**
168      * @param jmxAdaptorUrl The jmxAdaptorUrl to set.
169      */
170     public void setJmxAdaptorUrl(String jmxAdaptorUrl)
171     {
172         this.jmxAdaptorUrl = jmxAdaptorUrl;
173     }
174 
175 
176     public String getHost()
177     {
178         return host;
179     }
180 
181     public void setHost(String host)
182     {
183         this.host = host;
184     }
185 
186     public String getPort()
187     {
188         return port;
189     }
190 
191     public void setPort(String port)
192     {
193         this.port = port;
194     }
195 }