View Javadoc

1   /*
2    * $Id: WrapperManagerAgent.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.jmx.WrapperManager;
36  import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
37  
38  /**
39   * This agent integrates Java Service Wrapper into Mule. See
40   * <a href="http://wrapper.tanukisoftware.org">http://wrapper.tanukisoftware.org</a>
41   * for more details.
42   */
43  public class WrapperManagerAgent extends AbstractAgent
44  {
45      /**
46       * MBean name to register under.
47       */
48      public static final String WRAPPER_OBJECT_NAME = "name=WrapperManager";
49  
50      /**
51       * For cases when Mule is embedded in another process and that external process
52       * had registered the MBean.
53       */
54      public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
55  
56      private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
57  
58      /**
59       * This property is set by the native launcher, used for extra checks.
60       */
61      private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
62  
63      private MBeanServer mBeanServer;
64      private ObjectName wrapperName;
65  
66      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
67      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
68  
69      // atomic reference to avoid unnecessary construction calls
70      private final AtomicReference/*<WrapperManagerMBean>*/ wrapperManagerRef = new AtomicReference();
71  
72  
73      public WrapperManagerAgent()
74      {
75          super("Wrapper Manager");
76      }
77  
78      public void initialise() throws InitialisationException
79      {
80          try
81          {
82              final List servers = MBeanServerFactory.findMBeanServer(null);
83              if (servers.isEmpty())
84              {
85                  throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
86              }
87  
88              mBeanServer = (MBeanServer) servers.get(0);
89  
90              /*
91                Perform an extra check ourselves. If 'wrapper.native_library' property has
92                not been set, which is the case for embedded scenarios, don't even try to
93                construct the wrapper manager bean, as it performs a number of checks internally
94                and outputs a very verbose warning.
95              */
96              boolean launchedByWrapper;
97              if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
98              {
99                  launchedByWrapper = false;
100             }
101             // Check if an external process registered a wrapper MBean under default name.
102             else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
103             {
104                 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                 unregisterMeQuietly();
108                 return;
109             }
110             else
111             {
112                 lazyInitWrapperManager();
113                 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
114             }
115 
116             if (!launchedByWrapper)
117             {
118                 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
119                 unregisterMeQuietly();
120                 return;
121             }
122 
123             wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName(muleContext) + ":" + WRAPPER_OBJECT_NAME);
124 
125             unregisterMBeansIfNecessary();
126             mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
127         }
128         catch (InitialisationException iex)
129         {
130             // rethrow
131             throw iex;
132         }
133         catch (Exception e)
134         {
135             throw new InitialisationException(CoreMessages.failedToStart("wrapper agent"), e, this);
136         }
137     }
138 
139     public void start() throws MuleException 
140     {
141         // nothing to do
142     }
143 
144     public void stop() throws MuleException
145     {
146         // nothing to do
147     }
148 
149     /* @see org.mule.api.lifecycle.Disposable#dispose() */
150     public void dispose()
151     {
152         try
153         {
154             unregisterMBeansIfNecessary();
155         }
156         catch (Exception e)
157         {
158             logger.error("Couldn't unregister MBean: "
159                          + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
160         }
161     }
162 
163     /* @see org.mule.api.context.Agent#registered() */
164     public void registered()
165     {
166         // nothing to do
167     }
168 
169     /* @see org.mule.api.context.Agent#unregistered() */
170     public void unregistered()
171     {
172         // nothing to do
173     }
174 
175     // /////////////////////////////////////////////////////////////////////////
176     // Getters and setters
177     // /////////////////////////////////////////////////////////////////////////
178 
179     /* @see org.mule.api.context.Agent#getDescription() */
180     public String getDescription()
181     {
182         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
183         if (wm == null)
184         {
185             return "Wrapper Manager";
186         }
187         else return "Wrapper Manager: Mule PID #" + wm.getJavaPID() +
188                 ", Wrapper PID #" + wm.getWrapperPID();
189     }
190 
191     protected void lazyInitWrapperManager() {
192         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
193 
194         if (wm != null)
195         {
196             return;
197         }
198 
199         wm = new WrapperManager();
200         wrapperManagerRef.compareAndSet(null, wm);
201     }
202 
203     /**
204      * Unregister all MBeans if there are any left over the old deployment
205      */
206     protected void unregisterMBeansIfNecessary()
207         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
208         if (mBeanServer == null || wrapperName == null)
209         {
210             return;
211         }
212         if (mBeanServer.isRegistered(wrapperName))
213         {
214             mBeanServer.unregisterMBean(wrapperName);
215         }
216     }
217 
218     /**
219      * Quietly unregister ourselves.
220      */
221     protected void unregisterMeQuietly()
222     {
223         try
224         {
225             // remove the agent from the list, it's not functional
226             muleContext.getRegistry().unregisterAgent(this.getName());
227         }
228         catch (MuleException e)
229         {
230             // not interested, really
231         }
232     }
233 
234 }