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