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