View Javadoc

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  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      private String name = "yourkit-profiler";
48      private MBeanServer mBeanServer;
49      private ObjectName profilerName;
50  
51      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
52      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
53  
54      /**
55       * Logger used by this class
56       */
57      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          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          this.name = name;
77      }
78  
79      /*
80       * (non-Javadoc)
81       *
82       * @see org.mule.api.context.Agent#getDescription()
83       */
84      public String getDescription()
85      {
86          return "Profiler JMX Agent";
87      }
88  
89      public List getDependentAgents()
90      {
91          return Collections.EMPTY_LIST;
92      }
93  
94      public void initialise() throws InitialisationException
95      {
96          if(!isApiAvailable())
97          {
98              logger.warn("Cannot find YourKit API. Profiler JMX Agent will be unregistered.");
99              unregisterMeQuietly();
100             return;
101         }
102 
103         final List servers = MBeanServerFactory.findMBeanServer(null);
104         if(servers.isEmpty())
105         {
106             throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
107         }
108 
109         try
110         {
111             mBeanServer = (MBeanServer) servers.get(0);
112 
113             MuleContext muleContext = MuleServer.getMuleContext();
114             profilerName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + PROFILER_OBJECT_NAME);
115 
116             // unregister existing YourKit MBean first if required
117             unregisterMBeansIfNecessary();
118             mBeanServer.registerMBean(new YourKitProfilerService(), profilerName);
119         }
120         catch(Exception e)
121         {
122             throw new InitialisationException(CoreMessages.failedToStart(this.getName()), e, this);
123         }
124     }
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         if(mBeanServer == null || profilerName == null)
133         {
134             return;
135         }
136         if(mBeanServer.isRegistered(profilerName))
137         {
138             mBeanServer.unregisterMBean(profilerName);
139         }
140     }
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             RegistryContext.getRegistry().unregisterAgent(this.getName());
151         }
152         catch (MuleException e)
153         {
154             // not interested, really
155         }
156     }
157 
158     private boolean isApiAvailable()
159     {
160         try{
161             ClassUtils.getClass("com.yourkit.api.Controller");
162             return true;
163         }
164         catch(ClassNotFoundException e)
165         {
166             return false;
167         }
168     }
169 
170     public void start() throws MuleException
171     {
172         // nothing to do
173     }
174 
175     public void stop() throws MuleException
176     {
177         // nothing to do
178     }
179 
180     /*
181      * (non-Javadoc)
182      *
183      * @see org.mule.api.lifecycle.Disposable#dispose()
184      */
185     public void dispose()
186     {
187         try
188         {
189             unregisterMBeansIfNecessary();
190         }
191         catch (Exception e)
192         {
193             logger.error("Couldn't unregister MBean: "
194                          + (profilerName != null ? profilerName.getCanonicalName() : "null"), e);
195         }
196     }
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     }
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     }
217 
218 }