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