View Javadoc

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