View Javadoc

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