1
2
3
4
5
6
7
8
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
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
53
54 protected static final Log logger = LogFactory.getLog(YourKitProfilerAgent.class);
55
56
57
58
59
60
61 public String getName()
62 {
63 return this.name;
64 }
65
66
67
68
69
70
71 public void setName(String name)
72 {
73 this.name = name;
74 }
75
76
77
78
79
80
81 public String getDescription()
82 {
83 return "Profiler JMX Agent";
84 }
85
86
87
88
89
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
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
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
140
141 protected void unregisterMeQuietly()
142 {
143 try
144 {
145
146 MuleManager.getInstance().unregisterAgent(this.getName());
147 }
148 catch (UMOException e)
149 {
150
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
168
169
170
171 public void start() throws UMOException
172 {
173
174 }
175
176
177
178
179
180
181 public void stop() throws UMOException
182 {
183
184 }
185
186
187
188
189
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
206
207
208
209 public void registered()
210 {
211
212 }
213
214
215
216
217
218
219 public void unregistered()
220 {
221
222 }
223
224 }