View Javadoc

1   /*
2    * $Id: DefaultJmxSupportAgent.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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      private boolean loadJdmkAgent = false;
35      private boolean loadMx4jAgent = false;
36      private boolean loadProfilerAgent = false;
37      private String port;
38      private String host;
39  
40      public DefaultJmxSupportAgent()
41      {
42          super("jmx-default-config");
43      }
44  
45      /**
46       * Username/password combinations for JMX Remoting
47       * authentication.
48       */
49      private Map credentials = new HashMap();
50  
51  
52      /**
53       * Should be a 1 line description of the agent
54       *
55       * @return agent description
56       */
57      public String getDescription()
58      {
59          return "Default Jmx Agent Support";
60      }
61  
62      /** {@inheritDoc} */
63      public void registered()
64      {
65          // nothing to do
66      }
67  
68      /** {@inheritDoc} */
69      public void unregistered()
70      {
71          // nothing to do
72      }
73  
74      /** {@inheritDoc} */
75      public void start() throws MuleException
76      {
77          // nothing to do
78      }
79  
80      /** {@inheritDoc} */
81      public void stop() throws MuleException
82      {
83          // nothing to do
84      }
85  
86      /**
87       * A lifecycle method where implementor should free up any resources. If an
88       * exception is thrown it should just be logged and processing should continue.
89       * This method should not throw Runtime exceptions.
90       */
91      public void dispose()
92      {
93          // nothing to do
94      }
95  
96      /**
97       * Method used to perform any initialisation work. If a fatal error occurs during
98       * initialisation an <code>InitialisationException</code> should be thrown,
99       * causing the Mule instance to shutdown. If the error is recoverable, say by
100      * retrying to connect, a <code>RecoverableException</code> should be thrown.
101      * There is no guarantee that by throwing a Recoverable exception that the Mule
102      * instance will not shut down.
103      *
104      * @throws org.mule.api.lifecycle.InitialisationException
105      *          if a fatal error occurs
106      *          causing the Mule instance to shutdown
107      */
108     public void initialise() throws InitialisationException
109     {
110         try
111         {
112             Agent agent = createRmiAgent();
113             if (!isAgentRegistered(agent))
114             {
115                 RegistryContext.getRegistry().registerAgent(agent);
116             }
117             agent = createJmxAgent();
118             if (!isAgentRegistered(agent))
119             {
120                 RegistryContext.getRegistry().registerAgent(agent);
121             }
122             agent = createLog4jAgent();
123             if (!isAgentRegistered(agent))
124             {
125                 RegistryContext.getRegistry().registerAgent(agent);
126             }
127             agent = createJmxNotificationAgent();
128             if (!isAgentRegistered(agent))
129             {
130                 RegistryContext.getRegistry().registerAgent(agent);
131             }
132             if (loadJdmkAgent)
133             {
134                 agent = createJdmkAgent();
135                 if (!isAgentRegistered(agent))
136                 {
137                     RegistryContext.getRegistry().registerAgent(agent);
138                 }
139             }
140 
141             if (loadMx4jAgent)
142             {
143                 agent = createMx4jAgent();
144                 if (!isAgentRegistered(agent))
145                 {
146                     RegistryContext.getRegistry().registerAgent(agent);
147                 }
148             }
149 
150             if (loadProfilerAgent)
151             {
152                 agent = createProfilerAgent();
153                 if (!isAgentRegistered(agent))
154                 {
155                     RegistryContext.getRegistry().registerAgent(agent);
156                 }
157             }
158 
159             // remove this agent once it has registered the other agents
160             //TODO RM* this currently does nothing!!!
161             muleContext.getRegistry().unregisterAgent(name);
162         }
163         catch (MuleException e)
164         {
165             throw new InitialisationException(e, this);
166         }
167     }
168 
169     public JmxAgent createJmxAgent()
170     {
171         JmxAgent agent = new JmxAgent();
172         String remotingUri = null;
173         if (StringUtils.isBlank(host) && StringUtils.isBlank(port))
174         {
175             remotingUri = JmxAgent.DEFAULT_REMOTING_URI;
176         }
177         else if (StringUtils.isNotBlank(host))
178         {
179             // enable support for multi-NIC servers by configuring
180             // a custom RMIClientSocketFactory
181             Map props = agent.getConnectorServerProperties();
182             Map mergedProps = new HashMap(props.size() + 1);
183             mergedProps.putAll(props);
184             RMIClientSocketFactory factory = new FixedHostRmiClientSocketFactory(host);
185             mergedProps.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
186                     factory);
187             agent.setConnectorServerProperties(mergedProps);
188         }
189 
190         // if defaults haven't been used
191         if (StringUtils.isBlank(remotingUri))
192         {
193             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         if (credentials != null && !credentials.isEmpty())
200         {
201             agent.setCredentials(credentials);
202         }
203         agent.setConnectorServerUrl(remotingUri);
204         return agent;
205     }
206 
207     protected Log4jAgent createLog4jAgent()
208     {
209         return new Log4jAgent();
210     }
211 
212     protected RmiRegistryAgent createRmiAgent()
213     {
214         final RmiRegistryAgent agent = new RmiRegistryAgent();
215         agent.setHost(StringUtils.defaultString(host, DEFAULT_HOST));
216         agent.setPort(StringUtils.defaultString(port, DEFAULT_PORT));
217         return agent;
218     }
219 
220     protected JmxServerNotificationAgent createJmxNotificationAgent()
221     {
222         return new JmxServerNotificationAgent();
223     }
224 
225     protected Mx4jAgent createMx4jAgent()
226     {
227         return new Mx4jAgent();
228     }
229 
230     protected JdmkAgent createJdmkAgent()
231     {
232         return new JdmkAgent();
233     }
234 
235     protected YourKitProfilerAgent createProfilerAgent()
236     {
237         return new YourKitProfilerAgent();
238     }
239 
240     protected boolean isAgentRegistered(Agent agent)
241     {
242         return muleContext.getRegistry().lookupAgent(agent.getName()) != null;
243     }
244 
245     /**
246      * Getter for property 'loadJdmkAgent'.
247      *
248      * @return Value for property 'loadJdmkAgent'.
249      */
250     public boolean isLoadJdmkAgent()
251     {
252         return loadJdmkAgent;
253     }
254 
255     /**
256      * Setter for property 'loadJdmkAgent'.
257      *
258      * @param loadJdmkAgent Value to set for property 'loadJdmkAgent'.
259      */
260     public void setLoadJdmkAgent(boolean loadJdmkAgent)
261     {
262         this.loadJdmkAgent = loadJdmkAgent;
263     }
264 
265     /**
266      * Getter for property 'loadMx4jAgent'.
267      *
268      * @return Value for property 'loadMx4jAgent'.
269      */
270     public boolean isLoadMx4jAgent()
271     {
272         return loadMx4jAgent;
273     }
274 
275     /**
276      * Setter for property 'loadMx4jAgent'.
277      *
278      * @param loadMx4jAgent Value to set for property 'loadMx4jAgent'.
279      */
280     public void setLoadMx4jAgent(boolean loadMx4jAgent)
281     {
282         this.loadMx4jAgent = loadMx4jAgent;
283     }
284 
285     /**
286      * Getter for property 'loadProfilerAgent'.
287      * @return Value for property 'loadProfilerAgent'.
288      */
289     public boolean isLoadProfilerAgent()
290     {
291         return loadProfilerAgent;
292     }
293 
294     /**
295      * Setter for property 'loadProfilerAgent'.
296      * @param loadProfilerAgent Value to set for property 'loadProfilerAgent'.
297      */
298     public void setLoadProfilerAgent(boolean loadProfilerAgent)
299     {
300         this.loadProfilerAgent = loadProfilerAgent;
301     }
302 
303     /**
304      * Getter for property 'port'.
305      *
306      * @return Value for property 'port'.
307      */
308     public String getPort()
309     {
310         return port;
311     }
312 
313     /**
314      * Setter for property 'port'.
315      *
316      * @param port Value to set for property 'port'.
317      */
318     public void setPort(final String port)
319     {
320         this.port = port;
321     }
322 
323     /**
324      * Getter for property 'host'.
325      *
326      * @return Value for property 'host'.
327      */
328     public String getHost()
329     {
330         return host;
331     }
332 
333     /**
334      * Setter for property 'host'.
335      *
336      * @param host Value to set for property 'host'.
337      */
338     public void setHost(final String host)
339     {
340         this.host = host;
341     }
342 
343 
344     /**
345      * Setter for property 'credentials'.
346      *
347      * @param credentials Value to set for property 'credentials'.
348      */
349     public void setCredentials(final Map credentials)
350     {
351         this.credentials = credentials;
352     }
353 
354 }