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