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