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 import java.util.concurrent.atomic.AtomicReference;
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 org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.tanukisoftware.wrapper.WrapperSystemPropertyUtil;
35 import org.tanukisoftware.wrapper.jmx.WrapperManager;
36 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
37 import org.tanukisoftware.wrapper.security.WrapperPermission;
38
39
40
41
42
43
44 public class WrapperManagerAgent extends AbstractAgent
45 {
46
47
48
49 public static final String WRAPPER_JMX_NAME = "name=WrapperManager";
50
51
52
53
54
55 public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
56
57 private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
58
59
60
61
62 private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
63
64 private MBeanServer mBeanServer;
65 private ObjectName wrapperName;
66
67 private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
68 private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
69
70
71 private final AtomicReference<WrapperManagerMBean> wrapperManagerRef = new AtomicReference<WrapperManagerMBean>();
72
73
74 public WrapperManagerAgent()
75 {
76 super("wrapper-manager");
77 }
78
79 @Override
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 = 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 @Override
157 public void start() throws MuleException
158 {
159
160 }
161
162 @Override
163 public void stop() throws MuleException
164 {
165
166 }
167
168 @Override
169 public void dispose()
170 {
171 try
172 {
173 unregisterMBeansIfNecessary();
174 }
175 catch (Exception e)
176 {
177 logger.error("Couldn't unregister MBean: "
178 + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
179 }
180 }
181
182
183
184
185
186
187 @Override
188 public String getDescription()
189 {
190 WrapperManagerMBean wm = wrapperManagerRef.get();
191 if (wm == null)
192 {
193 return "Wrapper Manager";
194 }
195 else
196 {
197 return "Wrapper Manager: Mule PID #" + getJavaPID() + ", Wrapper PID #" + getWrapperPID();
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public static int getJavaPID()
212 {
213 SecurityManager sm = System.getSecurityManager();
214 if (sm != null)
215 {
216 sm.checkPermission(new WrapperPermission("getJavaPID"));
217 }
218
219 return WrapperSystemPropertyUtil.getIntProperty("wrapper.java.pid", 0);
220 }
221
222
223
224
225
226
227
228
229
230
231
232 public static int getWrapperPID()
233 {
234 SecurityManager sm = System.getSecurityManager();
235 if (sm != null)
236 {
237 sm.checkPermission(new WrapperPermission("getWrapperPID"));
238 }
239
240 return WrapperSystemPropertyUtil.getIntProperty("wrapper.pid", 0);
241 }
242
243 protected void lazyInitWrapperManager()
244 {
245 WrapperManagerMBean wm = wrapperManagerRef.get();
246
247 if (wm != null)
248 {
249 return;
250 }
251
252 wm = new WrapperManager();
253 wrapperManagerRef.compareAndSet(null, wm);
254 }
255
256
257
258
259 protected void unregisterMBeansIfNecessary()
260 throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
261 {
262 if (mBeanServer == null || wrapperName == null)
263 {
264 return;
265 }
266 if (mBeanServer.isRegistered(wrapperName))
267 {
268 mBeanServer.unregisterMBean(wrapperName);
269 }
270 }
271
272 }