Coverage Report - org.mule.module.management.agent.WrapperManagerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
WrapperManagerAgent
34%
20/59
15%
3/20
2.818
 
 1  
 /*
 2  
  * $Id: WrapperManagerAgent.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.api.MuleException;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.module.management.i18n.ManagementMessages;
 18  
 import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
 19  
 import org.mule.module.management.support.JmxSupport;
 20  
 import org.mule.module.management.support.JmxSupportFactory;
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 import javax.management.InstanceNotFoundException;
 25  
 import javax.management.MBeanRegistrationException;
 26  
 import javax.management.MBeanServer;
 27  
 import javax.management.MBeanServerFactory;
 28  
 import javax.management.MalformedObjectNameException;
 29  
 import javax.management.ObjectName;
 30  
 
 31  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
 32  
 
 33  
 import org.apache.commons.logging.Log;
 34  
 import org.apache.commons.logging.LogFactory;
 35  
 import org.tanukisoftware.wrapper.jmx.WrapperManager;
 36  
 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
 37  
 
 38  
 /**
 39  
  * This agent integrates Java Service Wrapper into Mule. See
 40  
  * <a href="http://wrapper.tanukisoftware.org">http://wrapper.tanukisoftware.org</a>
 41  
  * for more details.
 42  
  */
 43  
 public class WrapperManagerAgent extends AbstractAgent
 44  
 {
 45  
     /**
 46  
      * MBean name to register under.
 47  
      */
 48  
     public static final String WRAPPER_OBJECT_NAME = "name=WrapperManager";
 49  
 
 50  
     /**
 51  
      * For cases when Mule is embedded in another process and that external process
 52  
      * had registered the MBean.
 53  
      */
 54  
     public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
 55  
 
 56  12
     private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
 57  
 
 58  
     /**
 59  
      * This property is set by the native launcher, used for extra checks.
 60  
      */
 61  
     private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
 62  
 
 63  
     private MBeanServer mBeanServer;
 64  
     private ObjectName wrapperName;
 65  
 
 66  22
     private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
 67  22
     private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
 68  
 
 69  
     // atomic reference to avoid unnecessary construction calls
 70  22
     private final AtomicReference/*<WrapperManagerMBean>*/ wrapperManagerRef = new AtomicReference();
 71  
 
 72  
 
 73  
     public WrapperManagerAgent()
 74  
     {
 75  22
         super("Wrapper Manager");
 76  22
     }
 77  
 
 78  
     public void initialise() throws InitialisationException
 79  
     {
 80  
         try
 81  
         {
 82  22
             final List servers = MBeanServerFactory.findMBeanServer(null);
 83  22
             if (servers.isEmpty())
 84  
             {
 85  0
                 throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
 86  
             }
 87  
 
 88  22
             mBeanServer = (MBeanServer) servers.get(0);
 89  
 
 90  
             /*
 91  
               Perform an extra check ourselves. If 'wrapper.native_library' property has
 92  
               not been set, which is the case for embedded scenarios, don't even try to
 93  
               construct the wrapper manager bean, as it performs a number of checks internally
 94  
               and outputs a very verbose warning.
 95  
             */
 96  
             boolean launchedByWrapper;
 97  22
             if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
 98  
             {
 99  22
                 launchedByWrapper = false;
 100  
             }
 101  
             // Check if an external process registered a wrapper MBean under default name.
 102  0
             else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
 103  
             {
 104  0
                 logger.info("Mule is embedded in a container already launched by a wrapper." +
 105  
                             "Duplicates will not be registered. Use the " + DEFAULT_WRAPPER_MBEAN_NAME + " MBean " +
 106  
                             "instead for control.");
 107  0
                 unregisterMeQuietly();
 108  0
                 return;
 109  
             }
 110  
             else
 111  
             {
 112  0
                 lazyInitWrapperManager();
 113  0
                 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
 114  
             }
 115  
 
 116  22
             if (!launchedByWrapper)
 117  
             {
 118  22
                 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
 119  22
                 unregisterMeQuietly();
 120  22
                 return;
 121  
             }
 122  
 
 123  0
             wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_OBJECT_NAME);
 124  
 
 125  0
             unregisterMBeansIfNecessary();
 126  0
             mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
 127  
         }
 128  0
         catch (InitialisationException iex)
 129  
         {
 130  
             // rethrow
 131  0
             throw iex;
 132  
         }
 133  0
         catch (Exception e)
 134  
         {
 135  0
             throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
 136  0
         }
 137  0
     }
 138  
 
 139  
     public void start() throws MuleException 
 140  
     {
 141  
         // nothing to do
 142  22
     }
 143  
 
 144  
     public void stop() throws MuleException
 145  
     {
 146  
         // nothing to do
 147  22
     }
 148  
 
 149  
     /* @see org.mule.api.lifecycle.Disposable#dispose() */
 150  
     public void dispose()
 151  
     {
 152  
         try
 153  
         {
 154  0
             unregisterMBeansIfNecessary();
 155  
         }
 156  0
         catch (Exception e)
 157  
         {
 158  0
             logger.error("Couldn't unregister MBean: "
 159  
                          + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
 160  0
         }
 161  0
     }
 162  
 
 163  
     /* @see org.mule.api.context.Agent#registered() */
 164  
     public void registered()
 165  
     {
 166  
         // nothing to do
 167  0
     }
 168  
 
 169  
     /* @see org.mule.api.context.Agent#unregistered() */
 170  
     public void unregistered()
 171  
     {
 172  
         // nothing to do
 173  0
     }
 174  
 
 175  
     // /////////////////////////////////////////////////////////////////////////
 176  
     // Getters and setters
 177  
     // /////////////////////////////////////////////////////////////////////////
 178  
 
 179  
     /* @see org.mule.api.context.Agent#getDescription() */
 180  
     public String getDescription()
 181  
     {
 182  0
         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
 183  0
         if (wm == null)
 184  
         {
 185  0
             return "Wrapper Manager";
 186  
         }
 187  0
         else return "Wrapper Manager: Mule PID #" + wm.getJavaPID() +
 188  
                 ", Wrapper PID #" + wm.getWrapperPID();
 189  
     }
 190  
 
 191  
     protected void lazyInitWrapperManager() {
 192  0
         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
 193  
 
 194  0
         if (wm != null)
 195  
         {
 196  0
             return;
 197  
         }
 198  
 
 199  0
         wm = new WrapperManager();
 200  0
         wrapperManagerRef.compareAndSet(null, wm);
 201  0
     }
 202  
 
 203  
     /**
 204  
      * Unregister all MBeans if there are any left over the old deployment
 205  
      */
 206  
     protected void unregisterMBeansIfNecessary()
 207  
         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
 208  0
         if (mBeanServer == null || wrapperName == null)
 209  
         {
 210  0
             return;
 211  
         }
 212  0
         if (mBeanServer.isRegistered(wrapperName))
 213  
         {
 214  0
             mBeanServer.unregisterMBean(wrapperName);
 215  
         }
 216  0
     }
 217  
 
 218  
     /**
 219  
      * Quietly unregister ourselves.
 220  
      */
 221  
     protected void unregisterMeQuietly()
 222  
     {
 223  
         try
 224  
         {
 225  
             // remove the agent from the list, it's not functional
 226  22
             muleContext.getRegistry().unregisterAgent(this.getName());
 227  
         }
 228  0
         catch (MuleException e)
 229  
         {
 230  
             // not interested, really
 231  22
         }
 232  22
     }
 233  
 
 234  
 }