1 /*
2 * $Id: JmxRegistrationContext.java 19191 2010-08-25 21:05:23Z tcarlson $
3 * --------------------------------------------------------------------------------------
4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.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.context.notification.MuleContextNotification;
15 import org.mule.context.notification.NotificationException;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
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<JmxRegistrationContext> contexts = new ThreadLocal<JmxRegistrationContext>();
44
45 private String resolvedDomain;
46
47 /** Do not instantiate JmxRegistrationContext. */
48 private JmxRegistrationContext(MuleContext context)
49 {
50 try
51 {
52 // register the cleanup hook, otherwise server stop/start cycles may produce
53 // Mule JMX domains with ever increasing suffix.
54 context.registerListener(new MuleContextNotificationListener<MuleContextNotification>()
55 {
56 public void onNotification(MuleContextNotification notification)
57 {
58 MuleContextNotification mn = (MuleContextNotification) notification;
59 if (MuleContextNotification.CONTEXT_DISPOSED == mn.getAction())
60 {
61 // just in case someone is holding a ref to the context instance
62 resolvedDomain = null;
63 // disassociate
64 contexts.set(null);
65 }
66 }
67 });
68 } catch (NotificationException e)
69 {
70 logger.warn("Did not cleanup properly.", e);
71 }
72 }
73
74 /**
75 * Get current context or create one if none exist for the current startup cycle.
76 * @return jmx registration context
77 */
78 public static JmxRegistrationContext getCurrent(MuleContext context)
79 {
80 JmxRegistrationContext ctx = contexts.get();
81 if (ctx == null)
82 {
83 ctx = new JmxRegistrationContext(context);
84 }
85 contexts.set(ctx);
86 return ctx;
87 }
88
89 /**
90 * Getter for property 'resolvedDomain'.
91 *
92 * @return Value for property 'resolvedDomain'.
93 */
94 public String getResolvedDomain()
95 {
96 return resolvedDomain;
97 }
98
99 /**
100 * Setter for property 'resolvedDomain'.
101 *
102 * @param resolvedDomain Value to set for property 'resolvedDomain'.
103 */
104 public void setResolvedDomain(String resolvedDomain)
105 {
106 this.resolvedDomain = resolvedDomain;
107 }
108 }