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 | 0 | public class YourKitProfilerAgent implements UMOAgent |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | |
public static final String PROFILER_OBJECT_NAME = "name=Profiler"; |
43 | |
|
44 | 0 | private String name = "Profiler Agent"; |
45 | |
private MBeanServer mBeanServer; |
46 | |
private ObjectName profilerName; |
47 | |
|
48 | 0 | private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance(); |
49 | 0 | private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport(); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | protected static final Log logger = LogFactory.getLog(YourKitProfilerAgent.class); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public String getName() |
62 | |
{ |
63 | 0 | return this.name; |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void setName(String name) |
72 | |
{ |
73 | 0 | this.name = name; |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public String getDescription() |
82 | |
{ |
83 | 0 | return "Profiler JMX Agent"; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void initialise() throws InitialisationException |
92 | |
{ |
93 | 0 | if(!isApiAvailable()) |
94 | |
{ |
95 | 0 | logger.warn("Cannot find YourKit API. Profiler JMX Agent will be unregistered."); |
96 | 0 | unregisterMeQuietly(); |
97 | 0 | return; |
98 | |
} |
99 | |
|
100 | 0 | final List servers = MBeanServerFactory.findMBeanServer(null); |
101 | 0 | if(servers.isEmpty()) |
102 | |
{ |
103 | 0 | throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this); |
104 | |
} |
105 | |
|
106 | |
try |
107 | |
{ |
108 | 0 | mBeanServer = (MBeanServer) servers.get(0); |
109 | |
|
110 | 0 | profilerName = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":" + PROFILER_OBJECT_NAME); |
111 | |
|
112 | |
|
113 | 0 | unregisterMBeansIfNecessary(); |
114 | 0 | mBeanServer.registerMBean(new YourKitProfilerService(), profilerName); |
115 | |
} |
116 | 0 | catch(Exception e) |
117 | |
{ |
118 | 0 | throw new InitialisationException(CoreMessages.failedToStart(this.getName()), e, this); |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected void unregisterMBeansIfNecessary() |
126 | |
throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException |
127 | |
{ |
128 | 0 | if(mBeanServer == null || profilerName == null) |
129 | |
{ |
130 | 0 | return; |
131 | |
} |
132 | 0 | if(mBeanServer.isRegistered(profilerName)) |
133 | |
{ |
134 | 0 | mBeanServer.unregisterMBean(profilerName); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
protected void unregisterMeQuietly() |
142 | |
{ |
143 | |
try |
144 | |
{ |
145 | |
|
146 | 0 | MuleManager.getInstance().unregisterAgent(this.getName()); |
147 | |
} |
148 | 0 | catch (UMOException e) |
149 | |
{ |
150 | |
|
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
private boolean isApiAvailable() |
155 | |
{ |
156 | |
try{ |
157 | 0 | ClassUtils.getClass("com.yourkit.api.Controller"); |
158 | 0 | return true; |
159 | |
} |
160 | 0 | catch(ClassNotFoundException e) |
161 | |
{ |
162 | 0 | return false; |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void start() throws UMOException |
172 | |
{ |
173 | |
|
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public void stop() throws UMOException |
182 | |
{ |
183 | |
|
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public void dispose() |
192 | |
{ |
193 | |
try |
194 | |
{ |
195 | 0 | unregisterMBeansIfNecessary(); |
196 | |
} |
197 | 0 | catch (Exception e) |
198 | |
{ |
199 | 0 | logger.error("Couldn't unregister MBean: " |
200 | |
+ (profilerName != null ? profilerName.getCanonicalName() : "null"), e); |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void registered() |
210 | |
{ |
211 | |
|
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public void unregistered() |
220 | |
{ |
221 | |
|
222 | 0 | } |
223 | |
|
224 | |
} |