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