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