View Javadoc

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