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