KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > mbean > MBeanUtils


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
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: MBeanUtils.java,v 1.2 2005/04/28 11:37:26 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.util.mbean;
26
27 import java.util.List JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.apache.commons.logging.Log;
35
36 import org.objectweb.carol.util.configuration.ConfigurationException;
37 import org.objectweb.carol.util.configuration.ProtocolConfigurationImplMBean;
38
39 /**
40  * This class is used to manage mbean registration.
41  * All MBean stuff should go here, all imports on javax.management are in this class.
42  * So if carol is not initialized with MBeanServer, JMX jars are not required at runtime.
43  * @author Florent Benoit
44  */

45 public class MBeanUtils {
46
47     /**
48      * MBeanServer
49      */

50     private static MBeanServer JavaDoc mbeanServer = null;
51
52     /**
53      * Utility class, no constructor
54      */

55     private MBeanUtils() {
56
57     }
58
59     /**
60      * Init MBeanServer
61      * @param logger the logger to use to log messages
62      * @param idMbeanServer the MBeanServer ID
63      * @throws ConfigurationException if the registration failed
64      */

65     protected static void initMBeanServer(Log logger, String JavaDoc idMbeanServer) throws ConfigurationException {
66         List JavaDoc mbeanServers = MBeanServerFactory.findMBeanServer(idMbeanServer);
67         if (mbeanServers.size() == 0) {
68             throw new ConfigurationException("No MBean Servers were found with id '" + idMbeanServer + "'");
69         }
70         if (mbeanServers.size() > 1) {
71             if (logger.isDebugEnabled()) {
72                 logger.debug("Take first MBeanServer of the list");
73             }
74         }
75         mbeanServer = (MBeanServer JavaDoc) mbeanServers.get(0);
76     }
77
78
79     /**
80      * Register a ProtocolConfiguration object in MBeanServer
81      * @param protocolConfiguration the configuration to register
82      * @param logger the logger to use to log messages
83      * @param idMbeanServer the MBeanServer ID
84      * @param serverName the name of the server (for ObjectName)
85      * @throws ConfigurationException if the registration failed
86      */

87     public static void registerProtocolConfigurationMBean(ProtocolConfigurationImplMBean protocolConfiguration, Log logger, String JavaDoc idMbeanServer, String JavaDoc serverName) throws ConfigurationException {
88
89         // get MBeanServer if not present
90
if (mbeanServer == null) {
91             initMBeanServer(logger, idMbeanServer);
92         }
93         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(mbeanServer.getDefaultDomain());
94         sb.append(":j2eeType=JNDIResource");
95         sb.append(",name=");
96         sb.append(protocolConfiguration.getName());
97         sb.append(",J2EEServer=");
98         sb.append(serverName);
99         ObjectName JavaDoc on = null;
100         try {
101             on = new ObjectName JavaDoc(sb.toString());
102         } catch (MalformedObjectNameException JavaDoc e) {
103             throw new ConfigurationException("Cannot build ObjectName for configuration '" + protocolConfiguration.getName() + "'", e);
104         }
105
106         // Set the objectname
107
protocolConfiguration.setobjectName(on.toString());
108
109         try {
110             mbeanServer.registerMBean(protocolConfiguration, on);
111         } catch (Exception JavaDoc e) {
112             throw new ConfigurationException("Cannot register MBean '" + on + "' in MBeanServer", e);
113         }
114
115     }
116
117 }
118
Popular Tags