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