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