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