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.WrapperSystemPropertyUtil;
36 import org.tanukisoftware.wrapper.jmx.WrapperManager;
37 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
38 import org.tanukisoftware.wrapper.security.WrapperPermission;
39
40
41
42
43
44
45 public class WrapperManagerAgent extends AbstractAgent
46 {
47
48
49
50 public static final String WRAPPER_JMX_NAME = "name=WrapperManager";
51
52
53
54
55
56 public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
57
58 private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
59
60
61
62
63 private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
64
65 private MBeanServer mBeanServer;
66 private ObjectName wrapperName;
67
68 private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
69 private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
70
71
72 private final AtomicReference
73
74
75 public WrapperManagerAgent()
76 {
77 super("wrapper-manager");
78 }
79
80 public void initialise() throws InitialisationException
81 {
82 try
83 {
84 List<?> servers = MBeanServerFactory.findMBeanServer(null);
85 if (servers.isEmpty())
86 {
87 throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
88 }
89
90 mBeanServer = (MBeanServer) servers.get(0);
91
92
93
94
95
96
97
98 boolean launchedByWrapper;
99 if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
100 {
101 launchedByWrapper = false;
102 }
103
104 else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
105 {
106 logger.info("Mule is embedded in a container already launched by a wrapper." +
107 "Duplicates will not be registered. Use the " + DEFAULT_WRAPPER_MBEAN_NAME + " MBean " +
108 "instead for control.");
109 unregisterMeQuietly();
110 return;
111 }
112 else
113 {
114 lazyInitWrapperManager();
115 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
116 }
117
118 if (!launchedByWrapper)
119 {
120 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
121 unregisterMeQuietly();
122 return;
123 }
124
125 final boolean containerMode = muleContext.getConfiguration().isContainerMode();
126 if (containerMode)
127 {
128
129 wrapperName = jmxSupport.getObjectName(JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX + ":" + WRAPPER_JMX_NAME);
130 if (mBeanServer.isRegistered(wrapperName))
131 {
132
133 return;
134 }
135 }
136 else
137 {
138
139 wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_JMX_NAME);
140 }
141
142 unregisterMBeansIfNecessary();
143 mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
144 }
145 catch (InitialisationException iex)
146 {
147
148 throw iex;
149 }
150 catch (Exception e)
151 {
152 throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
153 }
154 }
155
156 public void start() throws MuleException
157 {
158
159 }
160
161 public void stop() throws MuleException
162 {
163
164 }
165
166
167 public void dispose()
168 {
169 try
170 {
171 unregisterMBeansIfNecessary();
172 }
173 catch (Exception e)
174 {
175 logger.error("Couldn't unregister MBean: "
176 + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
177 }
178 }
179
180
181
182
183
184
185
186 @Override
187 public String getDescription()
188 {
189 WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
190 if (wm == null)
191 {
192 return "Wrapper Manager";
193 }
194 else
195 {
196 return "Wrapper Manager: Mule PID #" + getJavaPID() + ", Wrapper PID #" + getWrapperPID();
197 }
198 }
199
200
201
202
203
204
205
206
207
208
209
210 public static int getJavaPID()
211 {
212 SecurityManager sm = System.getSecurityManager();
213 if (sm != null)
214 {
215 sm.checkPermission(new WrapperPermission("getJavaPID"));
216 }
217
218 return WrapperSystemPropertyUtil.getIntProperty("wrapper.java.pid", 0);
219 }
220
221
222
223
224
225
226
227
228
229
230
231 public static int getWrapperPID()
232 {
233 SecurityManager sm = System.getSecurityManager();
234 if (sm != null)
235 {
236 sm.checkPermission(new WrapperPermission("getWrapperPID"));
237 }
238
239 return WrapperSystemPropertyUtil.getIntProperty("wrapper.pid", 0);
240 }
241
242 protected void lazyInitWrapperManager() {
243 WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
244
245 if (wm != null)
246 {
247 return;
248 }
249
250 wm = new WrapperManager();
251 wrapperManagerRef.compareAndSet(null, wm);
252 }
253
254
255
256
257 protected void unregisterMBeansIfNecessary()
258 throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
259 if (mBeanServer == null || wrapperName == null)
260 {
261 return;
262 }
263 if (mBeanServer.isRegistered(wrapperName))
264 {
265 mBeanServer.unregisterMBean(wrapperName);
266 }
267 }
268
269
270
271
272 protected void unregisterMeQuietly()
273 {
274 try
275 {
276
277 muleContext.getRegistry().unregisterAgent(this.getName());
278 }
279 catch (MuleException e)
280 {
281
282 }
283 }
284 }