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