Coverage Report - org.mule.module.management.agent.JdmkAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
JdmkAgent
0%
0/49
0%
0/8
2.308
 
 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  0
         super("jdmk-agent");
 52  0
     }
 53  
 
 54  
     protected Object createAdaptor() throws Exception
 55  
     {
 56  0
         final URI uri = new URI(jmxAdaptorUrl);
 57  0
         final int port = uri.getPort();
 58  0
         return ClassUtils.instanciateClass(CLASSNAME_ADAPTER,
 59  
                                            new Object[] {new Integer(port)}, this.getClass());
 60  
     }
 61  
 
 62  
     public String getDescription()
 63  
     {
 64  0
         return "Jdmk Http adaptor: " + jmxAdaptorUrl;
 65  
     }
 66  
 
 67  
     public void start() throws MuleException
 68  
     {
 69  
         try
 70  
         {
 71  0
             mBeanServer.invoke(adaptorName, "start", null, null);
 72  
         }
 73  0
         catch (InstanceNotFoundException e)
 74  
         {
 75  0
             throw new JmxManagementException(
 76  
                 CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
 77  
         }
 78  0
         catch (MBeanException e)
 79  
         {
 80  0
             throw new JmxManagementException(
 81  
                 CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
 82  
         }
 83  0
         catch (ReflectionException e)
 84  
         {
 85  
             // ignore
 86  0
         }
 87  0
     }
 88  
 
 89  
     public void stop() throws MuleException
 90  
     {
 91  0
         if (mBeanServer == null)
 92  
         {
 93  0
             return;
 94  
         }
 95  
         
 96  
         try
 97  
         {
 98  0
             mBeanServer.invoke(adaptorName, "stop", null, null);
 99  
         }
 100  0
         catch (InstanceNotFoundException e)
 101  
         {
 102  0
             throw new JmxManagementException(
 103  
                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
 104  
         }
 105  0
         catch (MBeanException e)
 106  
         {
 107  0
             throw new JmxManagementException(
 108  
                 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
 109  
         }
 110  0
         catch (ReflectionException e)
 111  
         {
 112  
             // ignore
 113  0
         }
 114  0
     }
 115  
 
 116  
     public void dispose()
 117  
     {
 118  
         try
 119  
         {
 120  0
             stop();
 121  
         }
 122  0
         catch (Exception e)
 123  
         {
 124  
             // TODO: log an exception
 125  0
         }
 126  0
     }
 127  
 
 128  
     public void initialise() throws InitialisationException
 129  
     {
 130  
         try
 131  
         {
 132  0
             mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
 133  0
             final Object adaptor = createAdaptor();
 134  0
             if (StringUtils.isBlank(jmxAdaptorUrl))
 135  
             {
 136  0
                 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
 137  
                 {
 138  0
                     jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port;
 139  
                 }
 140  
                 else
 141  
                 {
 142  0
                     jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL;
 143  
                 }
 144  
             }
 145  
             // TODO use Jmx support classes
 146  0
             adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName());
 147  0
             mBeanServer.registerMBean(adaptor, adaptorName);
 148  
         }
 149  0
         catch (Exception e)
 150  
         {
 151  0
             throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this);
 152  0
         }
 153  0
     }
 154  
 
 155  
     /**
 156  
      * @return Returns the jmxAdaptorUrl.
 157  
      */
 158  
     public String getJmxAdaptorUrl()
 159  
     {
 160  0
         return jmxAdaptorUrl;
 161  
     }
 162  
 
 163  
     /**
 164  
      * @param jmxAdaptorUrl The jmxAdaptorUrl to set.
 165  
      */
 166  
     public void setJmxAdaptorUrl(String jmxAdaptorUrl)
 167  
     {
 168  0
         this.jmxAdaptorUrl = jmxAdaptorUrl;
 169  0
     }
 170  
 
 171  
 
 172  
     public String getHost()
 173  
     {
 174  0
         return host;
 175  
     }
 176  
 
 177  
     public void setHost(String host)
 178  
     {
 179  0
         this.host = host;
 180  0
     }
 181  
 
 182  
     public String getPort()
 183  
     {
 184  0
         return port;
 185  
     }
 186  
 
 187  
     public void setPort(String port)
 188  
     {
 189  0
         this.port = port;
 190  0
     }
 191  
 }