Coverage Report - org.mule.module.management.support.JmxRegistrationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxRegistrationContext
88%
15/17
100%
2/2
1.6
JmxRegistrationContext$1
100%
6/6
100%
2/2
1.6
 
 1  
 /*
 2  
  * $Id: JmxRegistrationContext.java 11234 2008-03-06 23:44:34Z tcarlson $
 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.module.management.support;
 11  
 
 12  
 import org.mule.api.MuleContext;
 13  
 import org.mule.api.context.notification.MuleContextNotificationListener;
 14  
 import org.mule.api.context.notification.ServerNotification;
 15  
 import org.mule.context.notification.MuleContextNotification;
 16  
 import org.mule.context.notification.NotificationException;
 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  48
 public class JmxRegistrationContext
 33  
 {
 34  
     /**
 35  
      * The logger used for this class
 36  
      */
 37  26
     private final transient 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  16
     private static final ThreadLocal contexts = new ThreadLocal();
 45  
 
 46  
     private String resolvedDomain;
 47  
 
 48  
     /** Do not instantiate JmxRegistrationContext. */
 49  
     private JmxRegistrationContext(MuleContext context)
 50  26
     {
 51  
         try
 52  
         {
 53  
             // register the cleanup hook, otherwise server stop/start cycles may produce
 54  
             // Mule JMX domains with ever increasing suffix.
 55  26
             context.registerListener(new MuleContextNotificationListener()
 56  
             {
 57  26
                 public void onNotification(ServerNotification notification)
 58  
                 {
 59  188
                     MuleContextNotification mn = (MuleContextNotification) notification;
 60  188
                     if (MuleContextNotification.CONTEXT_DISPOSED == mn.getAction())
 61  
                     {
 62  
                         // just in case someone is holding a ref to the context instance
 63  24
                         resolvedDomain = null;
 64  
                         // disassociate
 65  24
                         contexts.set(null);
 66  
                     }
 67  188
                 }
 68  
             });
 69  0
         } catch (NotificationException e)
 70  
         {
 71  0
             logger.warn("Did not cleanup properly.", e);
 72  26
         }
 73  26
     }
 74  
 
 75  
     /**
 76  
      * Get current context or create one if none exist for the current startup cycle.
 77  
      * @return jmx registration context
 78  
      */
 79  
     public static JmxRegistrationContext getCurrent(MuleContext context)
 80  
     {
 81  122
         JmxRegistrationContext ctx = (JmxRegistrationContext) contexts.get();
 82  122
         if (ctx == null)
 83  
         {
 84  26
             ctx = new JmxRegistrationContext(context);
 85  
         }
 86  122
         contexts.set(ctx);
 87  122
         return ctx;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Getter for property 'resolvedDomain'.
 92  
      *
 93  
      * @return Value for property 'resolvedDomain'.
 94  
      */
 95  
     public String getResolvedDomain()
 96  
     {
 97  122
         return resolvedDomain;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Setter for property 'resolvedDomain'.
 102  
      *
 103  
      * @param resolvedDomain Value to set for property 'resolvedDomain'.
 104  
      */
 105  
     public void setResolvedDomain(String resolvedDomain)
 106  
     {
 107  26
         this.resolvedDomain = resolvedDomain;
 108  26
     }
 109  
 }