KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > support > JmxRegistrationContext


1 /*
2  * $Id: JmxRegistrationContext.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.mule.MuleManager;
15 import org.mule.impl.internal.notifications.ManagerNotification;
16 import org.mule.impl.internal.notifications.ManagerNotificationListener;
17 import org.mule.impl.internal.notifications.NotificationException;
18 import org.mule.umo.manager.UMOServerNotification;
19
20 /**
21  * Stores JMX info pertinent to the currently intialising Mule manager with
22  * JMX management enabled. The info is being kept for the duration of Mule server life,
23  * and cleared on manager disposal.
24  * <p/>
25  * The main reason for that class is that JmxAgent prepares only the JMX foundation, while
26  * the agents following it may require some contextual information about Mule's JMX, such as
27  * a currently resolved Mule domain name (if non-clashing JMX domains support is enabled, which
28  * is by default). Otherwise, they are left unaware of the previous work, and a random number
29  * of JMX domains might be created for Mule.
30  */

31 public class JmxRegistrationContext
32 {
33     /**
34      * The logger used for this class
35      */

36     private final transient Log logger = LogFactory.getLog(getClass());
37
38     /**
39      * Normally ThreadLocal is fine, as Mule is being initialised and destroyed
40      * by a single thread. We only need to share this info between random agents
41      * during startup.
42      */

43     private static final ThreadLocal JavaDoc contexts = new ThreadLocal JavaDoc();
44
45     private String JavaDoc resolvedDomain;
46
47     /** Do not instantiate JmxRegistrationContext. */
48     private JmxRegistrationContext()
49     {
50         // no manager available, bail out
51
if (!MuleManager.isInstanciated())
52         {
53             return;
54         }
55
56         try
57         {
58             // register the cleanup hook, otherwise server stop/start cycles may produce
59
// Mule JMX domains with ever increasing suffix.
60
MuleManager.getInstance().registerListener(new ManagerNotificationListener()
61             {
62                 public void onNotification(UMOServerNotification notification)
63                 {
64                     ManagerNotification mn = (ManagerNotification) notification;
65                     if (ManagerNotification.MANAGER_DISPOSED == mn.getAction())
66                     {
67                         // just in case someone is holding a ref to the context instance
68
resolvedDomain = null;
69                         // disassociate
70
contexts.set(null);
71                     }
72                 }
73             });
74         } catch (NotificationException e)
75         {
76             logger.warn("Did not cleanup properly.", e);
77         }
78     }
79
80     /**
81      * Get current context or create one if none exist for the current startup cycle.
82      * @return jmx registration context
83      */

84     public static JmxRegistrationContext getCurrent()
85     {
86         JmxRegistrationContext ctx = (JmxRegistrationContext) contexts.get();
87         if (ctx == null)
88         {
89             ctx = new JmxRegistrationContext();
90         }
91         contexts.set(ctx);
92         return ctx;
93     }
94
95     /**
96      * Getter for property 'resolvedDomain'.
97      *
98      * @return Value for property 'resolvedDomain'.
99      */

100     public String JavaDoc getResolvedDomain()
101     {
102         return resolvedDomain;
103     }
104
105     /**
106      * Setter for property 'resolvedDomain'.
107      *
108      * @param resolvedDomain Value to set for property 'resolvedDomain'.
109      */

110     public void setResolvedDomain(String JavaDoc resolvedDomain)
111     {
112         this.resolvedDomain = resolvedDomain;
113     }
114 }
115
Popular Tags