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