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