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