1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.management.agent;
12
13 import org.mule.AbstractAgent;
14 import org.mule.api.MuleException;
15 import org.mule.api.lifecycle.InitialisationException;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.module.management.i18n.ManagementMessages;
18 import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
19 import org.mule.module.management.support.JmxSupport;
20 import org.mule.module.management.support.JmxSupportFactory;
21
22 import java.util.List;
23
24 import javax.management.InstanceNotFoundException;
25 import javax.management.MBeanRegistrationException;
26 import javax.management.MBeanServer;
27 import javax.management.MBeanServerFactory;
28 import javax.management.MalformedObjectNameException;
29 import javax.management.ObjectName;
30
31 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.tanukisoftware.wrapper.jmx.WrapperManager;
36 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
37
38
39
40
41
42
43 public class WrapperManagerAgent extends AbstractAgent
44 {
45
46
47
48 public static final String WRAPPER_OBJECT_NAME = "name=WrapperManager";
49
50
51
52
53
54 public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
55
56 private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
57
58
59
60
61 private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
62
63 private MBeanServer mBeanServer;
64 private ObjectName wrapperName;
65
66 private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
67 private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
68
69
70 private final AtomicReference
71
72
73 public WrapperManagerAgent()
74 {
75 super("Wrapper Manager");
76 }
77
78 public void initialise() throws InitialisationException
79 {
80 try
81 {
82 final List servers = MBeanServerFactory.findMBeanServer(null);
83 if (servers.isEmpty())
84 {
85 throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
86 }
87
88 mBeanServer = (MBeanServer) servers.get(0);
89
90
91
92
93
94
95
96 boolean launchedByWrapper;
97 if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
98 {
99 launchedByWrapper = false;
100 }
101
102 else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
103 {
104 logger.info("Mule is embedded in a container already launched by a wrapper." +
105 "Duplicates will not be registered. Use the " + DEFAULT_WRAPPER_MBEAN_NAME + " MBean " +
106 "instead for control.");
107 unregisterMeQuietly();
108 return;
109 }
110 else
111 {
112 lazyInitWrapperManager();
113 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
114 }
115
116 if (!launchedByWrapper)
117 {
118 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
119 unregisterMeQuietly();
120 return;
121 }
122
123 wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_OBJECT_NAME);
124
125 unregisterMBeansIfNecessary();
126 mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
127 }
128 catch (InitialisationException iex)
129 {
130
131 throw iex;
132 }
133 catch (Exception e)
134 {
135 throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
136 }
137 }
138
139 public void start() throws MuleException
140 {
141
142 }
143
144 public void stop() throws MuleException
145 {
146
147 }
148
149
150 public void dispose()
151 {
152 try
153 {
154 unregisterMBeansIfNecessary();
155 }
156 catch (Exception e)
157 {
158 logger.error("Couldn't unregister MBean: "
159 + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
160 }
161 }
162
163
164 public void registered()
165 {
166
167 }
168
169
170 public void unregistered()
171 {
172
173 }
174
175
176
177
178
179
180 public String getDescription()
181 {
182 WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
183 if (wm == null)
184 {
185 return "Wrapper Manager";
186 }
187 else return "Wrapper Manager: Mule PID #" + wm.getJavaPID() +
188 ", Wrapper PID #" + wm.getWrapperPID();
189 }
190
191 protected void lazyInitWrapperManager() {
192 WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
193
194 if (wm != null)
195 {
196 return;
197 }
198
199 wm = new WrapperManager();
200 wrapperManagerRef.compareAndSet(null, wm);
201 }
202
203
204
205
206 protected void unregisterMBeansIfNecessary()
207 throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
208 if (mBeanServer == null || wrapperName == null)
209 {
210 return;
211 }
212 if (mBeanServer.isRegistered(wrapperName))
213 {
214 mBeanServer.unregisterMBean(wrapperName);
215 }
216 }
217
218
219
220
221 protected void unregisterMeQuietly()
222 {
223 try
224 {
225
226 muleContext.getRegistry().unregisterAgent(this.getName());
227 }
228 catch (MuleException e)
229 {
230
231 }
232 }
233
234 }