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