KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > jmx > MBeanServerHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MBeanServerHelper.java 16 2006-02-21 18:52:14Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.jmx;
27
28 import java.util.List JavaDoc;
29
30 import javax.management.AttributeNotFoundException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MBeanServerFactory JavaDoc;
35 import javax.management.MalformedObjectNameException JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
38
39 /**
40  * Allow to start an MBean server and get an MBeanServer.
41  * @author Florent Benoit
42  */

43 public final class MBeanServerHelper {
44
45     /**
46      * MBeanServer.
47      */

48     private static MBeanServer JavaDoc mbeanServer = null;
49
50     /**
51      * Id of the generated MBeanServer.
52      */

53     private static String JavaDoc idMbeanServer = null;
54
55     /**
56      * Default domain name.
57      */

58     private static final String JavaDoc DEFAULT_DOMAIN_NAME = "EasyBeans";
59
60     /**
61      * Utility class, no public constructor.
62      */

63     private MBeanServerHelper() {
64
65     }
66
67     /**
68      * Gets first available MBean Server.
69      * @return first available MBean server.
70      * @throws JMXRemoteException if no server is available
71      */

72     protected static MBeanServer JavaDoc getMBeanServerServer() throws JMXRemoteException {
73         if (getInternalMBeanServer() != null) {
74             return getInternalMBeanServer();
75         }
76         throw new JMXRemoteException("No running MBeanServer was found.");
77     }
78
79     /**
80      * @return first MBean server found.
81      */

82     private static MBeanServer JavaDoc getInternalMBeanServer() {
83         List JavaDoc mbeanServers = MBeanServerFactory.findMBeanServer(null);
84         if (mbeanServers.size() > 0) {
85             return (MBeanServer JavaDoc) mbeanServers.get(0);
86         }
87         return null;
88     }
89
90 /**
91  * Starts an MBeanServer if no MBeanServer is available.
92  * @throws MBeanServerException if MBeanServer can't be started
93  */

94     public static synchronized void startMBeanServer() throws MBeanServerException {
95         mbeanServer = getInternalMBeanServer();
96         // Need to create one
97
if (mbeanServer == null) {
98         // Need to create an MBean server
99
mbeanServer = MBeanServerFactory.createMBeanServer(DEFAULT_DOMAIN_NAME);
100         }
101
102         ObjectName JavaDoc mbeanServerDelegate = null;
103         try {
104             mbeanServerDelegate = new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
105         } catch (MalformedObjectNameException JavaDoc e) {
106             throw new MBeanServerException("Cannot build an objectName", e);
107         } catch (NullPointerException JavaDoc e) {
108             throw new MBeanServerException("Cannot build an objectName", e);
109         }
110
111         try {
112             idMbeanServer = (String JavaDoc) mbeanServer.getAttribute(mbeanServerDelegate, "MBeanServerId");
113         } catch (AttributeNotFoundException JavaDoc e) {
114             throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e);
115         } catch (InstanceNotFoundException JavaDoc e) {
116             throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e);
117         } catch (MBeanException JavaDoc e) {
118             throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e);
119         } catch (ReflectionException JavaDoc e) {
120             throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e);
121         }
122     }
123
124     /**
125      * @return the id of the created MbeanServer.
126      */

127     protected static String JavaDoc getIdMbeanServer() {
128         return idMbeanServer;
129     }
130
131 }
132
Popular Tags