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