1 /*
2 * $Id: JmxRegistrationContext.java 22391 2011-07-12 12:00:48Z dirk.olmes $
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 @Override
57 public void onNotification(MuleContextNotification notification)
58 {
59 MuleContextNotification mn = 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 }
70 catch (NotificationException e)
71 {
72 logger.warn("Did not cleanup properly.", e);
73 }
74 }
75
76 /**
77 * Get current context or create one if none exist for the current startup cycle.
78 * @return jmx registration context
79 */
80 public static JmxRegistrationContext getCurrent(MuleContext context)
81 {
82 JmxRegistrationContext ctx = contexts.get();
83 if (ctx == null)
84 {
85 ctx = new JmxRegistrationContext(context);
86 }
87 contexts.set(ctx);
88 return ctx;
89 }
90
91 /**
92 * Getter for property 'resolvedDomain'.
93 *
94 * @return Value for property 'resolvedDomain'.
95 */
96 public String getResolvedDomain()
97 {
98 return resolvedDomain;
99 }
100
101 /**
102 * Setter for property 'resolvedDomain'.
103 *
104 * @param resolvedDomain Value to set for property 'resolvedDomain'.
105 */
106 public void setResolvedDomain(String resolvedDomain)
107 {
108 this.resolvedDomain = resolvedDomain;
109 }
110 }