View Javadoc

1   /*
2    * $Id: JmxAgentConfigurer.java 19191 2010-08-25 21:05:23Z tcarlson $
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  package org.mule.module.management.agent;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.registry.RegistrationException;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.management.MBeanServer;
24  import javax.management.remote.rmi.RMIConnectorServer;
25  
26  /**
27   * Mule now binds to a platform mbeanserver by default and jmx agent is always registered via a
28   * bootstrap process. Thus a namespace handler creates this configurer class instead which propagates
29   * user settings to a jmx agent in the registry (instead of trying to register a duplicate jmx agent).
30   */
31  public class JmxAgentConfigurer implements MuleContextAware
32  {
33  
34      // populated with values below in a static initializer
35      public static final Map DEFAULT_CONNECTOR_SERVER_PROPERTIES;
36  
37      /**
38       * Logger used by this class
39       */
40      protected static final Log logger = LogFactory.getLog(JmxAgentConfigurer.class);
41  
42      protected MuleContext muleContext;
43  
44      /**
45       * Should MBeanServer be discovered.
46       */
47      protected boolean locateServer = true;
48      // don't create mbean server by default, use a platform mbean server
49      private boolean createServer = false;
50      private String connectorServerUrl;
51      private MBeanServer mBeanServer;
52      private Map connectorServerProperties = null;
53      private boolean enableStatistics = true;
54      private boolean createRmiRegistry = true;
55  
56      /**
57       * Username/password combinations for JMX Remoting authentication.
58       */
59      private Map credentials = new HashMap();
60  
61      static
62      {
63          Map props = new HashMap(1);
64          props.put(RMIConnectorServer.JNDI_REBIND_ATTRIBUTE, "true");
65          DEFAULT_CONNECTOR_SERVER_PROPERTIES = Collections.unmodifiableMap(props);
66      }
67  
68      public JmxAgentConfigurer()
69      {
70          connectorServerProperties = new HashMap(DEFAULT_CONNECTOR_SERVER_PROPERTIES);
71      }
72  
73      /**
74       * @return Returns the createServer.
75       */
76      public boolean isCreateServer()
77      {
78          return createServer;
79      }
80  
81      /**
82       * @param createServer The createServer to set.
83       */
84      public void setCreateServer(boolean createServer)
85      {
86          this.createServer = createServer;
87      }
88  
89      /**
90       * @return Returns the locateServer.
91       */
92      public boolean isLocateServer()
93      {
94          return locateServer;
95      }
96  
97      /**
98       * @param locateServer The locateServer to set.
99       */
100     public void setLocateServer(boolean locateServer)
101     {
102         this.locateServer = locateServer;
103     }
104 
105     /**
106      * @return Returns the connectorServerUrl.
107      */
108     public String getConnectorServerUrl()
109     {
110         return connectorServerUrl;
111     }
112 
113     /**
114      * @param connectorServerUrl The connectorServerUrl to set.
115      */
116     public void setConnectorServerUrl(String connectorServerUrl)
117     {
118         this.connectorServerUrl = connectorServerUrl;
119     }
120 
121     /**
122      * @return Returns the enableStatistics.
123      */
124     public boolean isEnableStatistics()
125     {
126         return enableStatistics;
127     }
128 
129     /**
130      * @param enableStatistics The enableStatistics to set.
131      */
132     public void setEnableStatistics(boolean enableStatistics)
133     {
134         this.enableStatistics = enableStatistics;
135     }
136 
137     /**
138      * @return Returns the mBeanServer.
139      */
140     public MBeanServer getMBeanServer()
141     {
142         return mBeanServer;
143     }
144 
145     /**
146      * @param mBeanServer The mBeanServer to set.
147      */
148     public void setMBeanServer(MBeanServer mBeanServer)
149     {
150         this.mBeanServer = mBeanServer;
151     }
152 
153     /**
154      * Getter for property 'connectorServerProperties'.
155      *
156      * @return Value for property 'connectorServerProperties'.
157      */
158     public Map getConnectorServerProperties()
159     {
160         return connectorServerProperties;
161     }
162 
163     /**
164      * Setter for property 'connectorServerProperties'. Set to {@code null} to use defaults ({@link
165      * #DEFAULT_CONNECTOR_SERVER_PROPERTIES}). Pass in an empty map to use no parameters. Passing a non-empty map will
166      * replace defaults.
167      *
168      * @param connectorServerProperties Value to set for property 'connectorServerProperties'.
169      */
170     public void setConnectorServerProperties(Map connectorServerProperties)
171     {
172         this.connectorServerProperties = connectorServerProperties;
173     }
174 
175 
176     /**
177      * Setter for property 'credentials'.
178      *
179      * @param newCredentials Value to set for property 'credentials'.
180      */
181     public void setCredentials(final Map newCredentials)
182     {
183         this.credentials.clear();
184         if (newCredentials != null && !newCredentials.isEmpty())
185         {
186             this.credentials.putAll(newCredentials);
187         }
188     }
189 
190     public boolean isCreateRmiRegistry()
191     {
192         return createRmiRegistry;
193     }
194 
195     public void setCreateRmiRegistry(boolean createRmiRegistry)
196     {
197         this.createRmiRegistry = createRmiRegistry;
198     }
199 
200     public void setMuleContext(MuleContext context)
201     {
202         this.muleContext = context;
203         try
204         {
205             // by the time mule context is injected, other attributes will have been set already
206             JmxAgent agent = muleContext.getRegistry().lookupObject(JmxAgent.class);
207             // in case it is injected, otherwise will follow the init logic
208             if (getMBeanServer() != null)
209             {
210                 agent.setMBeanServer(getMBeanServer());
211             }
212             if (getConnectorServerUrl() != null)
213             {
214                 agent.setConnectorServerUrl(getConnectorServerUrl());
215             }
216             if (getConnectorServerProperties() != null && !getConnectorServerProperties().isEmpty())
217             {
218                 agent.setConnectorServerProperties(getConnectorServerProperties());
219             }
220             // these can be copied as is
221             agent.setCreateServer(isCreateServer());
222             agent.setLocateServer(isLocateServer());
223             agent.setEnableStatistics(isEnableStatistics());
224             agent.setCreateRmiRegistry(isCreateRmiRegistry());
225         }
226         catch (RegistrationException e)
227         {
228             throw new RuntimeException(e);
229         }
230     }
231 
232     public void setName(String name)
233     {
234         // ignore the name, spring wants it
235     }
236 }