001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)MBeanUtils.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.util.jmx;
030:
031: import com.sun.jbi.util.LocalStringKeys;
032: import com.sun.jbi.util.StringTranslator;
033:
034: import javax.management.MBeanServer;
035: import javax.management.ObjectName;
036: import javax.management.StandardMBean;
037:
038: /**
039: * Helper class for frequently used MBean housekeeping. This is a static
040: * class that must have a reference to the MBean server currently being used.
041: * Early in the initialization of the JBI framework, the MBean server reference
042: * is set by the framework, before any other JBI services are initialized. This
043: * ensures that the MBean server reference will always be available. While not
044: * an ideal design, in the context of the JBI runtime, this is a workable
045: * solution and prevents every caller from having to have a reference to the
046: * MBean server.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class MBeanUtils {
051: /**
052: * Local handle to MBean Server.
053: */
054: private static MBeanServer sMBeanServer;
055:
056: /**
057: * Local handle to StringTranslator.
058: */
059: private static StringTranslator sTranslator;
060:
061: /**
062: * Initialize the static data. This method is called by the framework
063: * during initialization so that all other code can use the other methods
064: * in the class at any time. This is also used by the ProxyBinding as it
065: * has its own classloader so it needs to initialize the same static data.
066: * @param server The MBean Server reference to be used.
067: */
068: public static void init(MBeanServer server) {
069: if (null == server) {
070: throw new java.lang.IllegalArgumentException(sTranslator
071: .getString(LocalStringKeys.NULL_ARGUMENT, "server"));
072: }
073:
074: sMBeanServer = server;
075: sTranslator = new StringTranslator("com.sun.jbi.util", null);
076: }
077:
078: /**
079: * Create and register a standard MBean with the main MBean Server.
080: * @param interfaceClass the MBean interface implemented by the instance.
081: * @param instance the MBean implementation instance.
082: * @param mbeanName the JMX ObjectName for the MBean.
083: * @param replace indicates whether an existing registration should be
084: * replaced or cause an error. If true, an existing registration should be
085: * replaced; if false, an existing registration should cause an error.
086: * @throws javax.jbi.JBIException If the MBean creation or registration
087: * fails.
088: */
089: public static void registerStandardMBean(Class interfaceClass,
090: Object instance, ObjectName mbeanName, boolean replace)
091: throws javax.jbi.JBIException {
092: if (null == sMBeanServer) {
093: return;
094: }
095:
096: // Create a StandardMBean for the MBean instance.
097:
098: StandardMBean mbean = null;
099: try {
100: mbean = new StandardMBean(instance, interfaceClass);
101: } catch (javax.management.NotCompliantMBeanException ncEx) {
102: throw new javax.jbi.JBIException(sTranslator.getString(
103: LocalStringKeys.MBEAN_CREATION_NOT_JMX_COMPLIANT,
104: mbeanName, instance.getClass().getName()));
105: }
106:
107: // If an existing registration should be replaced, remove any existing
108: // registration first.
109:
110: if (replace) {
111: if (sMBeanServer.isRegistered(mbeanName)) {
112: try {
113: sMBeanServer.unregisterMBean(mbeanName);
114: } catch (javax.management.InstanceNotFoundException infEx) {
115: ; // Just ignore this error
116: } catch (javax.management.MBeanRegistrationException mbrEx) {
117: String msg = mbrEx.getMessage();
118: Throwable cause = mbrEx.getCause();
119: String causeName = cause.getClass().getName();
120: throw new javax.jbi.JBIException(
121: sTranslator
122: .getString(
123: LocalStringKeys.MBEAN_UNREGISTRATION_EXCEPTION,
124: mbeanName, causeName,
125: null == msg ? "" : msg),
126: mbrEx);
127: }
128: }
129: }
130:
131: // Register the MBean.
132:
133: try {
134: sMBeanServer.registerMBean(mbean, mbeanName);
135: } catch (javax.management.InstanceAlreadyExistsException iaeEx) {
136: throw new javax.jbi.JBIException(sTranslator
137: .getString(
138: LocalStringKeys.MBEAN_ALREADY_REGISTERED,
139: mbeanName));
140: } catch (javax.management.MBeanRegistrationException mbrEx) {
141: String msg = mbrEx.getMessage();
142: Throwable cause = mbrEx.getCause();
143: String causeName = cause.getClass().getName();
144: throw new javax.jbi.JBIException(sTranslator.getString(
145: LocalStringKeys.MBEAN_REGISTRATION_EXCEPTION,
146: mbeanName, causeName, null == msg ? "" : msg),
147: mbrEx);
148: } catch (javax.management.NotCompliantMBeanException ncEx) {
149: throw new javax.jbi.JBIException(
150: sTranslator
151: .getString(
152: LocalStringKeys.MBEAN_REGISTRATION_NOT_JMX_COMPLIANT,
153: mbeanName, instance.getClass()
154: .getName()));
155: }
156: }
157:
158: /**
159: * Unregister an MBean from the MBean server.
160: * @param mbeanName the JMX ObjectName for the MBean.
161: * @throws javax.jbi.JBIException if the unregistration fails.
162: */
163: public static void unregisterMBean(ObjectName mbeanName)
164: throws javax.jbi.JBIException {
165: if (null == sMBeanServer) {
166: return;
167: }
168:
169: // Unregister the MBean
170:
171: try {
172: sMBeanServer.unregisterMBean(mbeanName);
173: } catch (javax.management.InstanceNotFoundException infEx) {
174: ; // Just ignore this error
175: } catch (javax.management.MBeanRegistrationException mbrEx) {
176: String msg = mbrEx.getMessage();
177: Throwable cause = mbrEx.getCause();
178: String causeName = cause.getClass().getName();
179: throw new javax.jbi.JBIException(sTranslator.getString(
180: LocalStringKeys.MBEAN_UNREGISTRATION_EXCEPTION,
181: mbeanName, causeName, null == msg ? "" : msg),
182: mbrEx);
183: }
184: }
185:
186: }
|