001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.modelmbean;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ObjectStreamField;
028: import java.io.Serializable;
029: import java.io.StreamCorruptedException;
030: import java.util.ArrayList;
031: import java.util.HashMap;
032: import java.util.List;
033: import java.util.Map;
034: import javax.management.Descriptor;
035: import javax.management.MBeanAttributeInfo;
036: import javax.management.MBeanConstructorInfo;
037: import javax.management.MBeanException;
038: import javax.management.MBeanInfo;
039: import javax.management.MBeanNotificationInfo;
040: import javax.management.MBeanOperationInfo;
041: import javax.management.RuntimeOperationsException;
042:
043: import org.jboss.mx.modelmbean.ModelMBeanConstants;
044: import org.jboss.mx.util.Serialization;
045:
046: /**
047: * Support class for <tt>ModelMBeanInfo</tt> interface.
048: *
049: * @see javax.management.modelmbean.ModelMBeanInfo
050: *
051: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
052: * @author <a href="mailto:adrian.brock@happeningtimes.com">Adrian Brock</a>.
053: * @author <a href="mailto:thomas.diesler@jboss.com">Thomas Diesler</a>.
054: * @version $Revision: 57200 $
055: *
056: */
057: public class ModelMBeanInfoSupport extends MBeanInfo implements
058: ModelMBeanInfo, Serializable {
059: // Constants -----------------------------------------------------
060:
061: // Attributes ----------------------------------------------------
062:
063: /**
064: * MBean descriptor for this Model MBean.
065: */
066: private Descriptor mbeanDescriptor = null;
067:
068: // Static --------------------------------------------------------
069:
070: private static final long serialVersionUID;
071: private static final ObjectStreamField[] serialPersistentFields;
072:
073: private ModelMBeanAttributeInfo[] modelAttributes = null;
074: private ModelMBeanConstructorInfo[] modelConstructors = null;
075: private ModelMBeanOperationInfo[] modelOperations = null;
076: private ModelMBeanNotificationInfo[] modelNotifications = null;
077:
078: static {
079: switch (Serialization.version) {
080: case Serialization.V1R0:
081: serialVersionUID = -3944083498453227709L;
082: // REVIEW: This is not in the spec, constructed from exceptions in testing
083: serialPersistentFields = new ObjectStreamField[] {
084: new ObjectStreamField("mmbAttributes",
085: new MBeanAttributeInfo[0].getClass()),
086: new ObjectStreamField("mmbConstructors",
087: new MBeanConstructorInfo[0].getClass()),
088: new ObjectStreamField("mmbNotifications",
089: new MBeanNotificationInfo[0].getClass()),
090: new ObjectStreamField("mmbOperations",
091: new MBeanOperationInfo[0].getClass()),
092: new ObjectStreamField("modelMBeanDescriptor",
093: Descriptor.class) };
094: break;
095: default:
096: serialVersionUID = -1935722590756516193L;
097: serialPersistentFields = new ObjectStreamField[] {
098: new ObjectStreamField("modelMBeanAttributes",
099: new MBeanAttributeInfo[0].getClass()),
100: new ObjectStreamField("modelMBeanConstructors",
101: new MBeanConstructorInfo[0].getClass()),
102: new ObjectStreamField("modelMBeanNotifications",
103: new MBeanNotificationInfo[0].getClass()),
104: new ObjectStreamField("modelMBeanOperations",
105: new MBeanOperationInfo[0].getClass()),
106: new ObjectStreamField("modelMBeanDescriptor",
107: Descriptor.class) };
108: }
109: }
110:
111: // Constructors --------------------------------------------------
112:
113: /**
114: * Copy constructor for Model MBean info. This instance is initialized with
115: * the values of the given Model MBean info.
116: *
117: * @param mbi Model MBean info used to initialize this instance
118: */
119: public ModelMBeanInfoSupport(ModelMBeanInfo mbi) {
120: super (mbi.getClassName(), mbi.getDescription(), mbi
121: .getAttributes(), mbi.getConstructors(), mbi
122: .getOperations(), mbi.getNotifications());
123: modelAttributes = (ModelMBeanAttributeInfo[]) mbi
124: .getAttributes();
125: modelConstructors = (ModelMBeanConstructorInfo[]) mbi
126: .getConstructors();
127: modelOperations = (ModelMBeanOperationInfo[]) mbi
128: .getOperations();
129: modelNotifications = (ModelMBeanNotificationInfo[]) mbi
130: .getNotifications();
131: try {
132: setMBeanDescriptor(mbi.getMBeanDescriptor());
133: } catch (MBeanException e) {
134: throw new RuntimeException("Cannot set MBean descriptor", e);
135: }
136: }
137:
138: /**
139: * Creates an instance of Model MBean info implementation based on the given
140: * values. The Model MBean is configured with a default MBean descriptor.
141: *
142: * @param className name of the Model MBean implementation class
143: * @param description human readable description string for this Model MBean
144: * @param modelAttributes an array of Model MBean attribute metadata to describe
145: * the management modelAttributes of this Model MBean
146: * @param modelConstructors an array of Model MBean constructor metadata that
147: * describes the modelConstructors of this Model MBean
148: * implementation class
149: * @param modelOperations an array of Model MBean operation metadata to describe
150: * the management modelOperations of this Model MBean
151: * @param modelNotifications an array of Model MBean notification metadata to
152: * describe the management modelNotifications of this
153: * Model MBean
154: */
155: public ModelMBeanInfoSupport(String className, String description,
156: ModelMBeanAttributeInfo[] modelAttributes,
157: ModelMBeanConstructorInfo[] modelConstructors,
158: ModelMBeanOperationInfo[] modelOperations,
159: ModelMBeanNotificationInfo[] modelNotifications) {
160: super (
161: className,
162: description,
163: (null == modelAttributes) ? new ModelMBeanAttributeInfo[0]
164: : modelAttributes,
165: (null == modelConstructors) ? new ModelMBeanConstructorInfo[0]
166: : modelConstructors,
167: (null == modelOperations) ? new ModelMBeanOperationInfo[0]
168: : modelOperations,
169: (null == modelNotifications) ? new ModelMBeanNotificationInfo[0]
170: : modelNotifications);
171:
172: this .modelAttributes = (ModelMBeanAttributeInfo[]) super
173: .getAttributes();
174: this .modelConstructors = (ModelMBeanConstructorInfo[]) super
175: .getConstructors();
176: this .modelOperations = (ModelMBeanOperationInfo[]) super
177: .getOperations();
178: this .modelNotifications = (ModelMBeanNotificationInfo[]) super
179: .getNotifications();
180: try {
181: setMBeanDescriptor(createDefaultDescriptor());
182: } catch (MBeanException e) {
183: throw new RuntimeException("Cannot set MBean descriptor", e);
184: }
185: }
186:
187: /**
188: * Creates an instance of Model MBean info implementation based on the given
189: * values and descriptor.
190: *
191: * @param className name of the Model MBean implementation class
192: * @param description human readable description string for this Model MBean
193: * @param modelAttributes an array of Model MBean attribute metadata to describe
194: * the management modelAttributes of this Model MBean
195: * @param modelConstructors an array of Model MBean constructor metadata that
196: * describes the modelConstructors of this Model MBean
197: * implementation class
198: * @param modelOperations an array of Model MBean operation metadata to describe
199: * the management modelOperations of this Model MBean
200: * @param modelNotifications an array of Model MBean notification metadata to
201: * describe the management modelNotifications of this
202: * Model MBean
203: * @param mbeandescriptor descriptor for the MBean
204: */
205: public ModelMBeanInfoSupport(String className, String description,
206: ModelMBeanAttributeInfo[] modelAttributes,
207: ModelMBeanConstructorInfo[] modelConstructors,
208: ModelMBeanOperationInfo[] modelOperations,
209: ModelMBeanNotificationInfo[] modelNotifications,
210: Descriptor mbeandescriptor)
211: throws RuntimeOperationsException {
212: super (
213: className,
214: description,
215: (null == modelAttributes) ? new ModelMBeanAttributeInfo[0]
216: : modelAttributes,
217: (null == modelConstructors) ? new ModelMBeanConstructorInfo[0]
218: : modelConstructors,
219: (null == modelOperations) ? new ModelMBeanOperationInfo[0]
220: : modelOperations,
221: (null == modelNotifications) ? new ModelMBeanNotificationInfo[0]
222: : modelNotifications);
223: this .modelAttributes = (ModelMBeanAttributeInfo[]) super
224: .getAttributes();
225: this .modelConstructors = (ModelMBeanConstructorInfo[]) super
226: .getConstructors();
227: this .modelOperations = (ModelMBeanOperationInfo[]) super
228: .getOperations();
229: this .modelNotifications = (ModelMBeanNotificationInfo[]) super
230: .getNotifications();
231: try {
232: setMBeanDescriptor(mbeandescriptor);
233: } catch (MBeanException e) {
234: throw new RuntimeException("Cannot set MBean descriptor", e);
235: }
236: }
237:
238: // ModelMBeanInfo interface implementation -----------------------
239:
240: /**
241: * Returns the descriptors of an Model MBean for a given management
242: * interface element type. The descriptor type must be one of the following: <br><pre>
243: *
244: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#MBEAN_DESCRIPTOR MBEAN_DESCRIPTOR}
245: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#ATTRIBUTE_DESCRIPTOR ATTRIBUTE_DESCRIPTOR}
246: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#OPERATION_DESCRIPTOR OPERATION_DESCRIPTOR}
247: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#NOTIFICATION_DESCRIPTOR NOTIFICATION_DESCRIPTOR}
248: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#CONSTRUCTOR_DESCRIPTOR CONSTRUCTOR_DESCRIPTOR}
249: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#ALL_DESCRIPTORS ALL_DESCRIPTORS}
250: *
251: * </pre>
252: *
253: * Using <tt>ALL_DESCRIPTORS</tt> returns descriptors for the MBean, and all
254: * its modelAttributes, modelOperations, modelNotifications and modelConstructors.
255: *
256: * @param descrType descriptor type string
257: *
258: * @return MBean descriptors.
259: */
260: public Descriptor[] getDescriptors(String descrType)
261: throws MBeanException {
262: if (descrType == null) {
263: List list = new ArrayList(100);
264: list.add(mbeanDescriptor);
265: list.addAll(getAttributeDescriptors().values());
266: list.addAll(getOperationDescriptors().values());
267: list.addAll(getNotificationDescriptors().values());
268: list.addAll(getConstructorDescriptors().values());
269: return (Descriptor[]) list.toArray(new Descriptor[0]);
270: }
271:
272: else if (descrType
273: .equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR))
274: return new Descriptor[] { mbeanDescriptor };
275:
276: else if (descrType
277: .equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR))
278: return (Descriptor[]) getAttributeDescriptors().values()
279: .toArray(new Descriptor[0]);
280:
281: else if (descrType
282: .equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR))
283: return (Descriptor[]) getOperationDescriptors().values()
284: .toArray(new Descriptor[0]);
285:
286: else if (descrType
287: .equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR))
288: return (Descriptor[]) getNotificationDescriptors().values()
289: .toArray(new Descriptor[0]);
290:
291: else if (descrType
292: .equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR))
293: return (Descriptor[]) getConstructorDescriptors().values()
294: .toArray(new Descriptor[0]);
295:
296: throw new IllegalArgumentException("unknown descriptor type: "
297: + descrType);
298: }
299:
300: /**
301: * Returns a descriptor of a management interface element matching the given
302: * name and type. The descriptor type string must be one of the following: <br><pre>
303: *
304: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#MBEAN_DESCRIPTOR MBEAN_DESCRIPTOR}
305: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#ATTRIBUTE_DESCRIPTOR ATTRIBUTE_DESCRIPTOR}
306: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#OPERATION_DESCRIPTOR OPERATION_DESCRIPTOR}
307: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#NOTIFICATION_DESCRIPTOR NOTIFICATION_DESCRIPTOR}
308: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#CONSTRUCTOR_DESCRIPTOR CONSTRUCTOR_DESCRIPTOR}
309: *
310: * </pre>
311: *
312: * @param descrName name of the descriptor
313: * @param descrType type of the descriptor
314: *
315: * @return the requested descriptor or <tt>null</tt> if it was not found
316: *
317: * @throws RuntimeOperationsException if an illegal descriptor type was given
318: */
319: public Descriptor getDescriptor(String descrName, String descrType)
320: throws MBeanException {
321: if (descrType == null)
322: throw new RuntimeOperationsException(
323: new IllegalArgumentException("null descriptor type"));
324:
325: if (descrType
326: .equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR))
327: return mbeanDescriptor;
328: else if (descrType
329: .equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR))
330: return getAttributeDescriptor(descrName);
331: else if (descrType
332: .equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR))
333: return getOperationDescriptor(descrName);
334: else if (descrType
335: .equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR))
336: return getConstructorDescriptor(descrName);
337: else if (descrType
338: .equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR))
339: return getNotificationDescriptor(descrName);
340:
341: throw new RuntimeOperationsException(
342: new IllegalArgumentException(
343: "unknown descriptor type: " + descrType));
344: }
345:
346: /**
347: * Adds or replaces the descriptors in this Model MBean. All descriptors
348: * must be valid. <tt>Null</tt> references will be ignored.
349: *
350: * @param inDescriptors array of descriptors
351: */
352: public void setDescriptors(Descriptor[] inDescriptors)
353: throws MBeanException {
354: for (int i = 0; i < inDescriptors.length; ++i) {
355: if (inDescriptors[i] != null && inDescriptors[i].isValid()) {
356: setDescriptor(
357: inDescriptors[i],
358: (String) inDescriptors[i]
359: .getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE));
360: }
361: }
362: }
363:
364: /**
365: * Adds or replaces the descriptor in this Model MBean. Descriptor must be
366: * valid. If <tt>descrType</tt> is not specified, the <tt>descriptorType</tt>
367: * field of the given descriptor is used. <p>
368: *
369: * The <tt>descriptorType</tt> must contain one of the following values: <br><pre>
370: *
371: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#MBEAN_DESCRIPTOR MBEAN_DESCRIPTOR}
372: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#ATTRIBUTE_DESCRIPTOR ATTRIBUTE_DESCRIPTOR}
373: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#OPERATION_DESCRIPTOR OPERATION_DESCRIPTOR}
374: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#NOTIFICATION_DESCRIPTOR NOTIFICATION_DESCRIPTOR}
375: * - {@link org.jboss.mx.modelmbean.ModelMBeanConstants#CONSTRUCTOR_DESCRIPTOR CONSTRUCTOR_DESCRIPTOR}
376: *
377: * </pre>
378: *
379: * @param descr descriptor to set
380: * @param descrType descriptor type string, can be <tt>null</tt>
381: *
382: * @throws RuntimeOperationsException if <tt>descr</tt> is <tt>null</tt>, or
383: * descriptor is not valid.
384: */
385: public void setDescriptor(Descriptor descr, String descrType)
386: throws MBeanException {
387: if (descr == null)
388: throw new RuntimeOperationsException(
389: new IllegalArgumentException("null descriptor"));
390:
391: if (!descr.isValid())
392: throw new RuntimeOperationsException(
393: new IllegalArgumentException(
394: "not a valid descriptor"));
395:
396: if (descrType == null)
397: descrType = (String) descr
398: .getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE);
399:
400: if (descrType
401: .equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR)) {
402: setMBeanDescriptor(descr);
403: } else if (descrType
404: .equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR)) {
405: ModelMBeanAttributeInfo info = getAttribute((String) descr
406: .getFieldValue(ModelMBeanConstants.NAME));
407: info.setDescriptor(descr);
408: } else if (descrType
409: .equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR)) {
410: ModelMBeanOperationInfo info = getOperation((String) descr
411: .getFieldValue(ModelMBeanConstants.NAME));
412: info.setDescriptor(descr);
413: } else if (descrType
414: .equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR)) {
415: ModelMBeanConstructorInfo info = getConstructor((String) descr
416: .getFieldValue(ModelMBeanConstants.NAME));
417: info.setDescriptor(descr);
418: } else if (descrType
419: .equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR)) {
420: ModelMBeanNotificationInfo info = getNotification((String) descr
421: .getFieldValue(ModelMBeanConstants.NAME));
422: info.setDescriptor(descr);
423: } else
424: throw new RuntimeOperationsException(
425: new IllegalArgumentException(
426: "unknown descriptor type: " + descrType));
427: }
428:
429: /**
430: * Returns the attribute info for the named attribute, or null if there is none.
431: */
432: public ModelMBeanAttributeInfo getAttribute(String inName)
433: throws MBeanException {
434: if (inName == null)
435: throw new RuntimeOperationsException(
436: new IllegalArgumentException("Null attribute name"));
437:
438: for (int i = 0; i < modelAttributes.length; ++i) {
439: if (modelAttributes[i].getName().equals(inName))
440: return modelAttributes[i];
441: }
442:
443: return null;
444: }
445:
446: /**
447: * Returns the operation info for the named attribute, or null if there is none.
448: */
449: public ModelMBeanOperationInfo getOperation(String inName)
450: throws MBeanException {
451: if (inName == null)
452: throw new RuntimeOperationsException(
453: new IllegalArgumentException("Null operation name"));
454:
455: for (int i = 0; i < modelOperations.length; ++i)
456: if (modelOperations[i].getName().equals(inName))
457: return modelOperations[i];
458:
459: return null;
460: }
461:
462: /**
463: * Returns the constructor info for the named attribute, or null if there is none.
464: */
465: public ModelMBeanConstructorInfo getConstructor(String inName)
466: throws MBeanException {
467: if (inName == null)
468: throw new RuntimeOperationsException(
469: new IllegalArgumentException(
470: "Null constructor name"));
471:
472: for (int i = 0; i < modelConstructors.length; ++i)
473: if (modelConstructors[i].getName().equals(inName))
474: return modelConstructors[i];
475:
476: return null;
477: }
478:
479: /**
480: * Returns the attribute info for the named attribute, or null if there is none.
481: */
482: public ModelMBeanNotificationInfo getNotification(String inName)
483: throws MBeanException {
484: if (inName == null)
485: throw new RuntimeOperationsException(
486: new IllegalArgumentException(
487: "Null notification name"));
488:
489: for (int i = 0; i < modelNotifications.length; ++i)
490: if (modelNotifications[i].getName().equals(inName))
491: return modelNotifications[i];
492:
493: return null;
494: }
495:
496: public MBeanAttributeInfo[] getAttributes() {
497: return super .getAttributes();
498: }
499:
500: public MBeanOperationInfo[] getOperations() {
501: return super .getOperations();
502: }
503:
504: public MBeanConstructorInfo[] getConstructors() {
505: return super .getConstructors();
506: }
507:
508: public MBeanNotificationInfo[] getNotifications() {
509: return super .getNotifications();
510: }
511:
512: public Descriptor getMBeanDescriptor() throws MBeanException {
513: return mbeanDescriptor;
514: }
515:
516: /**
517: * Sets the ModelMBean's descriptor. This descriptor contains default, MBean wide metadata about the MBean and
518: * default policies for persistence and caching. This operation does a complete replacement of the descriptor,
519: * no merging is done.
520: *
521: * If the descriptor to set to is null then the default descriptor will be created.
522: *
523: * The default descriptor is: name=mbeanName,descriptorType=mbean, displayName=this.getClassName(), persistPolicy=never,log=F,export=F,visiblity=1
524: * If the descriptor does not contain all these fields, they will be added with these default values.
525: * See getMBeanDescriptor method javadoc for description of valid field names.
526: */
527: public void setMBeanDescriptor(Descriptor inDescriptor)
528: throws MBeanException, RuntimeOperationsException {
529: if (inDescriptor == null)
530: inDescriptor = createDefaultDescriptor();
531:
532: if (inDescriptor.isValid()
533: && isMBeanDescriptorValid(inDescriptor)) {
534: addDefaultMBeanDescriptorFields(inDescriptor);
535: this .mbeanDescriptor = inDescriptor;
536: }
537: }
538:
539: /**
540: * Validate the descriptor in the context of an attribute
541: */
542: private boolean isMBeanDescriptorValid(Descriptor inDescriptor) {
543: String name = (String) inDescriptor
544: .getFieldValue(ModelMBeanConstants.NAME);
545: if (name == null)
546: throw new RuntimeOperationsException(
547: new IllegalArgumentException("Invalid null name"));
548:
549: String descriptorType = (String) inDescriptor
550: .getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE);
551: if (ModelMBeanConstants.MBEAN_DESCRIPTOR
552: .equalsIgnoreCase(descriptorType) == false)
553: throw new RuntimeOperationsException(
554: new IllegalArgumentException(
555: "Invalid descriptorType, for mbean '"
556: + name
557: + "' expected 'MBean' but got: "
558: + descriptorType));
559:
560: return true;
561: }
562:
563: // Public --------------------------------------------------------
564:
565: /**
566: * @deprecated use {@link #getDescriptor(String, String)} instead.
567: */
568: public Descriptor getDescriptor(String descrName)
569: throws MBeanException {
570:
571: /*
572: * NOTE: this method is not part of the ModelMBeanInfo interface but is
573: * included in the RI javadocs so it is also here for the sake
574: * of completeness. The problem here though is that this method
575: * to work without the descriptor type string assumes unique name
576: * for all descriptors regardless their type (something that is
577: * not mandated by the spec). Hence the deprecated tag. [JPL]
578: */
579: if (descrName.equals(mbeanDescriptor
580: .getFieldValue(ModelMBeanConstants.NAME)))
581: return mbeanDescriptor;
582:
583: Descriptor descr = null;
584:
585: descr = (Descriptor) getAttributeDescriptors().get(descrName);
586: if (descr != null)
587: return descr;
588:
589: descr = (Descriptor) getOperationDescriptors().get(descrName);
590: if (descr != null)
591: return descr;
592:
593: descr = (Descriptor) getNotificationDescriptors()
594: .get(descrName);
595: if (descr != null)
596: return descr;
597:
598: descr = (Descriptor) getConstructorDescriptors().get(descrName);
599: if (descr != null)
600: return descr;
601:
602: return null;
603: }
604:
605: // Y overrides ---------------------------------------------------
606: public synchronized Object clone() {
607: ModelMBeanInfoSupport clone = (ModelMBeanInfoSupport) super
608: .clone();
609: clone.mbeanDescriptor = (Descriptor) mbeanDescriptor.clone();
610: return clone;
611: }
612:
613: // Private -------------------------------------------------------
614: private void addDefaultMBeanDescriptorFields(Descriptor descr) {
615: if (descr.getFieldValue(ModelMBeanConstants.NAME) == null
616: || descr.getFieldValue(ModelMBeanConstants.NAME)
617: .equals(""))
618: descr.setField(ModelMBeanConstants.NAME, getClassName());
619: if (descr.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE) == null)
620: descr.setField(ModelMBeanConstants.DESCRIPTOR_TYPE,
621: ModelMBeanConstants.MBEAN_DESCRIPTOR);
622: if (!(((String) descr
623: .getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE))
624: .equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR)))
625: descr.setField(ModelMBeanConstants.DESCRIPTOR_TYPE,
626: ModelMBeanConstants.MBEAN_DESCRIPTOR);
627: if (descr.getFieldValue(ModelMBeanConstants.DISPLAY_NAME) == null)
628: descr.setField(ModelMBeanConstants.DISPLAY_NAME,
629: getClassName());
630: if (descr.getFieldValue(ModelMBeanConstants.PERSIST_POLICY) == null)
631: descr.setField(ModelMBeanConstants.PERSIST_POLICY,
632: ModelMBeanConstants.PP_NEVER);
633: if (descr.getFieldValue(ModelMBeanConstants.LOG) == null)
634: descr.setField(ModelMBeanConstants.LOG, "F");
635: if (descr.getFieldValue(ModelMBeanConstants.VISIBILITY) == null)
636: descr.setField(ModelMBeanConstants.VISIBILITY,
637: ModelMBeanConstants.HIGH_VISIBILITY);
638: }
639:
640: /**
641: * The default descriptor is:
642: * name=mbeanName
643: * descriptorType=mbean
644: * displayName=this.getClassName()
645: * persistPolicy=never
646: * log=F
647: * visiblity=1
648: */
649: private Descriptor createDefaultDescriptor() {
650: return new DescriptorSupport(
651: new String[] {
652: ModelMBeanConstants.NAME + "=" + getClassName(),
653: ModelMBeanConstants.DESCRIPTOR_TYPE + "="
654: + ModelMBeanConstants.MBEAN_DESCRIPTOR,
655: ModelMBeanConstants.DISPLAY_NAME + "="
656: + getClassName(),
657: ModelMBeanConstants.PERSIST_POLICY + "="
658: + ModelMBeanConstants.PP_NEVER,
659: ModelMBeanConstants.LOG + "=" + "F",
660: ModelMBeanConstants.VISIBILITY + "="
661: + ModelMBeanConstants.HIGH_VISIBILITY });
662: }
663:
664: private Map getAttributeDescriptors() {
665: Map map = new HashMap();
666: for (int i = 0; i < modelAttributes.length; ++i)
667: map.put(modelAttributes[i].getName(), (modelAttributes[i])
668: .getDescriptor());
669: return map;
670: }
671:
672: private Descriptor getAttributeDescriptor(String descrName) {
673: for (int i = 0; i < modelAttributes.length; ++i)
674: if (modelAttributes[i].getName().equals(descrName))
675: return modelAttributes[i].getDescriptor();
676: return null;
677: }
678:
679: private Map getOperationDescriptors() {
680: Map map = new HashMap();
681: for (int i = 0; i < modelOperations.length; ++i)
682: map.put(modelOperations[i].getName(), (modelOperations[i])
683: .getDescriptor());
684: return map;
685: }
686:
687: private Descriptor getOperationDescriptor(String descrName) {
688: for (int i = 0; i < modelOperations.length; ++i)
689: if (modelOperations[i].getName().equals(descrName))
690: return modelOperations[i].getDescriptor();
691: return null;
692: }
693:
694: private Map getConstructorDescriptors() {
695: Map map = new HashMap();
696: for (int i = 0; i < modelConstructors.length; ++i)
697: map.put(modelConstructors[i].getName(),
698: (modelConstructors[i]).getDescriptor());
699: return map;
700: }
701:
702: private Descriptor getConstructorDescriptor(String descrName) {
703: for (int i = 0; i < modelConstructors.length; ++i)
704: if (modelConstructors[i].getName().equals(descrName))
705: return modelConstructors[i].getDescriptor();
706: return null;
707: }
708:
709: private Map getNotificationDescriptors() {
710: Map map = new HashMap();
711: for (int i = 0; i < modelNotifications.length; ++i)
712: map.put(modelNotifications[i].getName(),
713: (modelNotifications[i]).getDescriptor());
714: return map;
715: }
716:
717: private Descriptor getNotificationDescriptor(String descrName) {
718: for (int i = 0; i < modelNotifications.length; ++i)
719: if (modelNotifications[i].getName().equals(descrName))
720: return modelNotifications[i].getDescriptor();
721: return null;
722: }
723:
724: private void readObject(ObjectInputStream ois) throws IOException,
725: ClassNotFoundException {
726: ModelMBeanAttributeInfo[] attrInfo;
727: ModelMBeanConstructorInfo[] consInfo;
728: ModelMBeanOperationInfo[] operInfo;
729: ModelMBeanNotificationInfo[] notifyInfo;
730: Descriptor desc;
731:
732: ObjectInputStream.GetField getField = ois.readFields();
733: switch (Serialization.version) {
734: case Serialization.V1R0:
735: attrInfo = (ModelMBeanAttributeInfo[]) getField.get(
736: "mmbAttributes", null);
737: consInfo = (ModelMBeanConstructorInfo[]) getField.get(
738: "mmbConstructors", null);
739: notifyInfo = (ModelMBeanNotificationInfo[]) getField.get(
740: "mmbNotifications", null);
741: operInfo = (ModelMBeanOperationInfo[]) getField.get(
742: "mmbOperations", null);
743: break;
744: default:
745: attrInfo = (ModelMBeanAttributeInfo[]) getField.get(
746: "modelMBeanAttributes", null);
747: consInfo = (ModelMBeanConstructorInfo[]) getField.get(
748: "modelMBeanConstructors", null);
749: notifyInfo = (ModelMBeanNotificationInfo[]) getField.get(
750: "modelMBeanNotifications", null);
751: operInfo = (ModelMBeanOperationInfo[]) getField.get(
752: "modelMBeanOperations", null);
753: }
754: desc = (Descriptor) getField.get("modelMBeanDescriptor", null);
755: if (desc == null)
756: throw new StreamCorruptedException("Null descriptor?");
757: this .modelAttributes = (null == attrInfo) ? new ModelMBeanAttributeInfo[0]
758: : attrInfo;
759: this .modelConstructors = (null == consInfo) ? new ModelMBeanConstructorInfo[0]
760: : consInfo;
761: this .modelOperations = (null == operInfo) ? new ModelMBeanOperationInfo[0]
762: : operInfo;
763: this .modelNotifications = (null == notifyInfo) ? new ModelMBeanNotificationInfo[0]
764: : notifyInfo;
765:
766: try {
767: setMBeanDescriptor(createDefaultDescriptor());
768: } catch (MBeanException ignore) {
769: }
770: }
771:
772: private void writeObject(ObjectOutputStream oos) throws IOException {
773: ObjectOutputStream.PutField putField = oos.putFields();
774: switch (Serialization.version) {
775: case Serialization.V1R0:
776: putField.put("mmbAttributes", modelAttributes);
777: putField.put("mmbConstructors", modelConstructors);
778: putField.put("mmbNotifications", modelNotifications);
779: putField.put("mmbOperations", modelOperations);
780: break;
781: default:
782: putField.put("modelMBeanAttributes", modelAttributes);
783: putField.put("modelMBeanConstructors", modelConstructors);
784: putField.put("modelMBeanNotifications", modelNotifications);
785: putField.put("modelMBeanOperations", modelOperations);
786: }
787: putField.put("modelMBeanDescriptor", mbeanDescriptor);
788: oos.writeFields();
789: }
790: }
|