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