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