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 | 0 | 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 | 0 | private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance(); |
69 | 0 | private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport(); |
70 | |
|
71 | |
|
72 | 0 | private final AtomicReference wrapperManagerRef = new AtomicReference(); |
73 | |
|
74 | |
|
75 | |
public WrapperManagerAgent() |
76 | |
{ |
77 | 0 | super("wrapper-manager"); |
78 | 0 | } |
79 | |
|
80 | |
public void initialise() throws InitialisationException |
81 | |
{ |
82 | |
try |
83 | |
{ |
84 | 0 | List<?> servers = MBeanServerFactory.findMBeanServer(null); |
85 | 0 | if (servers.isEmpty()) |
86 | |
{ |
87 | 0 | throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this); |
88 | |
} |
89 | |
|
90 | 0 | mBeanServer = (MBeanServer) servers.get(0); |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
boolean launchedByWrapper; |
99 | 0 | if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null) |
100 | |
{ |
101 | 0 | launchedByWrapper = false; |
102 | |
} |
103 | |
|
104 | 0 | else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME))) |
105 | |
{ |
106 | 0 | 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 | 0 | unregisterMeQuietly(); |
110 | 0 | return; |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | 0 | lazyInitWrapperManager(); |
115 | 0 | launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper(); |
116 | |
} |
117 | |
|
118 | 0 | if (!launchedByWrapper) |
119 | |
{ |
120 | 0 | logger.info("This JVM hasn't been launched by the wrapper, the agent will not run."); |
121 | 0 | unregisterMeQuietly(); |
122 | 0 | return; |
123 | |
} |
124 | |
|
125 | 0 | final boolean containerMode = muleContext.getConfiguration().isContainerMode(); |
126 | 0 | if (containerMode) |
127 | |
{ |
128 | |
|
129 | 0 | wrapperName = jmxSupport.getObjectName(JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX + ":" + WRAPPER_JMX_NAME); |
130 | 0 | if (mBeanServer.isRegistered(wrapperName)) |
131 | |
{ |
132 | |
|
133 | 0 | return; |
134 | |
} |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | |
|
139 | 0 | wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_JMX_NAME); |
140 | |
} |
141 | |
|
142 | 0 | unregisterMBeansIfNecessary(); |
143 | 0 | mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName); |
144 | |
} |
145 | 0 | catch (InitialisationException iex) |
146 | |
{ |
147 | |
|
148 | 0 | throw iex; |
149 | |
} |
150 | 0 | catch (Exception e) |
151 | |
{ |
152 | 0 | throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this); |
153 | 0 | } |
154 | 0 | } |
155 | |
|
156 | |
public void start() throws MuleException |
157 | |
{ |
158 | |
|
159 | 0 | } |
160 | |
|
161 | |
public void stop() throws MuleException |
162 | |
{ |
163 | |
|
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
public void dispose() |
168 | |
{ |
169 | |
try |
170 | |
{ |
171 | 0 | unregisterMBeansIfNecessary(); |
172 | |
} |
173 | 0 | catch (Exception e) |
174 | |
{ |
175 | 0 | logger.error("Couldn't unregister MBean: " |
176 | |
+ (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e); |
177 | 0 | } |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
@Override |
187 | |
public String getDescription() |
188 | |
{ |
189 | 0 | WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get(); |
190 | 0 | if (wm == null) |
191 | |
{ |
192 | 0 | return "Wrapper Manager"; |
193 | |
} |
194 | |
else |
195 | |
{ |
196 | 0 | 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 | 0 | SecurityManager sm = System.getSecurityManager(); |
213 | 0 | if (sm != null) |
214 | |
{ |
215 | 0 | sm.checkPermission(new WrapperPermission("getJavaPID")); |
216 | |
} |
217 | |
|
218 | 0 | 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 | 0 | SecurityManager sm = System.getSecurityManager(); |
234 | 0 | if (sm != null) |
235 | |
{ |
236 | 0 | sm.checkPermission(new WrapperPermission("getWrapperPID")); |
237 | |
} |
238 | |
|
239 | 0 | return WrapperSystemPropertyUtil.getIntProperty("wrapper.pid", 0); |
240 | |
} |
241 | |
|
242 | |
protected void lazyInitWrapperManager() { |
243 | 0 | WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get(); |
244 | |
|
245 | 0 | if (wm != null) |
246 | |
{ |
247 | 0 | return; |
248 | |
} |
249 | |
|
250 | 0 | wm = new WrapperManager(); |
251 | 0 | wrapperManagerRef.compareAndSet(null, wm); |
252 | 0 | } |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
protected void unregisterMBeansIfNecessary() |
258 | |
throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException { |
259 | 0 | if (mBeanServer == null || wrapperName == null) |
260 | |
{ |
261 | 0 | return; |
262 | |
} |
263 | 0 | if (mBeanServer.isRegistered(wrapperName)) |
264 | |
{ |
265 | 0 | mBeanServer.unregisterMBean(wrapperName); |
266 | |
} |
267 | 0 | } |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
protected void unregisterMeQuietly() |
273 | |
{ |
274 | |
try |
275 | |
{ |
276 | |
|
277 | 0 | muleContext.getRegistry().unregisterAgent(this.getName()); |
278 | |
} |
279 | 0 | catch (MuleException e) |
280 | |
{ |
281 | |
|
282 | 0 | } |
283 | 0 | } |
284 | |
} |