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 public class JmxRegistrationContext
33 {
34 /**
35 * The logger used for this class
36 */
37 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 private static final ThreadLocal contexts = new ThreadLocal();
45
46 private String resolvedDomain;
47
48 /** Do not instantiate JmxRegistrationContext. */
49 private JmxRegistrationContext(MuleContext context)
50 {
51 try
52 {
53 // register the cleanup hook, otherwise server stop/start cycles may produce
54 // Mule JMX domains with ever increasing suffix.
55 context.registerListener(new MuleContextNotificationListener()
56 {
57 public void onNotification(ServerNotification notification)
58 {
59 MuleContextNotification mn = (MuleContextNotification) notification;
60 if (MuleContextNotification.CONTEXT_DISPOSED == mn.getAction())
61 {
62 // just in case someone is holding a ref to the context instance
63 resolvedDomain = null;
64 // disassociate
65 contexts.set(null);
66 }
67 }
68 });
69 } catch (NotificationException e)
70 {
71 logger.warn("Did not cleanup properly.", e);
72 }
73 }
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 JmxRegistrationContext ctx = (JmxRegistrationContext) contexts.get();
82 if (ctx == null)
83 {
84 ctx = new JmxRegistrationContext(context);
85 }
86 contexts.set(ctx);
87 return ctx;
88 }
89
90 /**
91 * Getter for property 'resolvedDomain'.
92 *
93 * @return Value for property 'resolvedDomain'.
94 */
95 public String getResolvedDomain()
96 {
97 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 this.resolvedDomain = resolvedDomain;
108 }
109 }