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