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