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