1 /*
2 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
3 * The software in this package is published under the terms of the CPAL v1.0
4 * license, a copy of which has been included with this distribution in the
5 * LICENSE.txt file.
6 */
7 package org.mule.module.management.support;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.context.notification.MuleContextNotificationListener;
11 import org.mule.context.notification.MuleContextNotification;
12 import org.mule.context.notification.NotificationException;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 /**
18 * Stores JMX info pertinent to the currently intialising Mule manager with
19 * JMX management enabled. The info is being kept for the duration of Mule server life,
20 * and cleared on manager disposal.
21 * <p/>
22 * The main reason for that class is that JmxAgent prepares only the JMX foundation, while
23 * the agents following it may require some contextual information about Mule's JMX, such as
24 * a currently resolved Mule domain name (if non-clashing JMX domains support is enabled, which
25 * is by default). Otherwise, they are left unaware of the previous work, and a random number
26 * of JMX domains might be created for Mule.
27 */
28 public class JmxRegistrationContext
29 {
30 /**
31 * The logger used for this class
32 */
33 private final transient Log logger = LogFactory.getLog(getClass());
34
35 /**
36 * Normally ThreadLocal is fine, as Mule is being initialised and destroyed
37 * by a single thread. We only need to share this info between random agents
38 * during startup.
39 */
40 private static final ThreadLocal<JmxRegistrationContext> contexts = new ThreadLocal<JmxRegistrationContext>();
41
42 private String resolvedDomain;
43
44 /** Do not instantiate JmxRegistrationContext. */
45 private JmxRegistrationContext(MuleContext context)
46 {
47 try
48 {
49 // register the cleanup hook, otherwise server stop/start cycles may produce
50 // Mule JMX domains with ever increasing suffix.
51 context.registerListener(new MuleContextNotificationListener<MuleContextNotification>()
52 {
53 public void onNotification(MuleContextNotification notification)
54 {
55 MuleContextNotification mn = (MuleContextNotification) notification;
56 if (MuleContextNotification.CONTEXT_DISPOSED == mn.getAction())
57 {
58 // just in case someone is holding a ref to the context instance
59 resolvedDomain = null;
60 // disassociate
61 contexts.set(null);
62 }
63 }
64 });
65 } catch (NotificationException e)
66 {
67 logger.warn("Did not cleanup properly.", e);
68 }
69 }
70
71 /**
72 * Get current context or create one if none exist for the current startup cycle.
73 * @return jmx registration context
74 */
75 public static JmxRegistrationContext getCurrent(MuleContext context)
76 {
77 JmxRegistrationContext ctx = contexts.get();
78 if (ctx == null)
79 {
80 ctx = new JmxRegistrationContext(context);
81 }
82 contexts.set(ctx);
83 return ctx;
84 }
85
86 /**
87 * Getter for property 'resolvedDomain'.
88 *
89 * @return Value for property 'resolvedDomain'.
90 */
91 public String getResolvedDomain()
92 {
93 return resolvedDomain;
94 }
95
96 /**
97 * Setter for property 'resolvedDomain'.
98 *
99 * @param resolvedDomain Value to set for property 'resolvedDomain'.
100 */
101 public void setResolvedDomain(String resolvedDomain)
102 {
103 this.resolvedDomain = resolvedDomain;
104 }
105 }