View Javadoc

1   /*
2    * $Id: DefaultJmxSupportAgent.java 20842 2010-12-29 15:00:39Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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       * Username/password combinations for JMX Remoting
50       * authentication.
51       */
52      private Map<String, String> credentials = new HashMap<String, String>();
53  
54      /**
55       * Should be a 1 line description of the agent
56       *
57       * @return agent description
58       */
59      @Override
60      public String getDescription()
61      {
62          return "Default Jmx Support Agent";
63      }
64  
65      /** {@inheritDoc} */
66      public void start() throws MuleException
67      {
68          // nothing to do
69      }
70  
71      /** {@inheritDoc} */
72      public void stop() throws MuleException
73      {
74          // nothing to do
75      }
76  
77      /**
78       * A lifecycle method where implementor should free up any resources. If an
79       * exception is thrown it should just be logged and processing should continue.
80       * This method should not throw Runtime exceptions.
81       */
82      public void dispose()
83      {
84          // nothing to do
85      }
86  
87      /**
88       * Method used to perform any initialisation work. If a fatal error occurs during
89       * initialisation an <code>InitialisationException</code> should be thrown,
90       * causing the Mule instance to shutdown. If the error is recoverable, say by
91       * retrying to connect, a <code>RecoverableException</code> should be thrown.
92       * There is no guarantee that by throwing a Recoverable exception that the Mule
93       * instance will not shut down.
94       *
95       * @throws org.mule.api.lifecycle.InitialisationException
96       *          if a fatal error occurs
97       *          causing the Mule instance to shutdown
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             // any existing jmx agent will be modified with remote connector settings
111             agent = createJmxAgent();
112             // there must be only one jmx agent, so lookup by type instead
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             // remove this agent once it has registered the other agents
161             //TODO RM* this currently does nothing!!!
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                 // nothing registered yet
179                 agent = new JmxAgent();
180             }
181         }
182         catch (RegistrationException e)
183         {
184             throw new RuntimeException(e);
185         }
186 
187         // otherwise, just augment an existing jmx agent with remote connector
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             // enable support for multi-NIC servers by configuring
197             // a custom RMIClientSocketFactory
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         // if defaults haven't been used
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 }