Coverage Report - org.mule.module.management.agent.JmxAgentConfigurer
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxAgentConfigurer
0%
0/57
0%
0/12
0
 
 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  0
     protected static final Log logger = LogFactory.getLog(JmxAgentConfigurer.class);
 37  
 
 38  
     protected MuleContext muleContext;
 39  
 
 40  
     /**
 41  
      * Should MBeanServer be discovered.
 42  
      */
 43  0
     protected boolean locateServer = true;
 44  
     // don't create mbean server by default, use a platform mbean server
 45  0
     private boolean createServer = false;
 46  
     private String connectorServerUrl;
 47  
     private MBeanServer mBeanServer;
 48  0
     private Map<String, Object> connectorServerProperties = null;
 49  0
     private boolean enableStatistics = true;
 50  0
     private boolean createRmiRegistry = true;
 51  
 
 52  
     /**
 53  
      * Username/password combinations for JMX Remoting authentication.
 54  
      */
 55  0
     private Map<String, String> credentials = new HashMap<String, String>();
 56  
 
 57  
     static
 58  
     {
 59  0
         Map<String, Object> props = new HashMap<String, Object>(1);
 60  0
         props.put(RMIConnectorServer.JNDI_REBIND_ATTRIBUTE, "true");
 61  0
         DEFAULT_CONNECTOR_SERVER_PROPERTIES = Collections.unmodifiableMap(props);
 62  0
     }
 63  
 
 64  
     public JmxAgentConfigurer()
 65  0
     {
 66  0
         connectorServerProperties = new HashMap<String, Object>(DEFAULT_CONNECTOR_SERVER_PROPERTIES);
 67  0
     }
 68  
 
 69  
     public boolean isCreateServer()
 70  
     {
 71  0
         return createServer;
 72  
     }
 73  
 
 74  
     public void setCreateServer(boolean createServer)
 75  
     {
 76  0
         this.createServer = createServer;
 77  0
     }
 78  
 
 79  
     public boolean isLocateServer()
 80  
     {
 81  0
         return locateServer;
 82  
     }
 83  
 
 84  
     public void setLocateServer(boolean locateServer)
 85  
     {
 86  0
         this.locateServer = locateServer;
 87  0
     }
 88  
 
 89  
     public String getConnectorServerUrl()
 90  
     {
 91  0
         return connectorServerUrl;
 92  
     }
 93  
 
 94  
     public void setConnectorServerUrl(String connectorServerUrl)
 95  
     {
 96  0
         this.connectorServerUrl = connectorServerUrl;
 97  0
     }
 98  
 
 99  
     public boolean isEnableStatistics()
 100  
     {
 101  0
         return enableStatistics;
 102  
     }
 103  
 
 104  
     public void setEnableStatistics(boolean enableStatistics)
 105  
     {
 106  0
         this.enableStatistics = enableStatistics;
 107  0
     }
 108  
 
 109  
     public MBeanServer getMBeanServer()
 110  
     {
 111  0
         return mBeanServer;
 112  
     }
 113  
 
 114  
     public void setMBeanServer(MBeanServer mBeanServer)
 115  
     {
 116  0
         this.mBeanServer = mBeanServer;
 117  0
     }
 118  
 
 119  
     public Map<String, Object> getConnectorServerProperties()
 120  
     {
 121  0
         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  0
         this.connectorServerProperties = connectorServerProperties;
 135  0
     }
 136  
 
 137  
     public void setCredentials(final Map<String, String> newCredentials)
 138  
     {
 139  0
         this.credentials.clear();
 140  0
         if (newCredentials != null && !newCredentials.isEmpty())
 141  
         {
 142  0
             this.credentials.putAll(newCredentials);
 143  
         }
 144  0
     }
 145  
 
 146  
     public boolean isCreateRmiRegistry()
 147  
     {
 148  0
         return createRmiRegistry;
 149  
     }
 150  
 
 151  
     public void setCreateRmiRegistry(boolean createRmiRegistry)
 152  
     {
 153  0
         this.createRmiRegistry = createRmiRegistry;
 154  0
     }
 155  
 
 156  
     public void setMuleContext(MuleContext context)
 157  
     {
 158  0
         this.muleContext = context;
 159  
         try
 160  
         {
 161  
             // by the time mule context is injected, other attributes will have been set already
 162  0
             JmxAgent agent = muleContext.getRegistry().lookupObject(JmxAgent.class);
 163  
             // in case it is injected, otherwise will follow the init logic
 164  0
             if (getMBeanServer() != null)
 165  
             {
 166  0
                 agent.setMBeanServer(getMBeanServer());
 167  
             }
 168  0
             if (getConnectorServerUrl() != null)
 169  
             {
 170  0
                 agent.setConnectorServerUrl(getConnectorServerUrl());
 171  
             }
 172  0
             if (getConnectorServerProperties() != null && !getConnectorServerProperties().isEmpty())
 173  
             {
 174  0
                 agent.setConnectorServerProperties(getConnectorServerProperties());
 175  
             }
 176  
             // these can be copied as is
 177  0
             agent.setCreateServer(isCreateServer());
 178  0
             agent.setLocateServer(isLocateServer());
 179  0
             agent.setEnableStatistics(isEnableStatistics());
 180  0
             agent.setCreateRmiRegistry(isCreateRmiRegistry());
 181  0
             agent.setCredentials(credentials);
 182  
         }
 183  0
         catch (RegistrationException e)
 184  
         {
 185  0
             throw new RuntimeException(e);
 186  0
         }
 187  0
     }
 188  
 
 189  
     public void setName(String name)
 190  
     {
 191  
         // ignore the name, spring wants it
 192  0
     }
 193  
 }