View Javadoc

1   /*
2    * $Id: JdmkAgent.java 22391 2011-07-12 12:00:48Z dirk.olmes $
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      @Override
67      public String getDescription()
68      {
69          return "Jdmk Http adaptor: " + jmxAdaptorUrl;
70      }
71  
72      @Override
73      public void start() throws MuleException
74      {
75          try
76          {
77              mBeanServer.invoke(adaptorName, "start", null, null);
78          }
79          catch (InstanceNotFoundException e)
80          {
81              throw new JmxManagementException(
82                  CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
83          }
84          catch (MBeanException e)
85          {
86              throw new JmxManagementException(
87                  CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
88          }
89          catch (ReflectionException e)
90          {
91              // ignore
92          }
93      }
94  
95      @Override
96      public void stop() throws MuleException
97      {
98          if (mBeanServer == null)
99          {
100             return;
101         }
102         
103         try
104         {
105             mBeanServer.invoke(adaptorName, "stop", null, null);
106         }
107         catch (InstanceNotFoundException e)
108         {
109             throw new JmxManagementException(
110                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
111         }
112         catch (MBeanException e)
113         {
114             throw new JmxManagementException(
115                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
116         }
117         catch (ReflectionException e)
118         {
119             // ignore
120         }
121     }
122 
123     @Override
124     public void dispose()
125     {
126         try
127         {
128             stop();
129         }
130         catch (Exception e)
131         {
132             // TODO: log an exception
133         }
134     }
135 
136     @Override
137     public void initialise() throws InitialisationException
138     {
139         try
140         {
141             mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
142             final Object adaptor = createAdaptor();
143             if (StringUtils.isBlank(jmxAdaptorUrl))
144             {
145                 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
146                 {
147                     jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port;
148                 }
149                 else
150                 {
151                     jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL;
152                 }
153             }
154             // TODO use Jmx support classes
155             adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName());
156             mBeanServer.registerMBean(adaptor, adaptorName);
157         }
158         catch (Exception e)
159         {
160             throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this);
161         }
162     }
163 
164     /**
165      * @return Returns the jmxAdaptorUrl.
166      */
167     public String getJmxAdaptorUrl()
168     {
169         return jmxAdaptorUrl;
170     }
171 
172     /**
173      * @param jmxAdaptorUrl The jmxAdaptorUrl to set.
174      */
175     public void setJmxAdaptorUrl(String jmxAdaptorUrl)
176     {
177         this.jmxAdaptorUrl = jmxAdaptorUrl;
178     }
179 
180 
181     public String getHost()
182     {
183         return host;
184     }
185 
186     public void setHost(String host)
187     {
188         this.host = host;
189     }
190 
191     public String getPort()
192     {
193         return port;
194     }
195 
196     public void setPort(String port)
197     {
198         this.port = port;
199     }
200 }