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.umo.UMOException; |
15 | |
import org.mule.umo.lifecycle.InitialisationException; |
16 | |
import org.mule.umo.manager.UMOAgent; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.rmi.server.RMIClientSocketFactory; |
20 | |
import java.text.MessageFormat; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.management.remote.rmi.RMIConnectorServer; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 2 | public class DefaultJmxSupportAgent implements UMOAgent |
30 | |
{ |
31 | |
|
32 | |
public static final String DEFAULT_HOST = "localhost"; |
33 | |
public static final String DEFAULT_PORT = "1099"; |
34 | |
|
35 | 2 | private String name = "Default Jmx"; |
36 | 2 | private boolean loadJdmkAgent = false; |
37 | 2 | private boolean loadMx4jAgent = false; |
38 | 2 | private boolean loadProfilerAgent = false; |
39 | |
private String port; |
40 | |
private String host; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 2 | private Map credentials = new HashMap(); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public String getName() |
54 | |
{ |
55 | 0 | return name; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public void setName(String name) |
64 | |
{ |
65 | 0 | this.name = name; |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public String getDescription() |
74 | |
{ |
75 | 0 | return "Default Jmx Agent Support"; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
public void registered() |
80 | |
{ |
81 | |
|
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
public void unregistered() |
86 | |
{ |
87 | |
|
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
public void start() throws UMOException |
92 | |
{ |
93 | |
|
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
public void stop() throws UMOException |
98 | |
{ |
99 | |
|
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void dispose() |
108 | |
{ |
109 | |
|
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public void initialise() throws InitialisationException { |
126 | |
|
127 | |
try |
128 | |
{ |
129 | 0 | UMOAgent agent = createRmiAgent(); |
130 | 0 | if (!isAgentRegistered(agent)) |
131 | |
{ |
132 | 0 | MuleManager.getInstance().registerAgent(agent); |
133 | |
} |
134 | 0 | agent = createJmxAgent(); |
135 | 0 | if (!isAgentRegistered(agent)) |
136 | |
{ |
137 | 0 | MuleManager.getInstance().registerAgent(agent); |
138 | |
} |
139 | 0 | agent = createLog4jAgent(); |
140 | 0 | if (!isAgentRegistered(agent)) |
141 | |
{ |
142 | 0 | MuleManager.getInstance().registerAgent(agent); |
143 | |
} |
144 | 0 | agent = createJmxNotificationAgent(); |
145 | 0 | if (!isAgentRegistered(agent)) |
146 | |
{ |
147 | 0 | MuleManager.getInstance().registerAgent(agent); |
148 | |
} |
149 | 0 | if (loadJdmkAgent) |
150 | |
{ |
151 | 0 | agent = createJdmkAgent(); |
152 | 0 | if (!isAgentRegistered(agent)) |
153 | |
{ |
154 | 0 | MuleManager.getInstance().registerAgent(agent); |
155 | |
} |
156 | |
} |
157 | |
|
158 | 0 | if (loadMx4jAgent) |
159 | |
{ |
160 | 0 | agent = createMx4jAgent(); |
161 | 0 | if (!isAgentRegistered(agent)) |
162 | |
{ |
163 | 0 | MuleManager.getInstance().registerAgent(agent); |
164 | |
} |
165 | |
} |
166 | |
|
167 | 0 | if (loadProfilerAgent) |
168 | |
{ |
169 | 0 | agent = createProfilerAgent(); |
170 | 0 | if (!isAgentRegistered(agent)) |
171 | |
{ |
172 | 0 | MuleManager.getInstance().registerAgent(agent); |
173 | |
} |
174 | |
} |
175 | |
|
176 | |
|
177 | 0 | MuleManager.getInstance().unregisterAgent(name); |
178 | |
} |
179 | 0 | catch (UMOException e) |
180 | |
{ |
181 | 0 | throw new InitialisationException(e, this); |
182 | 0 | } |
183 | 0 | } |
184 | |
|
185 | |
protected JmxAgent createJmxAgent() |
186 | |
{ |
187 | 2 | JmxAgent agent = new JmxAgent(); |
188 | 2 | String remotingUri = null; |
189 | 2 | if (StringUtils.isBlank(host) && StringUtils.isBlank(port)) |
190 | |
{ |
191 | 0 | remotingUri = JmxAgent.DEFAULT_REMOTING_URI; |
192 | |
} |
193 | 2 | else if (StringUtils.isNotBlank(host)) |
194 | |
{ |
195 | |
|
196 | |
|
197 | 2 | Map props = agent.getConnectorServerProperties(); |
198 | 2 | Map mergedProps = new HashMap(props.size() + 1); |
199 | 2 | mergedProps.putAll(props); |
200 | 2 | RMIClientSocketFactory factory = new FixedHostRmiClientSocketFactory(host); |
201 | 2 | mergedProps.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, |
202 | |
factory); |
203 | 2 | agent.setConnectorServerProperties(mergedProps); |
204 | |
} |
205 | |
|
206 | |
|
207 | 2 | if (StringUtils.isBlank(remotingUri)) |
208 | |
{ |
209 | 2 | remotingUri = |
210 | |
MessageFormat.format("service:jmx:rmi:///jndi/rmi://{0}:{1}/server", |
211 | |
new Object[] {StringUtils.defaultString(host, DEFAULT_HOST), |
212 | |
StringUtils.defaultString(port, DEFAULT_PORT)}); |
213 | |
} |
214 | |
|
215 | 2 | if (credentials != null && !credentials.isEmpty()) |
216 | |
{ |
217 | 0 | agent.setCredentials(credentials); |
218 | |
} |
219 | 2 | agent.setConnectorServerUrl(remotingUri); |
220 | 2 | return agent; |
221 | |
} |
222 | |
|
223 | |
protected Log4jAgent createLog4jAgent() |
224 | |
{ |
225 | 0 | return new Log4jAgent(); |
226 | |
} |
227 | |
|
228 | |
protected RmiRegistryAgent createRmiAgent() |
229 | |
{ |
230 | 0 | final RmiRegistryAgent agent = new RmiRegistryAgent(); |
231 | 0 | agent.setHost(StringUtils.defaultString(host, DEFAULT_HOST)); |
232 | 0 | agent.setPort(StringUtils.defaultString(port, DEFAULT_PORT)); |
233 | 0 | return agent; |
234 | |
} |
235 | |
|
236 | |
protected JmxServerNotificationAgent createJmxNotificationAgent() |
237 | |
{ |
238 | 0 | return new JmxServerNotificationAgent(); |
239 | |
} |
240 | |
|
241 | |
protected Mx4jAgent createMx4jAgent() |
242 | |
{ |
243 | 0 | return new Mx4jAgent(); |
244 | |
} |
245 | |
|
246 | |
protected JdmkAgent createJdmkAgent() |
247 | |
{ |
248 | 0 | return new JdmkAgent(); |
249 | |
} |
250 | |
|
251 | |
protected YourKitProfilerAgent createProfilerAgent() |
252 | |
{ |
253 | 0 | return new YourKitProfilerAgent(); |
254 | |
} |
255 | |
|
256 | |
protected boolean isAgentRegistered(UMOAgent agent) |
257 | |
{ |
258 | 0 | return MuleManager.getInstance().lookupAgent(agent.getName()) != null; |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public boolean isLoadJdmkAgent() |
267 | |
{ |
268 | 0 | return loadJdmkAgent; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public void setLoadJdmkAgent(boolean loadJdmkAgent) |
277 | |
{ |
278 | 0 | this.loadJdmkAgent = loadJdmkAgent; |
279 | 0 | } |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public boolean isLoadMx4jAgent() |
287 | |
{ |
288 | 0 | return loadMx4jAgent; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public void setLoadMx4jAgent(boolean loadMx4jAgent) |
297 | |
{ |
298 | 0 | this.loadMx4jAgent = loadMx4jAgent; |
299 | 0 | } |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public boolean isLoadProfilerAgent() |
306 | |
{ |
307 | 0 | return loadProfilerAgent; |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
public void setLoadProfilerAgent(boolean loadProfilerAgent) |
315 | |
{ |
316 | 0 | this.loadProfilerAgent = loadProfilerAgent; |
317 | 0 | } |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
public String getPort() |
325 | |
{ |
326 | 0 | return port; |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void setPort(final String port) |
335 | |
{ |
336 | 0 | this.port = port; |
337 | 0 | } |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public String getHost() |
345 | |
{ |
346 | 0 | return host; |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
public void setHost(final String host) |
355 | |
{ |
356 | 2 | this.host = host; |
357 | 2 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
public void setCredentials(final Map credentials) |
366 | |
{ |
367 | 0 | this.credentials = credentials; |
368 | 0 | } |
369 | |
} |