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