View Javadoc

1   /*
2    * $Id: JmxAgentConfigurer.java 21994 2011-05-27 05:00:35Z dirk.olmes $
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 java.util.Collections;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import javax.management.MBeanServer;
21  import javax.management.remote.rmi.RMIConnectorServer;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
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      // populated with values below in a static initializer
34      public static final Map<String, Object> DEFAULT_CONNECTOR_SERVER_PROPERTIES;
35  
36      /**
37       * Logger used by this class
38       */
39      protected static final Log logger = LogFactory.getLog(JmxAgentConfigurer.class);
40  
41      protected MuleContext muleContext;
42  
43      /**
44       * Should MBeanServer be discovered.
45       */
46      protected boolean locateServer = true;
47      // don't create mbean server by default, use a platform mbean server
48      private boolean createServer = false;
49      private String connectorServerUrl;
50      private MBeanServer mBeanServer;
51      private Map<String, Object> connectorServerProperties = null;
52      private boolean enableStatistics = true;
53      private boolean createRmiRegistry = true;
54  
55      /**
56       * Username/password combinations for JMX Remoting authentication.
57       */
58      private Map<String, String> credentials = new HashMap<String, String>();
59  
60      static
61      {
62          Map<String, Object> props = new HashMap<String, Object>(1);
63          props.put(RMIConnectorServer.JNDI_REBIND_ATTRIBUTE, "true");
64          DEFAULT_CONNECTOR_SERVER_PROPERTIES = Collections.unmodifiableMap(props);
65      }
66  
67      public JmxAgentConfigurer()
68      {
69          connectorServerProperties = new HashMap<String, Object>(DEFAULT_CONNECTOR_SERVER_PROPERTIES);
70      }
71  
72      public boolean isCreateServer()
73      {
74          return createServer;
75      }
76  
77      public void setCreateServer(boolean createServer)
78      {
79          this.createServer = createServer;
80      }
81  
82      public boolean isLocateServer()
83      {
84          return locateServer;
85      }
86  
87      public void setLocateServer(boolean locateServer)
88      {
89          this.locateServer = locateServer;
90      }
91  
92      public String getConnectorServerUrl()
93      {
94          return connectorServerUrl;
95      }
96  
97      public void setConnectorServerUrl(String connectorServerUrl)
98      {
99          this.connectorServerUrl = connectorServerUrl;
100     }
101 
102     public boolean isEnableStatistics()
103     {
104         return enableStatistics;
105     }
106 
107     public void setEnableStatistics(boolean enableStatistics)
108     {
109         this.enableStatistics = enableStatistics;
110     }
111 
112     public MBeanServer getMBeanServer()
113     {
114         return mBeanServer;
115     }
116 
117     public void setMBeanServer(MBeanServer mBeanServer)
118     {
119         this.mBeanServer = mBeanServer;
120     }
121 
122     public Map<String, Object> getConnectorServerProperties()
123     {
124         return connectorServerProperties;
125     }
126 
127     /**
128      * Setter for property 'connectorServerProperties'. Set to {@code null} to use
129      * defaults ({@link #DEFAULT_CONNECTOR_SERVER_PROPERTIES}). Pass in an empty map
130      * to use no parameters. Passing a non-empty map will replace defaults.
131      *
132      * @param connectorServerProperties Value to set for property
133      *            'connectorServerProperties'.
134      */
135     public void setConnectorServerProperties(Map<String, Object> connectorServerProperties)
136     {
137         this.connectorServerProperties = connectorServerProperties;
138     }
139 
140     public void setCredentials(final Map<String, String> newCredentials)
141     {
142         this.credentials.clear();
143         if (newCredentials != null && !newCredentials.isEmpty())
144         {
145             this.credentials.putAll(newCredentials);
146         }
147     }
148 
149     public boolean isCreateRmiRegistry()
150     {
151         return createRmiRegistry;
152     }
153 
154     public void setCreateRmiRegistry(boolean createRmiRegistry)
155     {
156         this.createRmiRegistry = createRmiRegistry;
157     }
158 
159     public void setMuleContext(MuleContext context)
160     {
161         this.muleContext = context;
162         try
163         {
164             // by the time mule context is injected, other attributes will have been set already
165             JmxAgent agent = muleContext.getRegistry().lookupObject(JmxAgent.class);
166             // in case it is injected, otherwise will follow the init logic
167             if (getMBeanServer() != null)
168             {
169                 agent.setMBeanServer(getMBeanServer());
170             }
171             if (getConnectorServerUrl() != null)
172             {
173                 agent.setConnectorServerUrl(getConnectorServerUrl());
174             }
175             if (getConnectorServerProperties() != null && !getConnectorServerProperties().isEmpty())
176             {
177                 agent.setConnectorServerProperties(getConnectorServerProperties());
178             }
179             // these can be copied as is
180             agent.setCreateServer(isCreateServer());
181             agent.setLocateServer(isLocateServer());
182             agent.setEnableStatistics(isEnableStatistics());
183             agent.setCreateRmiRegistry(isCreateRmiRegistry());
184             agent.setCredentials(credentials);
185         }
186         catch (RegistrationException e)
187         {
188             throw new RuntimeException(e);
189         }
190     }
191 
192     public void setName(String name)
193     {
194         // ignore the name, spring wants it
195     }
196 }