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