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 public class JmxRegistrationContext
33 {
34 /**
35 * The logger used for this class
36 */
37 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 private static final ThreadLocal contexts = new ThreadLocal();
45
46 private String resolvedDomain;
47
48 /** Do not instantiate JmxRegistrationContext. */
49 protected JmxRegistrationContext()
50 {
51 // no manager available, bail out
52 if (!MuleManager.isInstanciated())
53 {
54 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 MuleManager.getInstance().registerListener(new ManagerNotificationListener()
62 {
63 public void onNotification(UMOServerNotification notification)
64 {
65 ManagerNotification mn = (ManagerNotification) notification;
66 if (ManagerNotification.MANAGER_DISPOSED == mn.getAction())
67 {
68 // just in case someone is holding a ref to the context instance
69 resolvedDomain = null;
70 // disassociate
71 contexts.set(null);
72 }
73 }
74 });
75 } catch (NotificationException e)
76 {
77 logger.warn("Did not cleanup properly.", e);
78 }
79 }
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 JmxRegistrationContext ctx = (JmxRegistrationContext) contexts.get();
88 if (ctx == null)
89 {
90 ctx = new JmxRegistrationContext();
91 }
92 contexts.set(ctx);
93 return ctx;
94 }
95
96 /**
97 * Getter for property 'resolvedDomain'.
98 *
99 * @return Value for property 'resolvedDomain'.
100 */
101 public String getResolvedDomain()
102 {
103 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 this.resolvedDomain = resolvedDomain;
114 }
115 }