View Javadoc

1   /*
2    * $Id: WrapperManagerAgent.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * This agent integrates Java Service Wrapper into Mule. See
42   * <a href="http://wrapper.tanukisoftware.org">http://wrapper.tanukisoftware.org</a>
43   * for more details.
44   */
45  public class WrapperManagerAgent extends AbstractAgent
46  {
47      /**
48       * MBean name to register under.
49       */
50      public static final String WRAPPER_JMX_NAME = "name=WrapperManager";
51  
52      /**
53       * For cases when Mule is embedded in another process and that external process
54       * had registered the MBean.
55       */
56      public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
57  
58      private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
59  
60      /**
61       * This property is set by the native launcher, used for extra checks.
62       */
63      private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
64  
65      private MBeanServer mBeanServer;
66      private ObjectName wrapperName;
67  
68      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
69      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
70  
71      // atomic reference to avoid unnecessary construction calls
72      private final AtomicReference/*<WrapperManagerMBean>*/ wrapperManagerRef = new AtomicReference();
73  
74  
75      public WrapperManagerAgent()
76      {
77          super("wrapper-manager");
78      }
79  
80      public void initialise() throws InitialisationException
81      {
82          try
83          {
84              List<?> servers = MBeanServerFactory.findMBeanServer(null);
85              if (servers.isEmpty())
86              {
87                  throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
88              }
89  
90              mBeanServer = (MBeanServer) servers.get(0);
91  
92              /*
93                Perform an extra check ourselves. If 'wrapper.native_library' property has
94                not been set, which is the case for embedded scenarios, don't even try to
95                construct the wrapper manager bean, as it performs a number of checks internally
96                and outputs a very verbose warning.
97              */
98              boolean launchedByWrapper;
99              if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
100             {
101                 launchedByWrapper = false;
102             }
103             // Check if an external process registered a wrapper MBean under default name.
104             else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
105             {
106                 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                 unregisterMeQuietly();
110                 return;
111             }
112             else
113             {
114                 lazyInitWrapperManager();
115                 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
116             }
117 
118             if (!launchedByWrapper)
119             {
120                 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
121                 unregisterMeQuietly();
122                 return;
123             }
124 
125             final boolean containerMode = muleContext.getConfiguration().isContainerMode();
126             if (containerMode)
127             {
128                 // container mode, register mbean under Mule domain, no duplicates under each app's domain
129                 wrapperName = jmxSupport.getObjectName(JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX + ":" + WRAPPER_JMX_NAME);
130                 if (mBeanServer.isRegistered(wrapperName))
131                 {
132                     // ignore duplicate invocations when running in Mule container mode
133                     return;
134                 }
135             }
136             else
137             {
138                 // embedded case, use Mule's single domain
139                 wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_JMX_NAME);
140             }
141 
142             unregisterMBeansIfNecessary();
143             mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
144         }
145         catch (InitialisationException iex)
146         {
147             // rethrow
148             throw iex;
149         }
150         catch (Exception e)
151         {
152             throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
153         }
154     }
155 
156     public void start() throws MuleException
157     {
158         // nothing to do
159     }
160 
161     public void stop() throws MuleException
162     {
163         // nothing to do
164     }
165 
166     /* @see org.mule.api.lifecycle.Disposable#dispose() */
167     public void dispose()
168     {
169         try
170         {
171             unregisterMBeansIfNecessary();
172         }
173         catch (Exception e)
174         {
175             logger.error("Couldn't unregister MBean: "
176                          + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
177         }
178     }
179 
180 
181     // /////////////////////////////////////////////////////////////////////////
182     // Getters and setters
183     // /////////////////////////////////////////////////////////////////////////
184 
185     /* @see org.mule.api.context.Agent#getDescription() */
186     @Override
187     public String getDescription()
188     {
189         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
190         if (wm == null)
191         {
192             return "Wrapper Manager";
193         }
194         else
195         {
196             return "Wrapper Manager: Mule PID #" + getJavaPID() + ", Wrapper PID #" + getWrapperPID();
197         }
198     }
199 
200     /**
201      * This method is a copy of the implementation of
202      * {@link WrapperManagerMBean#getJavaPID()} and it is here because that method is
203      * not present in the {@link WrapperManagerMBean} until version 3.2.3.
204      * SpringSource's TC Server uses The wrapper version 3.2.0 so having this method
205      * here allows us to be compatible with TC Server.
206      * 
207      * @return The PID of the Java process.
208      * @see http://www.mulesoft.org/jira/browse/MULE-5106
209      */
210     public static int getJavaPID()
211     {
212         SecurityManager sm = System.getSecurityManager();
213         if (sm != null)
214         {
215             sm.checkPermission(new WrapperPermission("getJavaPID"));
216         }
217 
218         return WrapperSystemPropertyUtil.getIntProperty("wrapper.java.pid", 0);
219     }
220 
221     /**
222      * This method is a copy of the implementation of
223      * {@link WrapperManagerMBean#getWrapperPID()} and it is here because that method
224      * is not present in the {@link WrapperManagerMBean} until version 3.2.3.
225      * SpringSource's TC Server uses The wrapper version 3.2.0 so having this method
226      * here allows us to be compatible with TC Server.
227      * 
228      * @return The PID of the Wrapper process.
229      * @see http://www.mulesoft.org/jira/browse/MULE-5106
230      */
231     public static int getWrapperPID()
232     {
233         SecurityManager sm = System.getSecurityManager();
234         if (sm != null)
235         {
236             sm.checkPermission(new WrapperPermission("getWrapperPID"));
237         }
238 
239         return WrapperSystemPropertyUtil.getIntProperty("wrapper.pid", 0);
240     }
241 
242     protected void lazyInitWrapperManager() {
243         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
244 
245         if (wm != null)
246         {
247             return;
248         }
249 
250         wm = new WrapperManager();
251         wrapperManagerRef.compareAndSet(null, wm);
252     }
253 
254     /**
255      * Unregister all MBeans if there are any left over the old deployment
256      */
257     protected void unregisterMBeansIfNecessary()
258         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
259         if (mBeanServer == null || wrapperName == null)
260         {
261             return;
262         }
263         if (mBeanServer.isRegistered(wrapperName))
264         {
265             mBeanServer.unregisterMBean(wrapperName);
266         }
267     }
268 
269     /**
270      * Quietly unregister ourselves.
271      */
272     protected void unregisterMeQuietly()
273     {
274         try
275         {
276             // remove the agent from the list, it's not functional
277             muleContext.getRegistry().unregisterAgent(this.getName());
278         }
279         catch (MuleException e)
280         {
281             // not interested, really
282         }
283     }
284 }