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