KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > jmx > MBeanServerManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marc Wick.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.jmx;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MBeanServerFactory JavaDoc;
32 import javax.management.ObjectInstance JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import org.objectweb.cjdbc.common.i18n.Translate;
36 import org.objectweb.cjdbc.common.jmx.JmxException;
37 import org.objectweb.cjdbc.common.log.Trace;
38
39 /**
40  * The MBeanServerManager (Singleton) creates a single MBeanServer in an JVM.
41  * The server can be accessed with the getInstance() method.
42  * <p>
43  * The server is created with
44  * org.objectweb.cjdbc.controller.jmx.MBeanServerBuilder
45  *
46  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
47  * @version 1.0
48  */

49 public class MBeanServerManager
50 {
51
52   static Trace logger = Trace
53                                               .getLogger("org.objectweb.cjdbc.controller.jmx.MBeanServer");
54
55   private static MBeanServer JavaDoc mbs;
56   private static boolean isJmxEnabled = true;
57
58   /**
59    * creating a MBeanServer, if it does not exist, otherwise a reference to the
60    * MBeanServer is returned
61    *
62    * @return the mbeanserver instance, null if jmx is disabled
63    */

64   public static synchronized MBeanServer JavaDoc getInstance()
65   {
66
67     if (!isJmxEnabled)
68     {
69       return null;
70     }
71
72     if (mbs != null)
73     {
74       return mbs;
75     }
76
77     String JavaDoc defaultServerBuilder = System
78         .getProperty("javax.management.builder.initial");
79
80     if (!MBeanServerBuilder.class.getName().equals(defaultServerBuilder))
81     {
82       if (defaultServerBuilder != null)
83         logger.error("property javax.management.builder.initial was "
84             + defaultServerBuilder);
85
86       logger.debug("setting property javax.management.builder.initial");
87       System
88           .setProperty("javax.management.builder.initial",
89               org.objectweb.cjdbc.controller.jmx.MBeanServerBuilder.class
90                   .getName());
91
92     }
93
94     mbs = MBeanServerFactory.createMBeanServer();
95     return mbs;
96   }
97
98   /**
99    * Returns the isJmxEnabled value.
100    *
101    * @return Returns the isJmxEnabled.
102    */

103   public static boolean isJmxEnabled()
104   {
105     return isJmxEnabled;
106   }
107
108   /**
109    * enable or disable jmx
110    *
111    * @param isJmxEnabled The isJmxEnabled to set.
112    * @throws JmxException an exception
113    */

114   public static void setJmxEnabled(boolean isJmxEnabled) throws JmxException
115   {
116     if (MBeanServerManager.isJmxEnabled != isJmxEnabled && !isJmxEnabled && mbs!=null)
117     {
118       // stop rmi connectors
119
List JavaDoc list = RmiConnector.getRmiConnectors();
120       for (Iterator JavaDoc it = list.iterator(); it.hasNext();)
121       {
122         RmiConnector rmi = (RmiConnector) it.next();
123         rmi.stop();
124       }
125
126       // stop http adaptors
127
list = HttpAdaptor.getHttpAdaptors();
128       for (Iterator JavaDoc it = list.iterator(); it.hasNext();)
129       {
130         HttpAdaptor http = (HttpAdaptor) it.next();
131         http.stop();
132       }
133       // Stop mbean server
134
MBeanServerFactory.releaseMBeanServer(mbs);
135       mbs = null;
136     }
137     // set jmx enabled to its value
138
MBeanServerManager.isJmxEnabled = isJmxEnabled;
139   }
140
141   /**
142    * Registers an MBean with the MBean server if jmx is enabled, otherwise it
143    * returns null.
144    * <p>
145    * This method is equivalend to
146    *
147    * <pre>
148    * MBeanServer server = MBeanServerManager.getInstance();
149    * if (server != null)
150    * {
151    * server.registerMBean(object, name);
152    * }
153    * </pre>
154    *
155    * @param object The MBean to be registered as an MBean.
156    * @param name The object name of the MBean. May be null.
157    * @return An ObjectInstance, containing the ObjectName and the Java class
158    * name of the newly registered MBean. If the contained ObjectName is
159    * n, the contained Java class name is getMBeanInfo(n).getClassName().
160    * Or null if jmx is disabled
161    * @throws JmxException the object could not be registered
162    */

163   public static ObjectInstance JavaDoc registerMBean(Object JavaDoc object, ObjectName JavaDoc name)
164       throws JmxException
165   {
166     MBeanServer JavaDoc server = getInstance();
167     try
168     {
169
170       if (server != null)
171       {
172         logger.debug(Translate.get("jmx.register.mbean", new String JavaDoc[]{
173             object.getClass().toString(), name.getCanonicalName()}));
174
175         ObjectInstance JavaDoc objInstance = null;
176         if (!server.isRegistered(name))
177         {
178           objInstance = server.registerMBean(object, name);
179         }
180         else
181         {
182           logger.error(Translate.get("jmx.register.mbean.already.exist",
183               new String JavaDoc[]{name.getCanonicalName()}));
184           try
185           {
186             server.unregisterMBean(name);
187           }
188           catch (Exception JavaDoc e)
189           {
190             logger.error(Translate.get("jmx.delete.mbean.failed", new String JavaDoc[]{
191                 name.toString(), e.getMessage()}));
192           }
193           objInstance = server.registerMBean(object, name);
194         }
195
196         logger.debug(Translate.get("jmx.server.mbean.count", ""
197             + server.getMBeanCount()));
198         return objInstance;
199       }
200       return null;
201     }
202     catch (Exception JavaDoc e)
203     {
204       logger.error(Translate.get("jmx.register.mbean.failed",
205           new String JavaDoc[]{object.getClass().toString(), e.getMessage(),
206               e.getClass().toString()}));
207       e.printStackTrace();
208       e.getCause().printStackTrace();
209       throw new JmxException(e);
210     }
211   }
212
213   /**
214    * unregister an mbean.
215    *
216    * @param name the name of the bean to unregister
217    * @throws JmxException problems
218    */

219   public static void unregister(ObjectName JavaDoc name) throws JmxException
220   {
221     MBeanServer JavaDoc server = getInstance();
222     if (server != null)
223     {
224       try
225       {
226         // unregister the MBean
227
server.unregisterMBean(name);
228         logger.debug(Translate.get("jmx.server.mbean.count", ""
229             + server.getMBeanCount()));
230
231       }
232       catch (Exception JavaDoc e)
233       {
234         logger.error(Translate.get("jmx.register.mbean.failed", new String JavaDoc[]{
235             name.getCanonicalName(), e.getMessage()}));
236         throw new JmxException(e);
237       }
238     }
239   }
240
241 }
Popular Tags