View Javadoc

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  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      private String name = "Profiler Agent";
45      private MBeanServer mBeanServer;
46      private ObjectName profilerName;
47  
48      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
49      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
50  
51      /**
52       * Logger used by this class
53       */
54      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          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          this.name = name;
74      }
75  
76      /*
77       * (non-Javadoc)
78       *
79       * @see org.mule.umo.manager.UMOAgent#getDescription()
80       */
81      public String getDescription()
82      {
83          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          if(!isApiAvailable())
94          {
95              logger.warn("Cannot find YourKit API. Profiler JMX Agent will be unregistered.");
96              unregisterMeQuietly();
97              return;
98          }
99  
100         final List servers = MBeanServerFactory.findMBeanServer(null);
101         if(servers.isEmpty())
102         {
103             throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
104         }
105 
106         try
107         {
108             mBeanServer = (MBeanServer) servers.get(0);
109 
110             profilerName = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":" + PROFILER_OBJECT_NAME);
111 
112             // unregister existing YourKit MBean first if required
113             unregisterMBeansIfNecessary();
114             mBeanServer.registerMBean(new YourKitProfilerService(), profilerName);
115         }
116         catch(Exception e)
117         {
118             throw new InitialisationException(CoreMessages.failedToStart(this.getName()), e, this);
119         }
120     }
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         if(mBeanServer == null || profilerName == null)
129         {
130             return;
131         }
132         if(mBeanServer.isRegistered(profilerName))
133         {
134             mBeanServer.unregisterMBean(profilerName);
135         }
136     }
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             MuleManager.getInstance().unregisterAgent(this.getName());
147         }
148         catch (UMOException e)
149         {
150             // not interested, really
151         }
152     }
153 
154     private boolean isApiAvailable()
155     {
156         try{
157             ClassUtils.getClass("com.yourkit.api.Controller");
158             return true;
159         }
160         catch(ClassNotFoundException e)
161         {
162             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     }
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     }
185 
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.mule.umo.lifecycle.Disposable#dispose()
190      */
191     public void dispose()
192     {
193         try
194         {
195             unregisterMBeansIfNecessary();
196         }
197         catch (Exception e)
198         {
199             logger.error("Couldn't unregister MBean: "
200                          + (profilerName != null ? profilerName.getCanonicalName() : "null"), e);
201         }
202     }
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     }
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     }
223 
224 }