Coverage Report - org.mule.module.management.agent.YourKitProfilerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
YourKitProfilerAgent
0%
0/48
0%
0/12
2
 
 1  
 /*
 2  
  * $Id: YourKitProfilerAgent.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.MuleServer;
 14  
 import org.mule.RegistryContext;
 15  
 import org.mule.api.MuleContext;
 16  
 import org.mule.api.MuleException;
 17  
 import org.mule.api.agent.Agent;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.module.management.i18n.ManagementMessages;
 21  
 import org.mule.module.management.mbean.YourKitProfilerService;
 22  
 import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
 23  
 import org.mule.module.management.support.JmxSupport;
 24  
 import org.mule.module.management.support.JmxSupportFactory;
 25  
 import org.mule.util.ClassUtils;
 26  
 
 27  
 import java.util.Collections;
 28  
 import java.util.List;
 29  
 
 30  
 import javax.management.InstanceNotFoundException;
 31  
 import javax.management.MBeanRegistrationException;
 32  
 import javax.management.MBeanServer;
 33  
 import javax.management.MBeanServerFactory;
 34  
 import javax.management.MalformedObjectNameException;
 35  
 import javax.management.ObjectName;
 36  
 
 37  
 import org.apache.commons.logging.Log;
 38  
 import org.apache.commons.logging.LogFactory;
 39  
 
 40  0
 public class YourKitProfilerAgent implements Agent
 41  
 {
 42  
     /**
 43  
      * MBean name to register under.
 44  
      */
 45  
     public static final String PROFILER_OBJECT_NAME = "name=Profiler";
 46  
 
 47  0
     private String name = "yourkit-profiler";
 48  
     private MBeanServer mBeanServer;
 49  
     private ObjectName profilerName;
 50  
 
 51  0
     private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
 52  0
     private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
 53  
 
 54  
     /**
 55  
      * Logger used by this class
 56  
      */
 57  0
     protected static final Log logger = LogFactory.getLog(YourKitProfilerAgent.class);
 58  
 
 59  
     /*
 60  
     * (non-Javadoc)
 61  
     *
 62  
     * @see org.mule.api.context.Agent#getName()
 63  
     */
 64  
     public String getName()
 65  
     {
 66  0
         return this.name;
 67  
     }
 68  
 
 69  
     /*
 70  
      * (non-Javadoc)
 71  
      *
 72  
      * @see org.mule.api.context.Agent#setName(java.lang.String)
 73  
      */
 74  
     public void setName(String name)
 75  
     {
 76  0
         this.name = name;
 77  0
     }
 78  
 
 79  
     /*
 80  
      * (non-Javadoc)
 81  
      *
 82  
      * @see org.mule.api.context.Agent#getDescription()
 83  
      */
 84  
     public String getDescription()
 85  
     {
 86  0
         return "Profiler JMX Agent";
 87  
     }
 88  
 
 89  
     public List getDependentAgents()
 90  
     {
 91  0
         return Collections.EMPTY_LIST;
 92  
     }
 93  
 
 94  
     public void initialise() throws InitialisationException
 95  
     {
 96  0
         if(!isApiAvailable())
 97  
         {
 98  0
             logger.warn("Cannot find YourKit API. Profiler JMX Agent will be unregistered.");
 99  0
             unregisterMeQuietly();
 100  0
             return;
 101  
         }
 102  
 
 103  0
         final List servers = MBeanServerFactory.findMBeanServer(null);
 104  0
         if(servers.isEmpty())
 105  
         {
 106  0
             throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
 107  
         }
 108  
 
 109  
         try
 110  
         {
 111  0
             mBeanServer = (MBeanServer) servers.get(0);
 112  
 
 113  0
             MuleContext muleContext = MuleServer.getMuleContext();
 114  0
             profilerName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + PROFILER_OBJECT_NAME);
 115  
 
 116  
             // unregister existing YourKit MBean first if required
 117  0
             unregisterMBeansIfNecessary();
 118  0
             mBeanServer.registerMBean(new YourKitProfilerService(), profilerName);
 119  
         }
 120  0
         catch(Exception e)
 121  
         {
 122  0
             throw new InitialisationException(CoreMessages.failedToStart(this.getName()), e, this);
 123  0
         }
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Unregister Profiler MBean if there are any left over the old deployment
 128  
      */
 129  
     protected void unregisterMBeansIfNecessary()
 130  
             throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
 131  
     {
 132  0
         if(mBeanServer == null || profilerName == null)
 133  
         {
 134  0
             return;
 135  
         }
 136  0
         if(mBeanServer.isRegistered(profilerName))
 137  
         {
 138  0
             mBeanServer.unregisterMBean(profilerName);
 139  
         }
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Quietly unregister ourselves.
 144  
      */
 145  
     protected void unregisterMeQuietly()
 146  
     {
 147  
         try
 148  
         {
 149  
             // remove the agent from the list, it's not functional
 150  0
             RegistryContext.getRegistry().unregisterAgent(this.getName());
 151  
         }
 152  0
         catch (MuleException e)
 153  
         {
 154  
             // not interested, really
 155  0
         }
 156  0
     }
 157  
 
 158  
     private boolean isApiAvailable()
 159  
     {
 160  
         try{
 161  0
             ClassUtils.getClass("com.yourkit.api.Controller");
 162  0
             return true;
 163  
         }
 164  0
         catch(ClassNotFoundException e)
 165  
         {
 166  0
             return false;
 167  
         }
 168  
     }
 169  
 
 170  
     public void start() throws MuleException
 171  
     {
 172  
         // nothing to do
 173  0
     }
 174  
 
 175  
     public void stop() throws MuleException
 176  
     {
 177  
         // nothing to do
 178  0
     }
 179  
 
 180  
     /*
 181  
      * (non-Javadoc)
 182  
      *
 183  
      * @see org.mule.api.lifecycle.Disposable#dispose()
 184  
      */
 185  
     public void dispose()
 186  
     {
 187  
         try
 188  
         {
 189  0
             unregisterMBeansIfNecessary();
 190  
         }
 191  0
         catch (Exception e)
 192  
         {
 193  0
             logger.error("Couldn't unregister MBean: "
 194  
                          + (profilerName != null ? profilerName.getCanonicalName() : "null"), e);
 195  0
         }
 196  0
     }
 197  
 
 198  
     /*
 199  
      * (non-Javadoc)
 200  
      *
 201  
      * @see org.mule.api.context.Agent#registered()
 202  
      */
 203  
     public void registered()
 204  
     {
 205  
         // nothing to do
 206  0
     }
 207  
 
 208  
     /*
 209  
      * (non-Javadoc)
 210  
      *
 211  
      * @see org.mule.api.context.Agent#unregistered()
 212  
      */
 213  
     public void unregistered()
 214  
     {
 215  
         // nothing to do
 216  0
     }
 217  
 
 218  
 }