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