Coverage Report - org.mule.management.support.JmxRegistrationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxRegistrationContext
0%
0/19
0%
0/2
2
JmxRegistrationContext$1
0%
0/6
0%
0/1
2
 
 1  
 /*
 2  
  * $Id: JmxRegistrationContext.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  
 package org.mule.management.support;
 11  
 
 12  
 import org.mule.MuleManager;
 13  
 import org.mule.impl.internal.notifications.ManagerNotification;
 14  
 import org.mule.impl.internal.notifications.ManagerNotificationListener;
 15  
 import org.mule.impl.internal.notifications.NotificationException;
 16  
 import org.mule.umo.manager.UMOServerNotification;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 
 21  
 /**
 22  
  * Stores JMX info pertinent to the currently intialising Mule manager with
 23  
  * JMX management enabled. The info is being kept for the duration of Mule server life,
 24  
  * and cleared on manager disposal.
 25  
  * <p/>
 26  
  * The main reason for that class is that JmxAgent prepares only the JMX foundation, while
 27  
  * the agents following it may require some contextual information about Mule's JMX, such as
 28  
  * a currently resolved Mule domain name (if non-clashing JMX domains support is enabled, which
 29  
  * is by default). Otherwise, they are left unaware of the previous work, and a random number
 30  
  * of JMX domains might be created for Mule.
 31  
  */
 32  0
 public class JmxRegistrationContext
 33  
 {
 34  
     /**
 35  
      * The logger used for this class
 36  
      */
 37  0
     protected final Log logger = LogFactory.getLog(getClass());
 38  
 
 39  
     /**
 40  
      * Normally ThreadLocal is fine, as Mule is being initialised and destroyed
 41  
      * by a single thread. We only need to share this info between random agents
 42  
      * during startup.
 43  
      */
 44  0
     private static final ThreadLocal contexts = new ThreadLocal();
 45  
 
 46  
     private String resolvedDomain;
 47  
 
 48  
     /** Do not instantiate JmxRegistrationContext. */
 49  
     protected JmxRegistrationContext()
 50  0
     {
 51  
         // no manager available, bail out
 52  0
         if (!MuleManager.isInstanciated())
 53  
         {
 54  0
             return;
 55  
         }
 56  
 
 57  
         try
 58  
         {
 59  
             // register the cleanup hook, otherwise server stop/start cycles may produce
 60  
             // Mule JMX domains with ever increasing suffix.
 61  0
             MuleManager.getInstance().registerListener(new ManagerNotificationListener()
 62  
             {
 63  0
                 public void onNotification(UMOServerNotification notification)
 64  
                 {
 65  0
                     ManagerNotification mn = (ManagerNotification) notification;
 66  0
                     if (ManagerNotification.MANAGER_DISPOSED == mn.getAction())
 67  
                     {
 68  
                         // just in case someone is holding a ref to the context instance
 69  0
                         resolvedDomain = null;
 70  
                         // disassociate
 71  0
                         contexts.set(null);
 72  
                     }
 73  0
                 }
 74  
             });
 75  0
         } catch (NotificationException e)
 76  
         {
 77  0
             logger.warn("Did not cleanup properly.", e);
 78  0
         }
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Get current context or create one if none exist for the current startup cycle.
 83  
      * @return jmx registration context
 84  
      */
 85  
     public static JmxRegistrationContext getCurrent()
 86  
     {
 87  0
         JmxRegistrationContext ctx = (JmxRegistrationContext) contexts.get();
 88  0
         if (ctx == null)
 89  
         {
 90  0
             ctx = new JmxRegistrationContext();
 91  
         }
 92  0
         contexts.set(ctx);
 93  0
         return ctx;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Getter for property 'resolvedDomain'.
 98  
      *
 99  
      * @return Value for property 'resolvedDomain'.
 100  
      */
 101  
     public String getResolvedDomain()
 102  
     {
 103  0
         return resolvedDomain;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Setter for property 'resolvedDomain'.
 108  
      *
 109  
      * @param resolvedDomain Value to set for property 'resolvedDomain'.
 110  
      */
 111  
     public void setResolvedDomain(String resolvedDomain)
 112  
     {
 113  0
         this.resolvedDomain = resolvedDomain;
 114  0
     }
 115  
 }