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