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: * @(#)MBeanSet.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.support;
030:
031: import com.sun.jbi.EnvironmentContext;
032: import com.sun.jbi.StringTranslator;
033: import com.sun.jbi.util.EnvironmentAccess;
034: import com.sun.jbi.management.LocalStringKeys;
035:
036: import javax.management.MBeanServer;
037: import javax.management.StandardMBean;
038: import javax.management.InstanceNotFoundException;
039: import java.util.logging.Logger;
040: import java.util.HashMap;
041: import java.util.Iterator;
042: import javax.management.ObjectName;
043:
044: /**
045: * Implementation of MBeanSet interface.
046: *
047: * @author Sun Microsystems, Inc.
048: */
049: public class MBeanSet implements com.sun.jbi.management.MBeanSet {
050:
051: /** Handle to the main MBean server */
052: private final MBeanServer mMBeanServer;
053:
054: /** Handle to the logger where this class will log errors. */
055: private final Logger mLogger;
056:
057: /** Handle to StringTranslator for message translation */
058: private StringTranslator mTranslator = null;
059:
060: /** Storage for our MBeanSet:*/
061: private final HashMap mStorage;
062:
063: /** the null mbean object name: */
064: private static ObjectName nullObjectName = null;
065:
066: /** the null entry: */
067: private static Entry nullEntry = null;
068:
069: /**
070: * Constructs a <CODE>MBeanSet</CODE>.
071: * @param aMBeanServer is the MBean server provided by the framework.
072: * @param aLogger is where this class should log messages.
073: */
074: public MBeanSet(MBeanServer aMBeanServer, Logger aLogger) {
075: mMBeanServer = aMBeanServer;
076: mLogger = aLogger;
077: mStorage = new HashMap();
078:
079: EnvironmentContext ctx = null;
080:
081: try {
082: ctx = EnvironmentAccess.getContext();
083: } catch (NoClassDefFoundError e) {
084: // just leave ctx null; this class won't log errors.
085: }
086:
087: if (null != ctx) {
088: mTranslator = ctx
089: .getStringTranslator("com.sun.jbi.management");
090: }
091:
092: if ((null == mMBeanServer) && (null != mTranslator)) {
093: String warningMsg = mTranslator
094: .getString(LocalStringKeys.MBEANSET_MBEANSERVER_NULL_OP);
095:
096: mLogger.warning(warningMsg);
097: }
098:
099: if (null == nullObjectName) {
100: try {
101: nullObjectName = new ObjectName(
102: "NULL_JMX_DOMAIN:NULL_ATTRIBUTE_KEY=NULL_ATTRIBUTE_VALUE");
103: } catch (Exception e) {
104: if (null != mTranslator) {
105: String severeMsg = mTranslator.getString(
106: LocalStringKeys.MBEANSET_OBJECTNAME_NULL, e
107: .toString());
108: mLogger.severe(severeMsg);
109: }
110: }
111: }
112:
113: if (null == nullEntry) {
114: nullEntry = new Entry(nullObjectName, null, null, false);
115: }
116: }
117:
118: /** private inner class to implement MBeanSet entries: */
119: private class Entry {
120: public final ObjectName mMBeanName; //repeated for convenience
121: public final Class mMBeanDefinition;
122: public final Object mMBeanImplementation;
123: public final boolean mIsEmitter; //true if mbean is to emit notifications
124:
125: /** constructor: */
126: Entry(ObjectName theMBeanName, Class theMBeanDefinition,
127: Object theMBeanImplementation, boolean isEmitter) {
128: if (null == theMBeanName) {
129: theMBeanName = nullObjectName;
130: }
131:
132: mMBeanName = theMBeanName;
133: mMBeanDefinition = theMBeanDefinition;
134: mMBeanImplementation = theMBeanImplementation;
135: mIsEmitter = isEmitter; //true if mbean is to emit notifications
136: }
137:
138: /**
139: * toString - create a string representation of a MBeanSet Entry.
140: * @return String representing the Entry.
141: */
142: public String toString() {
143: return (mMBeanName.toString()
144: + "\t"
145: + ((mMBeanDefinition == null) ? "NULL_DEFINITION"
146: : mMBeanDefinition.toString()) + "\t" + ((mMBeanImplementation == null) ? "NULL_IMPLEMENTATION"
147: : mMBeanImplementation.toString()));
148: }
149: }
150:
151: /**
152: * Add an Standard MBean to the Set that can potentially emit JMX notifications.
153: *
154: * @param theMBeanName - the object name of the mbean to add.
155: * @param theMBeanDefinition - the interface that defines the MBean.
156: * @param theMBeanImplementation - an instance of the MBean implementation class.
157: * @param isEmitter - true if this MBean wants to emit jmx notifications.
158: *
159: * @return true if mbean is added to the set, otherwise false.
160: */
161: public boolean add(ObjectName theMBeanName,
162: Class theMBeanDefinition, Object theMBeanImplementation,
163: boolean isEmitter) {
164: if (null == theMBeanName) {
165: //map null object names to a dummy entry:
166: mStorage.put(nullObjectName, nullEntry);
167: } else {
168: Entry ee = new Entry(theMBeanName, theMBeanDefinition,
169: theMBeanImplementation, isEmitter);
170: mStorage.put(theMBeanName, ee);
171: }
172: return true;
173: }
174:
175: /**
176: * Add an Standard MBean to the Set that will not emit notifications.
177: *
178: * @param theMBeanName - the object name of the mbean to add.
179: * @param theMBeanDefinition - the interface that defines the MBean.
180: * @param theMBeanImplementation - an instance of the MBean implementation class.
181: *
182: * @return true if mbean is added to the set, otherwise false.
183: */
184: public boolean add(ObjectName theMBeanName,
185: Class theMBeanDefinition, Object theMBeanImplementation) {
186: return add(theMBeanName, theMBeanDefinition,
187: theMBeanImplementation, false);
188: }
189:
190: /**
191: * Replace a Standard MBean in the Set that can potentially emit JMX notifications.
192: *
193: * @param theMBeanName - the object name of the mbean to replace.
194: * @param theMBeanDefinition - the interface that defines the MBean.
195: * @param theMBeanImplementation - an instance of the MBean implementation class.
196: * @param isEmitter - true if this MBean wants to emit jmx notifications.
197: *
198: * @return true if mbean is replaced in the set, otherwise false.
199: */
200: public boolean replace(ObjectName theMBeanName,
201: Class theMBeanDefinition, Object theMBeanImplementation,
202: boolean isEmitter) {
203: return add(theMBeanName, theMBeanDefinition,
204: theMBeanImplementation, isEmitter);
205: }
206:
207: /**
208: * Replace a Standard MBean in the Set.
209: *
210: * @param theMBeanName - the object name of the mbean to replace.
211: * @param theMBeanDefinition - the interface that defines the MBean.
212: * @param theMBeanImplementation - an instance of the MBean implementation class.
213: *
214: * @return true if mbean is replaced in the set, otherwise false.
215: */
216: public boolean replace(ObjectName theMBeanName,
217: Class theMBeanDefinition, Object theMBeanImplementation) {
218: return add(theMBeanName, theMBeanDefinition,
219: theMBeanImplementation, false);
220: }
221:
222: /**
223: * Delete an MBean from an existing MBean Set.
224: *
225: * @param theMBeanName - the object name of the mbean to delete.
226: *
227: * @return true if the MBean is deleted from the set, otherwise false.
228: */
229: public boolean delete(ObjectName theMBeanName) {
230: if (null == theMBeanName) {
231: //map null object names to a dummy entry:
232: theMBeanName = nullObjectName;
233: }
234:
235: //true if we delete it:
236: return (mStorage.remove(theMBeanName) != null);
237: }
238:
239: /**
240: * True if an MBean exists in the named MBean Set.
241: *
242: * @param theMBeanName - the object name of the mbean to check.
243: *
244: * @return true if the MBean exists in the set, otherwise false.
245: */
246: public boolean exists(ObjectName theMBeanName) {
247: if (null == theMBeanName) {
248: //map null object names to a dummy entry:
249: theMBeanName = nullObjectName;
250: }
251:
252: return (mStorage.containsKey(theMBeanName));
253: }
254:
255: /**
256: * True if an MBean is part of this set and is registered.
257: *
258: * @param theMBeanName - the object name of the mbean to check.
259: *
260: * @return true if the MBean exists in the set, otherwise false.
261: */
262: public boolean isRegistered(ObjectName theMBeanName) {
263: if (null == mMBeanServer || null == theMBeanName) {
264: return false;
265: }
266:
267: if (theMBeanName.toString() == nullObjectName.toString()) {
268: return false;
269: }
270:
271: return mMBeanServer.isRegistered(theMBeanName);
272: }
273:
274: /**
275: * Register all of the MBean's in an MBean Set.
276: *
277: * Okay if some are already registered.
278: * Ignores null entries.
279: *
280: * To re-register, you must unregister the named object before
281: * registering the set.
282: *
283: * @return true iff all mbeans are now registered.
284: */
285: public boolean register() {
286: if ((null == mMBeanServer) && (null != mTranslator)) {
287: String warningMsg = mTranslator
288: .getString(LocalStringKeys.MBEANSET_MBEANSERVER_NULL_REG);
289:
290: mLogger.warning(warningMsg);
291: return false;
292: }
293:
294: int nerrors = 0;
295:
296: //foreach key...
297: Iterator ii = mStorage.keySet().iterator();
298: while (ii.hasNext()) {
299: ObjectName mbn = (ObjectName) ii.next();
300: Entry ee = (Entry) mStorage.get(mbn);
301:
302: //register as standard mbean if not already registered:
303: if (!isRegistered(mbn) && ee != nullEntry) {
304: try {
305: StandardMBean mbean = null;
306: if (ee.mIsEmitter) {
307: mbean = new NotifyStandardMBean(
308: ee.mMBeanImplementation,
309: ee.mMBeanDefinition);
310: } else {
311: if (ee.mMBeanImplementation instanceof StandardMBean) {
312: mbean = (StandardMBean) ee.mMBeanImplementation;
313: } else {
314: mbean = new StandardMBean(
315: ee.mMBeanImplementation,
316: ee.mMBeanDefinition);
317: }
318: }
319: mMBeanServer.registerMBean(mbean, mbn);
320: } catch (Exception e) {
321: /* throws:
322: * InstanceAlreadyExistsException,
323: * MBeanRegistrationException,
324: * NotCompliantMBeanException
325: */
326: e.printStackTrace();
327: ++nerrors;
328: }
329: }
330: }
331:
332: return (0 == nerrors);
333: }
334:
335: /**
336: * Unregister all of the MBean's in an MBean Set.
337: *
338: * It is not an error to unregister an MBean that is not registered.
339: *
340: * @return true iff all mbeans are unregistered.
341: */
342: public boolean unregister() {
343: if ((null == mMBeanServer) && (null != mTranslator)) {
344: String warningMsg = mTranslator
345: .getString(LocalStringKeys.MBEANSET_MBEANSERVER_NULL_UNREG);
346:
347: mLogger.warning(warningMsg);
348: return false;
349: }
350:
351: int nerrors = 0;
352:
353: //foreach key...
354: Iterator ii = mStorage.keySet().iterator();
355: while (ii.hasNext()) {
356: ObjectName mbn = (ObjectName) ii.next();
357: Entry ee = (Entry) mStorage.get(mbn);
358:
359: //unregister as standard mbean if not already registered:
360: if (isRegistered(mbn) && ee != nullEntry) {
361: try {
362: mMBeanServer.unregisterMBean(mbn);
363: } catch (InstanceNotFoundException infe) {
364: //ignore
365: } catch (Exception e) {
366: /* throws:
367: * InstanceAlreadyExistsException,
368: * MBeanRegistrationException,
369: * NotCompliantMBeanException
370: */
371: e.printStackTrace();
372: ++nerrors;
373: }
374: }
375: }
376:
377: return (0 == nerrors);
378: }
379:
380: /**
381: * dump contents of an MBeanSet to the log.
382: * @param aTitle - the title of the dump.
383: */
384: public void dump(String aTitle) {
385: String txt = aTitle;
386:
387: //foreach key...
388: Iterator ii = mStorage.keySet().iterator();
389: while (ii.hasNext()) {
390: ObjectName mbn = (ObjectName) ii.next();
391: Entry ee = (Entry) mStorage.get(mbn);
392:
393: txt += "\n" + ee.toString();
394: }
395:
396: mLogger.info(txt);
397: }
398:
399: }
|