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