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