Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

MBeanServerManager.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Marc Wick.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.jmx;
00026 
00027 import java.util.Iterator;
00028 import java.util.List;
00029 
00030 import javax.management.MBeanServer;
00031 import javax.management.MBeanServerFactory;
00032 import javax.management.ObjectInstance;
00033 import javax.management.ObjectName;
00034 
00035 import org.objectweb.cjdbc.common.i18n.Translate;
00036 import org.objectweb.cjdbc.common.jmx.JmxException;
00037 import org.objectweb.cjdbc.common.log.Trace;
00038 
00039 /**
00040  * The MBeanServerManager (Singleton) creates a single MBeanServer in an JVM.
00041  * The server can be accessed with the getInstance() method.
00042  * <p>
00043  * The server is created with
00044  * org.objectweb.cjdbc.controller.jmx.MBeanServerBuilder
00045  * 
00046  * @author <a href="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
00047  * @version 1.0
00048  */
00049 public class MBeanServerManager
00050 {
00051 
00052   static Trace               logger       = Trace
00053                                               .getLogger("org.objectweb.cjdbc.controller.jmx.MBeanServer");
00054 
00055   private static MBeanServer mbs;
00056   private static boolean     isJmxEnabled = true;
00057 
00058   /**
00059    * creating a MBeanServer, if it does not exist, otherwise a reference to the
00060    * MBeanServer is returned
00061    * 
00062    * @return the mbeanserver instance, null if jmx is disabled
00063    */
00064   public static synchronized MBeanServer getInstance()
00065   {
00066 
00067     if (!isJmxEnabled)
00068     {
00069       return null;
00070     }
00071 
00072     if (mbs != null)
00073     {
00074       return mbs;
00075     }
00076 
00077     String defaultServerBuilder = System
00078         .getProperty("javax.management.builder.initial");
00079 
00080     if (!MBeanServerBuilder.class.getName().equals(defaultServerBuilder))
00081     {
00082       if (defaultServerBuilder != null)
00083         logger.error("property javax.management.builder.initial was "
00084             + defaultServerBuilder);
00085 
00086       logger.debug("setting property javax.management.builder.initial");
00087       System
00088           .setProperty("javax.management.builder.initial",
00089               org.objectweb.cjdbc.controller.jmx.MBeanServerBuilder.class
00090                   .getName());
00091 
00092     }
00093 
00094     mbs = MBeanServerFactory.createMBeanServer();
00095     return mbs;
00096   }
00097 
00098   /**
00099    * Returns the isJmxEnabled value.
00100    * 
00101    * @return Returns the isJmxEnabled.
00102    */
00103   public static boolean isJmxEnabled()
00104   {
00105     return isJmxEnabled;
00106   }
00107 
00108   /**
00109    * enable or disable jmx
00110    * 
00111    * @param isJmxEnabled The isJmxEnabled to set.
00112    * @throws JmxException an exception
00113    */
00114   public static void setJmxEnabled(boolean isJmxEnabled) throws JmxException
00115   {
00116     if (MBeanServerManager.isJmxEnabled != isJmxEnabled && !isJmxEnabled && mbs!=null)
00117     {
00118       // stop rmi connectors
00119       List list = RmiConnector.getRmiConnectors();
00120       for (Iterator it = list.iterator(); it.hasNext();)
00121       {
00122         RmiConnector rmi = (RmiConnector) it.next();
00123         rmi.stop();
00124       }
00125 
00126       // stop http adaptors
00127       list = HttpAdaptor.getHttpAdaptors();
00128       for (Iterator it = list.iterator(); it.hasNext();)
00129       {
00130         HttpAdaptor http = (HttpAdaptor) it.next();
00131         http.stop();
00132       }
00133       // Stop mbean server
00134       MBeanServerFactory.releaseMBeanServer(mbs);
00135       mbs = null;
00136     }
00137     // set jmx enabled to its value
00138     MBeanServerManager.isJmxEnabled = isJmxEnabled;
00139   }
00140 
00141   /**
00142    * Registers an MBean with the MBean server if jmx is enabled, otherwise it
00143    * returns null.
00144    * <p>
00145    * This method is equivalend to
00146    * 
00147    * <pre>
00148    * MBeanServer server = MBeanServerManager.getInstance();
00149    * if (server != null)
00150    * {
00151    *   server.registerMBean(object, name);
00152    * }
00153    * </pre>
00154    * 
00155    * @param object The MBean to be registered as an MBean.
00156    * @param name The object name of the MBean. May be null.
00157    * @return An ObjectInstance, containing the ObjectName and the Java class
00158    *         name of the newly registered MBean. If the contained ObjectName is
00159    *         n, the contained Java class name is getMBeanInfo(n).getClassName().
00160    *         Or null if jmx is disabled
00161    * @throws JmxException the object could not be registered
00162    */
00163   public static ObjectInstance registerMBean(Object object, ObjectName name)
00164       throws JmxException
00165   {
00166     MBeanServer server = getInstance();
00167     try
00168     {
00169 
00170       if (server != null)
00171       {
00172         logger.debug(Translate.get("jmx.register.mbean", new String[]{
00173             object.getClass().toString(), name.getCanonicalName()}));
00174 
00175         ObjectInstance objInstance = null;
00176         if (!server.isRegistered(name))
00177         {
00178           objInstance = server.registerMBean(object, name);
00179         }
00180         else
00181         {
00182           logger.error(Translate.get("jmx.register.mbean.already.exist",
00183               new String[]{name.getCanonicalName()}));
00184           try
00185           {
00186             server.unregisterMBean(name);
00187           }
00188           catch (Exception e)
00189           {
00190             logger.error(Translate.get("jmx.delete.mbean.failed", new String[]{
00191                 name.toString(), e.getMessage()}));
00192           }
00193           objInstance = server.registerMBean(object, name);
00194         }
00195 
00196         logger.debug(Translate.get("jmx.server.mbean.count", ""
00197             + server.getMBeanCount()));
00198         return objInstance;
00199       }
00200       return null;
00201     }
00202     catch (Exception e)
00203     {
00204       logger.error(Translate.get("jmx.register.mbean.failed",
00205           new String[]{object.getClass().toString(), e.getMessage(),
00206               e.getClass().toString()}));
00207       e.printStackTrace();
00208       e.getCause().printStackTrace();
00209       throw new JmxException(e);
00210     }
00211   }
00212 
00213   /**
00214    * unregister an mbean.
00215    * 
00216    * @param name the name of the bean to unregister
00217    * @throws JmxException problems
00218    */
00219   public static void unregister(ObjectName name) throws JmxException
00220   {
00221     MBeanServer server = getInstance();
00222     if (server != null)
00223     {
00224       try
00225       {
00226         // unregister the MBean
00227         server.unregisterMBean(name);
00228         logger.debug(Translate.get("jmx.server.mbean.count", ""
00229             + server.getMBeanCount()));
00230 
00231       }
00232       catch (Exception e)
00233       {
00234         logger.error(Translate.get("jmx.register.mbean.failed", new String[]{
00235             name.getCanonicalName(), e.getMessage()}));
00236         throw new JmxException(e);
00237       }
00238     }
00239   }
00240 
00241 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1