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