View Javadoc

1   /*
2    * $Id: WrapperManagerAgent.java 7976 2007-08-21 14:26:13Z 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.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   * 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 implements UMOAgent {
44      /**
45       * MBean name to register under.
46       */
47      public static final String WRAPPER_OBJECT_NAME = "name=WrapperManager";
48  
49      /**
50       * For cases when Mule is embedded in another process and that external process
51       * had registered the MBean. 
52       */
53      public static final String DEFAULT_WRAPPER_MBEAN_NAME = "org.tanukisoftware.wrapper:type=WrapperManager";
54  
55      private static final Log logger = LogFactory.getLog(WrapperManagerAgent.class);
56  
57      /**
58       * This property is set by the native launcher, used for extra checks.
59       */
60      private static final String WRAPPER_SYSTEM_PROPERTY_NAME = "wrapper.native_library";
61  
62      private String name = "Wrapper Manager";
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      /* @see org.mule.umo.lifecycle.Initialisable#initialise() */
74      public void initialise() throws InitialisationException {
75  
76          try
77          {
78              final List servers = MBeanServerFactory.findMBeanServer(null);
79              if (servers.isEmpty())
80              {
81                  throw new InitialisationException(ManagementMessages.noMBeanServerAvailable(), this);
82              }
83  
84              mBeanServer = (MBeanServer) servers.get(0);
85  
86              /*
87                Perform an extra check ourselves. If 'wrapper.native_library' property has
88                not been set, which is the case for embedded scenarios, don't even try to
89                construct the wrapper manager bean, as it performs a number of checks internally
90                and outputs a very verbose warning.
91              */
92              boolean launchedByWrapper;
93              if (System.getProperty(WRAPPER_SYSTEM_PROPERTY_NAME) == null)
94              {
95                  launchedByWrapper = false;
96              }
97              // Check if an external process registered a wrapper MBean under default name.
98              else if (mBeanServer.isRegistered(jmxSupport.getObjectName(DEFAULT_WRAPPER_MBEAN_NAME)))
99              {
100                 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                 unregisterMeQuietly();
104                 return;
105             }
106             else
107             {
108                 lazyInitWrapperManager();
109                 launchedByWrapper = ((WrapperManagerMBean) wrapperManagerRef.get()).isControlledByNativeWrapper();
110             }
111 
112             if (!launchedByWrapper)
113             {
114                 logger.info("This JVM hasn't been launched by the wrapper, the agent will not run.");
115                 unregisterMeQuietly();
116                 return;
117             }
118 
119             wrapperName = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":" + WRAPPER_OBJECT_NAME);
120 
121             unregisterMBeansIfNecessary();
122             mBeanServer.registerMBean(wrapperManagerRef.get(), wrapperName);
123         }
124         catch (InitialisationException iex)
125         {
126             // rethrow
127             throw iex;
128         }
129         catch (Exception e)
130         {
131             throw new InitialisationException(
132                 CoreMessages.failedToStart("wrapper agent"), e, this);
133         }
134     }
135 
136     /* @see org.mule.umo.lifecycle.Startable#start() */
137     public void start() throws UMOException {
138         // no-op
139     }
140 
141     /* @see org.mule.umo.lifecycle.Stoppable#stop() */
142     public void stop() throws UMOException
143     {
144         // no-op
145     }
146 
147     /* @see org.mule.umo.lifecycle.Disposable#dispose() */
148     public void dispose()
149     {
150         try
151         {
152             unregisterMBeansIfNecessary();
153         }
154         catch (Exception e)
155         {
156             logger.error("Couldn't unregister MBean: "
157                          + (wrapperName != null ? wrapperName.getCanonicalName() : "null"), e);
158         }
159     }
160 
161     /* @see org.mule.umo.manager.UMOAgent#registered() */
162     public void registered()
163     {
164         // nothing to do
165     }
166 
167     /* @see org.mule.umo.manager.UMOAgent#unregistered() */
168     public void unregistered()
169     {
170         // nothing to do
171     }
172 
173     // /////////////////////////////////////////////////////////////////////////
174     // Getters and setters
175     // /////////////////////////////////////////////////////////////////////////
176 
177     /* @see org.mule.umo.manager.UMOAgent#getDescription() */
178     public String getDescription()
179     {
180         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
181         if (wm == null)
182         {
183             return "Wrapper Manager";
184         }
185         else return "Wrapper Manager: Mule PID #" + wm.getJavaPID() +
186                 ", Wrapper PID #" + wm.getWrapperPID();
187     }
188 
189     /* @see org.mule.umo.manager.UMOAgent#getName() */
190     public String getName()
191     {
192         return this.name;
193     }
194 
195     /* @see org.mule.umo.manager.UMOAgent#setName(java.lang.String) */
196     public void setName(String name)
197     {
198         this.name = name;
199     }
200 
201     protected void lazyInitWrapperManager() {
202         WrapperManagerMBean wm = (WrapperManagerMBean) wrapperManagerRef.get();
203 
204         if (wm != null)
205         {
206             return;
207         }
208 
209         wm = new WrapperManager();
210         wrapperManagerRef.compareAndSet(null, wm);
211     }
212 
213     /**
214      * Unregister all MBeans if there are any left over the old deployment
215      */
216     protected void unregisterMBeansIfNecessary()
217         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
218         if (mBeanServer == null || wrapperName == null)
219         {
220             return;
221         }
222         if (mBeanServer.isRegistered(wrapperName))
223         {
224             mBeanServer.unregisterMBean(wrapperName);
225         }
226     }
227 
228     /**
229      * Quietly unregister ourselves.
230      */
231     protected void unregisterMeQuietly()
232     {
233         try
234         {
235             // remove the agent from the list, it's not functional
236             MuleManager.getInstance().unregisterAgent(this.getName());
237         }
238         catch (UMOException e)
239         {
240             // not interested, really
241         }
242     }
243 
244 }