Coverage Report - org.mule.module.management.agent.WrapperManagerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
WrapperManagerAgent
0%
0/70
0%
0/28
0
 
 1  
 /*
 2  
  * $Id: WrapperManagerAgent.java 19747 2010-09-28 03:24:32Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.WrapperSystemPropertyUtil;
 36  
 import org.tanukisoftware.wrapper.jmx.WrapperManager;
 37  
 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
 38  
 import org.tanukisoftware.wrapper.security.WrapperPermission;
 39  
 
 40  
 /**
 41  
  * This agent integrates Java Service Wrapper into Mule. See
 42  
  * <a href="http://wrapper.tanukisoftware.org">http://wrapper.tanukisoftware.org</a>
 43  
  * for more details.
 44  
  */
 45  
 public class WrapperManagerAgent extends AbstractAgent
 46  
 {
 47  
     /**
 48  
      * MBean name to register under.
 49  
      */
 50  
     public static final String WRAPPER_JMX_NAME = "name=WrapperManager";
 51  
 
 52  
     /**
 53  
      * For cases when Mule is embedded in another process and that external process
 54  
      * had registered the MBean.
 55  
      */
 56  
     public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
 57  
 
 58  0
     private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
 59  
 
 60  
     /**
 61  
      * This property is set by the native launcher, used for extra checks.
 62  
      */
 63  
     private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
 64  
 
 65  
     private MBeanServer mBeanServer;
 66  
     private ObjectName wrapperName;
 67  
 
 68  0
     private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
 69  0
     private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
 70  
 
 71  
     // atomic reference to avoid unnecessary construction calls
 72  0
     private final AtomicReference/*<WrapperManagerMBean>*/ wrapperManagerRef = new AtomicReference();
 73  
 
 74  
 
 75  
     public WrapperManagerAgent()
 76  
     {
 77  0
         super("wrapper-manager");
 78  0
     }
 79  
 
 80  
     public void initialise() throws InitialisationException
 81  
     {
 82  
         try
 83  
         {
 84  0
             List<?> servers = MBeanServerFactory.findMBeanServer(null);
 85  0
             if (servers.isEmpty())
 86  
             {
 87  0
                 throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
 88  
             }
 89  
 
 90  0
             mBeanServer = (MBeanServer) servers.get(0);
 91  
 
 92  
             /*
 93  
               Perform an extra check ourselves. If 'wrapper.native_library' property has
 94  
               not been set, which is the case for embedded scenarios, don't even try to
 95  
               construct the wrapper manager bean, as it performs a number of checks internally
 96  
               and outputs a very verbose warning.
 97  
             */
 98  
             boolean launchedByWrapper;
 99  0
             if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
 100  
             {
 101  0
                 launchedByWrapper = false;
 102  
             }
 103  
             // Check if an external process registered a wrapper MBean under default name.
 104  0
             else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
 105  
             {
 106  0
                 logger.info("Mule is embedded in a container already launched by a wrapper." +
 107  
                             "Duplicates will not be registered. Use the " + DEFAULT_WRAPPER_MBEAN_NAME + " MBean " +
 108  
                             "instead for control.");
 109  0
                 unregisterMeQuietly();
 110  0
                 return;
 111  
             }
 112  
             else
 113  
             {
 114  0
                 lazyInitWrapperManager();
 115  0
                 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
 116  
             }
 117  
 
 118  0
             if (!launchedByWrapper)
 119  
             {
 120  0
                 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
 121  0
                 unregisterMeQuietly();
 122  0
                 return;
 123  
             }
 124  
 
 125  0
             final boolean containerMode = muleContext.getConfiguration().isContainerMode();
 126  0
             if (containerMode)
 127  
             {
 128  
                 // container mode, register mbean under Mule domain, no duplicates under each app's domain
 129  0
                 wrapperName = jmxSupport.getObjectName(JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX + ":" + WRAPPER_JMX_NAME);
 130  0
                 if (mBeanServer.isRegistered(wrapperName))
 131  
                 {
 132  
                     // ignore duplicate invocations when running in Mule container mode
 133  0
                     return;
 134  
                 }
 135  
             }
 136  
             else
 137  
             {
 138  
                 // embedded case, use Mule's single domain
 139  0
                 wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_JMX_NAME);
 140  
             }
 141  
 
 142  0
             unregisterMBeansIfNecessary();
 143  0
             mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
 144  
         }
 145  0
         catch (InitialisationException iex)
 146  
         {
 147  
             // rethrow
 148  0
             throw iex;
 149  
         }
 150  0
         catch (Exception e)
 151  
         {
 152  0
             throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
 153  0
         }
 154  0
     }
 155  
 
 156  
     public void start() throws MuleException
 157  
     {
 158  
         // nothing to do
 159  0
     }
 160  
 
 161  
     public void stop() throws MuleException
 162  
     {
 163  
         // nothing to do
 164  0
     }
 165  
 
 166  
     /* @see org.mule.api.lifecycle.Disposable#dispose() */
 167  
     public void dispose()
 168  
     {
 169  
         try
 170  
         {
 171  0
             unregisterMBeansIfNecessary();
 172  
         }
 173  0
         catch (Exception e)
 174  
         {
 175  0
             logger.error("Couldn't unregister MBean: "
 176  
                          + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
 177  0
         }
 178  0
     }
 179  
 
 180  
 
 181  
     // /////////////////////////////////////////////////////////////////////////
 182  
     // Getters and setters
 183  
     // /////////////////////////////////////////////////////////////////////////
 184  
 
 185  
     /* @see org.mule.api.context.Agent#getDescription() */
 186  
     @Override
 187  
     public String getDescription()
 188  
     {
 189  0
         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
 190  0
         if (wm == null)
 191  
         {
 192  0
             return "Wrapper Manager";
 193  
         }
 194  
         else
 195  
         {
 196  0
             return "Wrapper Manager: Mule PID #" + getJavaPID() + ", Wrapper PID #" + getWrapperPID();
 197  
         }
 198  
     }
 199  
 
 200  
     /**
 201  
      * This method is a copy of the implementation of
 202  
      * {@link WrapperManagerMBean#getJavaPID()} and it is here because that method is
 203  
      * not present in the {@link WrapperManagerMBean} until version 3.2.3.
 204  
      * SpringSource's TC Server uses The wrapper version 3.2.0 so having this method
 205  
      * here allows us to be compatible with TC Server.
 206  
      * 
 207  
      * @return The PID of the Java process.
 208  
      * @see http://www.mulesoft.org/jira/browse/MULE-5106
 209  
      */
 210  
     public static int getJavaPID()
 211  
     {
 212  0
         SecurityManager sm = System.getSecurityManager();
 213  0
         if (sm != null)
 214  
         {
 215  0
             sm.checkPermission(new WrapperPermission("getJavaPID"));
 216  
         }
 217  
 
 218  0
         return WrapperSystemPropertyUtil.getIntProperty("wrapper.java.pid", 0);
 219  
     }
 220  
 
 221  
     /**
 222  
      * This method is a copy of the implementation of
 223  
      * {@link WrapperManagerMBean#getWrapperPID()} and it is here because that method
 224  
      * is not present in the {@link WrapperManagerMBean} until version 3.2.3.
 225  
      * SpringSource's TC Server uses The wrapper version 3.2.0 so having this method
 226  
      * here allows us to be compatible with TC Server.
 227  
      * 
 228  
      * @return The PID of the Wrapper process.
 229  
      * @see http://www.mulesoft.org/jira/browse/MULE-5106
 230  
      */
 231  
     public static int getWrapperPID()
 232  
     {
 233  0
         SecurityManager sm = System.getSecurityManager();
 234  0
         if (sm != null)
 235  
         {
 236  0
             sm.checkPermission(new WrapperPermission("getWrapperPID"));
 237  
         }
 238  
 
 239  0
         return WrapperSystemPropertyUtil.getIntProperty("wrapper.pid", 0);
 240  
     }
 241  
 
 242  
     protected void lazyInitWrapperManager() {
 243  0
         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
 244  
 
 245  0
         if (wm != null)
 246  
         {
 247  0
             return;
 248  
         }
 249  
 
 250  0
         wm = new WrapperManager();
 251  0
         wrapperManagerRef.compareAndSet(null, wm);
 252  0
     }
 253  
 
 254  
     /**
 255  
      * Unregister all MBeans if there are any left over the old deployment
 256  
      */
 257  
     protected void unregisterMBeansIfNecessary()
 258  
         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
 259  0
         if (mBeanServer == null || wrapperName == null)
 260  
         {
 261  0
             return;
 262  
         }
 263  0
         if (mBeanServer.isRegistered(wrapperName))
 264  
         {
 265  0
             mBeanServer.unregisterMBean(wrapperName);
 266  
         }
 267  0
     }
 268  
 
 269  
     /**
 270  
      * Quietly unregister ourselves.
 271  
      */
 272  
     protected void unregisterMeQuietly()
 273  
     {
 274  
         try
 275  
         {
 276  
             // remove the agent from the list, it's not functional
 277  0
             muleContext.getRegistry().unregisterAgent(this.getName());
 278  
         }
 279  0
         catch (MuleException e)
 280  
         {
 281  
             // not interested, really
 282  0
         }
 283  0
     }
 284  
 }