0001: /**
0002: * The XMOJO Project 5
0003: * Copyright © 2003 XMOJO.org. All rights reserved.
0004:
0005: * NO WARRANTY
0006:
0007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
0008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
0009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
0010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
0011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
0013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
0014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
0015: * REPAIR OR CORRECTION.
0016:
0017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
0018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
0019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
0020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
0021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
0022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
0023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
0024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
0025: * SUCH DAMAGES.
0026: **/package javax.management.modelmbean;
0027:
0028: import java.io.ObjectInputStream;
0029: import java.io.ObjectOutputStream;
0030: import java.io.ObjectStreamField;
0031: import java.io.IOException;
0032: import java.io.Serializable;
0033: import java.util.Vector;
0034:
0035: import javax.management.Descriptor;
0036: import javax.management.MBeanInfo;
0037: import javax.management.MBeanAttributeInfo;
0038: import javax.management.MBeanConstructorInfo;
0039: import javax.management.MBeanOperationInfo;
0040: import javax.management.MBeanNotificationInfo;
0041: import javax.management.MBeanException;
0042: import javax.management.RuntimeOperationsException;
0043:
0044: /**
0045: * This class represents the meta data for ModelMBeans. Descriptors have been
0046: * added on the meta data objects.
0047: * <p>
0048: * Java resources wishing to be manageable instatiate the ModelMBean using the
0049: * MBeanServer's createMBean method. The resource then sets the ModelMBeanInfo
0050: * and Descriptors for the ModelMBean instance. The attributes and operations
0051: * exposed via the ModelMBeanInfo for the ModelMBean are accessible from Mbeans,
0052: * connectors/adapters like other MBeans. Through the Descriptors, values and
0053: * methods in the managed application can be defined and mapped to attributes
0054: * and operations of the ModelMBean. This mapping can be defined during development
0055: * in a file or dynamically and programmatically at runtime.
0056: * <p>
0057: * Every ModelMBean which is instantiated in the MBeanServer becomes manageable:
0058: * its attributes and operations become remotely accessible through the
0059: * connectors/adaptors connected to that MBeanServer. A Java object cannot be
0060: * registered in the MBeanServer unless it is a JMX compliant MBean. By instantiating
0061: * a ModelMBean, resources are guaranteed that the MBean is valid. MBeanException
0062: * and RuntimeOperationsException must be thrown on every public method. This
0063: * allows for wrappering exceptions from distributed communications (RMI, EJB, etc.)
0064: **/
0065: public class ModelMBeanInfoSupport extends MBeanInfo implements
0066: ModelMBeanInfo, Serializable {
0067: ModelMBeanAttributeInfo[] attributes = null;
0068: ModelMBeanConstructorInfo[] constructors = null;
0069: ModelMBeanOperationInfo[] operations = null;
0070: ModelMBeanNotificationInfo[] notifications = null;
0071: Descriptor descriptor = new DescriptorSupport();
0072: String className = null;
0073: String description = null;
0074:
0075: private static final long serialVersionUID = 0xc943ccf46e372743L;
0076:
0077: private static final ObjectStreamField[] serialPersistentFields = {
0078: new ObjectStreamField("modelMBeanDescriptor",
0079: Descriptor.class),
0080: new ObjectStreamField("mmbAttributes",
0081: new ModelMBeanAttributeInfo[] {}.getClass()),
0082: new ObjectStreamField("mmbConstructors",
0083: MBeanConstructorInfo.class),
0084: new ObjectStreamField("mmbNotifications",
0085: MBeanNotificationInfo.class),
0086: new ObjectStreamField("mmbOperations",
0087: MBeanOperationInfo.class),
0088: new ObjectStreamField("currClass", java.lang.String.class), };
0089:
0090: /**
0091: * Constructs a ModelMBeanInfoSupport which is a duplicate of the one passed in.
0092: *
0093: * @param mbi the ModelMBeanInfo instance from which the ModelMBeanInfo
0094: * being created is initialized
0095: */
0096: public ModelMBeanInfoSupport(ModelMBeanInfo mbi) {
0097: super ();
0098:
0099: ModelMBeanAttributeInfo[] attrs = null;
0100: ModelMBeanConstructorInfo[] constrs = null;
0101: ModelMBeanOperationInfo[] opers = null;
0102: ModelMBeanNotificationInfo[] notifs = null;
0103: Descriptor descr = null;
0104:
0105: if (mbi.getAttributes() != null) {
0106: ModelMBeanAttributeInfo[] a = (ModelMBeanAttributeInfo[]) mbi
0107: .getAttributes();
0108: attrs = new ModelMBeanAttributeInfo[a.length];
0109: for (int i = 0; i < a.length; i++)
0110: attrs[i] = (ModelMBeanAttributeInfo) a[i].clone();
0111: }
0112:
0113: if (mbi.getConstructors() != null) {
0114: ModelMBeanConstructorInfo[] c = (ModelMBeanConstructorInfo[]) mbi
0115: .getConstructors();
0116: constrs = new ModelMBeanConstructorInfo[c.length];
0117: for (int i = 0; i < c.length; i++)
0118: constrs[i] = (ModelMBeanConstructorInfo) c[i].clone();
0119:
0120: }
0121:
0122: if (mbi.getOperations() != null) {
0123: ModelMBeanOperationInfo[] o = (ModelMBeanOperationInfo[]) mbi
0124: .getOperations();
0125: opers = new ModelMBeanOperationInfo[o.length];
0126: for (int i = 0; i < o.length; i++)
0127: opers[i] = (ModelMBeanOperationInfo) o[i].clone();
0128: }
0129:
0130: if (mbi.getNotifications() != null) {
0131: ModelMBeanNotificationInfo[] n = (ModelMBeanNotificationInfo[]) mbi
0132: .getNotifications();
0133: notifs = new ModelMBeanNotificationInfo[n.length];
0134: for (int i = 0; i < n.length; i++)
0135: notifs[i] = (ModelMBeanNotificationInfo) n[i].clone();
0136: }
0137:
0138: try {
0139: if (mbi.getMBeanDescriptor() != null)
0140: descr = (Descriptor) mbi.getMBeanDescriptor().clone();
0141: } catch (Exception e) {
0142: if (e instanceof RuntimeException)
0143: throw (RuntimeException) e;
0144: throw new RuntimeException();
0145: }
0146:
0147: this .className = mbi.getClassName();
0148: this .description = mbi.getDescription();
0149: this .attributes = attrs;
0150: this .constructors = constrs;
0151: this .operations = opers;
0152: this .notifications = notifs;
0153: this .descriptor = descr;
0154: }
0155:
0156: /**
0157: * Creates a ModelMBeanInfoSupport with the provided information, but the
0158: * descriptor is a default. The default descriptor is: name=mbeanName,
0159: * descriptorType=mbean, displayName=this.getClassName(),
0160: * persistPolicy=never, log=F,export=F,visiblity=1
0161: *
0162: * @param className classname of ModelMBeanInfo
0163: *
0164: * @param description human readable description of the ModelMBean
0165: *
0166: * @param attributes array of ModelMBeanAttributeInfo objects which have descriptors
0167: *
0168: * @param constructors array of ModelMBeanConstructorInfo objects which have descriptor
0169: *
0170: * @param operations array of ModelMBeanOperationInfo objects which have descriptor
0171: *
0172: * @param notifications array of ModelMBeanNotificationInfo objects which have descriptor
0173: */
0174: public ModelMBeanInfoSupport(String className, String description,
0175: ModelMBeanAttributeInfo[] attributes,
0176: ModelMBeanConstructorInfo[] constructors,
0177: ModelMBeanOperationInfo[] operations,
0178: ModelMBeanNotificationInfo[] notifications) {
0179: super (className, description, attributes, constructors,
0180: operations, notifications);
0181: this .attributes = attributes;
0182: this .constructors = constructors;
0183: this .operations = operations;
0184: this .notifications = notifications;
0185: this .className = className;
0186: this .description = description;
0187: appendWithDefaultDescriptors((Descriptor) null);
0188: }
0189:
0190: /**
0191: * Creates a ModelMBeanInfoSupport with the provided information
0192: *
0193: * @param className classname of ModelMBeanInfo
0194: *
0195: * @param description human readable description of the ModelMBean
0196: *
0197: * @param attributes array of ModelMBeanAttributeInfo objects which have descriptors
0198: *
0199: * @param constructors array of ModelMBeanConstructorInfo objects which have descriptor
0200: *
0201: * @param operations array of ModelMBeanOperationInfo objects which have descriptor
0202: *
0203: * @param notifications array of ModelMBeanNotificationInfo objects which have descriptor
0204: *
0205: * @param mbeandescriptor the descriptor to be used as the MBeanDescriptor
0206: * containing mbean wide policy. If the descriptor is invalid
0207: * or null, a default descriptor will be constructed.
0208: * The default descriptor is:
0209: * name=mbeanName,
0210: * descriptorType=mbean,
0211: * displayName=this.getClassName(),
0212: * persistPolicy=never,
0213: * log=F,
0214: * export=F,
0215: * visiblity=1.
0216: * If the descriptor does not contain all these fields,
0217: * they will be added with these default values.
0218: */
0219: public ModelMBeanInfoSupport(String className, String description,
0220: ModelMBeanAttributeInfo[] attributes,
0221: ModelMBeanConstructorInfo[] constructors,
0222: ModelMBeanOperationInfo[] operations,
0223: ModelMBeanNotificationInfo[] notifications,
0224: Descriptor mbeandescriptor) {
0225: super (className, description, attributes, constructors,
0226: operations, notifications);
0227: this .attributes = attributes;
0228: this .constructors = constructors;
0229: this .operations = operations;
0230: this .notifications = notifications;
0231: this .className = className;
0232: this .description = description;
0233: this .descriptor = mbeandescriptor;
0234: appendWithDefaultDescriptors(mbeandescriptor);
0235: }
0236:
0237: /**
0238: * Returns the name of the Java class of the MBean described by this ModelMBeanInfo.
0239: *
0240: * @return the name of the Java class of the MBean described by this ModelMBeanInfo.
0241: */
0242: public String getClassName() {
0243: return className;
0244: }
0245:
0246: /**
0247: * Returns a human readable description of the MBean. Optional.
0248: *
0249: * @return a human readable description of the MBean. Optional.
0250: */
0251: public String getDescription() {
0252: return description;
0253: }
0254:
0255: /**
0256: * To clone this ModelMBeanInfo implementation object as
0257: * ModelMBeanInfoSupport object.
0258: *
0259: * @return The duplicate copy of the object
0260: */
0261: public Object clone() {
0262: ModelMBeanAttributeInfo[] attrs = null;
0263: ModelMBeanConstructorInfo[] constrs = null;
0264: ModelMBeanOperationInfo[] opers = null;
0265: ModelMBeanNotificationInfo[] notifs = null;
0266: Descriptor descr = null;
0267:
0268: if (attributes != null) {
0269: ModelMBeanAttributeInfo[] a = attributes;
0270: attrs = new ModelMBeanAttributeInfo[a.length];
0271: for (int i = 0; i < a.length; i++)
0272: attrs[i] = (ModelMBeanAttributeInfo) a[i].clone();
0273: }
0274:
0275: if (constructors != null) {
0276: ModelMBeanConstructorInfo[] c = constructors;
0277: constrs = new ModelMBeanConstructorInfo[c.length];
0278: for (int i = 0; i < c.length; i++)
0279: constrs[i] = (ModelMBeanConstructorInfo) c[i].clone();
0280:
0281: }
0282:
0283: if (operations != null) {
0284: ModelMBeanOperationInfo[] o = operations;
0285: opers = new ModelMBeanOperationInfo[o.length];
0286: for (int i = 0; i < o.length; i++)
0287: opers[i] = (ModelMBeanOperationInfo) o[i].clone();
0288: }
0289:
0290: if (notifications != null) {
0291: ModelMBeanNotificationInfo[] n = notifications;
0292: notifs = new ModelMBeanNotificationInfo[n.length];
0293: for (int i = 0; i < n.length; i++)
0294: notifs[i] = (ModelMBeanNotificationInfo) n[i].clone();
0295: }
0296:
0297: if (descriptor != null)
0298: descr = (Descriptor) descriptor.clone();
0299:
0300: return new ModelMBeanInfoSupport(getClassName(),
0301: getDescription(), attrs, constrs, opers, notifs, descr);
0302: }
0303:
0304: /**
0305: * Gets the attributes info of the ModelMBean targetted object. The return
0306: * object will be a array of ModelMBeanAttributeInfo objcts.
0307: *
0308: * @return The array of MBeanAttributeInfo objects are returned after
0309: * getting the attributes info of ModelMBean targetted object
0310: */
0311: public MBeanAttributeInfo[] getAttributes() {
0312: if (attributes == null)
0313: return new ModelMBeanAttributeInfo[0];
0314:
0315: return attributes;
0316: }
0317:
0318: /**
0319: * Gets the constructors info of the ModelMBean targetted object.
0320: * The return object will be a array of ModelMBeanConstructorInfo objects.
0321: *
0322: * @return The array of MBeanConstructorInfo objects are returned after
0323: * getting the constructors info of ModelMBean targetted object.
0324: */
0325: public MBeanConstructorInfo[] getConstructors() {
0326: if (constructors == null)
0327: return new ModelMBeanConstructorInfo[0];
0328:
0329: return constructors;
0330: }
0331:
0332: /**
0333: * Gets the notifications info emitted by the ModelMBean targetted object.
0334: * The return object will be a array of ModelMBeanNotificationInfo objects.
0335: *
0336: * @return The array of MBeanNotificationInfo objects are returned after
0337: * getting the notifications info of the ModelMBean targetted object.
0338: */
0339: public MBeanNotificationInfo[] getNotifications() {
0340: if (notifications == null)
0341: return new ModelMBeanNotificationInfo[0];
0342:
0343: return notifications;
0344: }
0345:
0346: /**
0347: * Gets the operations info of the ModelMBean targetted object. The return
0348: * object will be a array of ModelMBeanOperationInfo objects.
0349: *
0350: * @return The array of MBeanOperationInfo objects are returned after
0351: * getting the operations info of the ModelMBean targetted object.
0352: */
0353: public MBeanOperationInfo[] getOperations() {
0354: if (operations == null)
0355: return new ModelMBeanOperationInfo[0];
0356:
0357: return operations;
0358: }
0359:
0360: /**
0361: * Returns a Descriptor array consisting of all Descriptors for the
0362: * ModelMBeanInfo including the MBean Descriptor, attribute Descriptors,
0363: * operation Descriptors, constructor Descriptors, and notification Descriptors.
0364: *
0365: * @param inDescriptorType value of descriptorType field that must be set
0366: * for the descriptor to be returned. Must be "mbean",
0367: * "attribute", "operation", "constructor", or "notification".
0368: * If it is null then all types will be returned.
0369: *
0370: * @return Descriptor array containing all descriptors for the ModelMBean
0371: *
0372: * @exception MBeanException Wraps another exception
0373: *
0374: * @exception RuntimeOperationsException Wraps another exception
0375: */
0376: public Descriptor[] getDescriptors(String inDescriptorType)
0377: throws MBeanException, RuntimeOperationsException {
0378: Descriptor[] toRet = null;
0379:
0380: if (inDescriptorType == null)
0381: throw new RuntimeOperationsException(new RuntimeException(
0382: "invalid method param"));
0383:
0384: if (inDescriptorType.equals("mbean")) {
0385: return new Descriptor[] { descriptor };
0386: } else if (inDescriptorType.equals("attribute")
0387: && attributes != null) {
0388: toRet = new Descriptor[attributes.length];
0389: for (int i = 0; i < attributes.length; i++)
0390: toRet[i] = attributes[i].getDescriptor();
0391: } else if (inDescriptorType.equals("operation")
0392: && operations != null) {
0393: toRet = new Descriptor[operations.length];
0394: for (int i = 0; i < operations.length; i++)
0395: toRet[i] = operations[i].getDescriptor();
0396: } else if (inDescriptorType.equals("constructor")
0397: && constructors != null) {
0398: toRet = new Descriptor[constructors.length];
0399: for (int i = 0; i < constructors.length; i++)
0400: toRet[i] = constructors[i].getDescriptor();
0401: } else if (inDescriptorType.equals("notification")
0402: && notifications != null) {
0403: toRet = new Descriptor[notifications.length];
0404: for (int i = 0; i < notifications.length; i++)
0405: toRet[i] = notifications[i].getDescriptor();
0406: } else if (inDescriptorType.equals("all")) {
0407: //toRet = new Descriptor[attributes.length+constructors.length+operations.length+notifications.length];
0408: Vector descVec = new Vector();
0409: if (attributes != null) {
0410: for (int i = 0; i < attributes.length; i++) {
0411: descVec.add(attributes[i].getDescriptor());
0412: }
0413: }
0414: if (constructors != null) {
0415: for (int i = 0; i < constructors.length; i++) {
0416: descVec.add(constructors[i].getDescriptor());
0417: }
0418: }
0419: if (operations != null) {
0420: for (int i = 0; i < operations.length; i++) {
0421: descVec.add(operations[i].getDescriptor());
0422: }
0423: }
0424: if (notifications != null) {
0425: for (int i = 0; i < notifications.length; i++) {
0426: descVec.add(notifications[i].getDescriptor());
0427: }
0428: }
0429:
0430: int size = descVec.size();
0431: toRet = new Descriptor[size];
0432:
0433: for (int i = 0; i < size; i++) {
0434: toRet[i] = (Descriptor) descVec.get(i);
0435: }
0436: } else {
0437: throw new RuntimeOperationsException(new RuntimeException(
0438: "invalid method param"));
0439: }
0440:
0441: return toRet;
0442: }
0443:
0444: /**
0445: * Adds or replaces descriptors in the ModelMBeanInfo.
0446: *
0447: * @param inDescriptors The descriptors to be set in the ModelMBeanInfo.
0448: * Null elements of the list will be ignored. All descriptors
0449: * must have name and descriptorType fields.
0450: *
0451: * @exception MBeanException Wraps another exception
0452: *
0453: * @exception RuntimeOperationsException Wraps exceptions for illegal
0454: * or null arguments
0455: */
0456: public void setDescriptors(Descriptor[] inDescriptors)
0457: throws MBeanException, RuntimeOperationsException {
0458: if (inDescriptors == null)
0459: throw new RuntimeOperationsException(new RuntimeException(
0460: "invalid method param"));
0461:
0462: for (int i = 0; i < inDescriptors.length; i++)
0463: setDescriptor(inDescriptors[i], null);
0464: }
0465:
0466: /**
0467: * Returns a Descriptor requested by name
0468: *
0469: * @param inDescriptorName The name of the descriptor.
0470: *
0471: * @return Descriptor containing the descriptor for the ModelMBean with the
0472: * same name. If a descriptor is not found, null is returned.
0473: *
0474: * @exception MBeanException Wraps another exception
0475: *
0476: * @exception RuntimeOperationsException Wraps exceptions for invalid
0477: * input name or type.
0478: */
0479: public Descriptor getDescriptor(String inDescriptorName)
0480: throws MBeanException, RuntimeOperationsException {
0481: Descriptor descr = null;
0482:
0483: if ((descr = getDescriptor(inDescriptorName, "mbean")) != null)
0484: return descr;
0485:
0486: if ((descr = getDescriptor(inDescriptorName, "attribute")) != null)
0487: return descr;
0488:
0489: if ((descr = getDescriptor(inDescriptorName, "operation")) != null)
0490: return descr;
0491:
0492: if ((descr = getDescriptor(inDescriptorName, "notification")) != null)
0493: return descr;
0494:
0495: return null;
0496: }
0497:
0498: /**
0499: * Returns a Descriptor requested by name and descriptorType.
0500: *
0501: * @param inDescriptorName The name of the descriptor.
0502: *
0503: * @param inDescriptorType The type of the descriptor being requested.
0504: * If this is null then all types are searched. Valid types
0505: * are 'attribute', 'constructor', 'operation', and
0506: * 'notification'. This value must be equal to the
0507: * 'descriptorType' field in the descriptor that is returned.
0508: *
0509: * @return Descriptor containing the descriptor for the ModelMBean with
0510: * the same name and descriptorType. If a descriptor is not
0511: * found, null is returned.
0512: *
0513: * @exception MBeanException Wraps another exception
0514: *
0515: * @exception RuntimeOperationsException Wraps exceptions for invalid
0516: * input name or type.
0517: */
0518: public Descriptor getDescriptor(String inDescriptorName,
0519: String inDescriptorType) throws MBeanException,
0520: RuntimeOperationsException {
0521: if (inDescriptorName == null || inDescriptorType == null)
0522: throw new RuntimeOperationsException(new RuntimeException(
0523: "invalid method param"));
0524:
0525: if (inDescriptorType.equals("mbean")) {
0526: if (descriptor.getFieldValue("name").equals(
0527: inDescriptorName))
0528: return descriptor;
0529: } else if (inDescriptorType.equals("attribute")
0530: && attributes != null) {
0531: for (int i = 0; i < attributes.length; i++)
0532: if (attributes[i].getDescriptor().getFieldValue("name")
0533: .equals(inDescriptorName))
0534: return attributes[i].getDescriptor();
0535: } else if (inDescriptorType.equals("operation")
0536: && operations != null) {
0537: for (int i = 0; i < operations.length; i++)
0538: if (operations[i].getDescriptor().getFieldValue("name")
0539: .equals(inDescriptorName))
0540: return operations[i].getDescriptor();
0541: } else if (inDescriptorType.equals("constructor")
0542: && constructors != null) {
0543: for (int i = 0; i < constructors.length; i++)
0544: if (constructors[i].getDescriptor().getFieldValue(
0545: "name").equals(inDescriptorName))
0546: return constructors[i].getDescriptor();
0547: } else if (inDescriptorType.equals("notification")
0548: && notifications != null) {
0549: for (int i = 0; i < notifications.length; i++)
0550: if (notifications[i].getDescriptor().getFieldValue(
0551: "name").equals(inDescriptorName))
0552: return notifications[i].getDescriptor();
0553: } else
0554: throw new RuntimeOperationsException(new RuntimeException(
0555: "invalid method param"));
0556:
0557: return null;
0558: }
0559:
0560: /**
0561: * Adds or replaces descriptors in all the info arrays for the ModelMBean.
0562: *
0563: * @param inDescriptor The descriptor to be set in the ModelMBean.
0564: * It must NOT be null. All descriptors must have name and
0565: * descriptorType fields.
0566: *
0567: * @param inDescriptorType The type of the descriptor being set. If this
0568: * is null then the descriptorType field in the descriptor
0569: * is used. If specified this value must be set in the
0570: * descriptorType field for the descriptor to be returned.
0571: * Must be "mbean","attribute", "operation", "constructor",
0572: * or "notification".
0573: *
0574: * @exception RuntimeOperationsException Wraps exceptions for illegal
0575: * or null arguments.
0576: */
0577: public void setDescriptor(Descriptor inDescriptor,
0578: String inDescriptorType) throws MBeanException,
0579: RuntimeOperationsException {
0580: if (inDescriptor == null
0581: || inDescriptor.getFieldValue("name") == null
0582: || inDescriptor.getFieldValue("descriptorType") == null)
0583: throw new RuntimeOperationsException(
0584: new RuntimeException(
0585: "invalid method param. May be name|descriptorType is absent"));
0586:
0587: String name = (String) inDescriptor.getFieldValue("name");
0588:
0589: if (inDescriptorType == null)
0590: inDescriptorType = (String) inDescriptor
0591: .getFieldValue("descriptorType");
0592:
0593: if (inDescriptorType.equals("mbean")) {
0594: setMBeanDescriptor(inDescriptor);
0595: } else if (inDescriptorType.equals("attribute")
0596: && attributes != null) {
0597: for (int i = 0; i < attributes.length; i++) {
0598: if (((String) attributes[i].getDescriptor()
0599: .getFieldValue("name")).equals(name))
0600: attributes[i].setDescriptor(inDescriptor);
0601: break;
0602: }
0603: } else if (inDescriptorType.equals("operation")
0604: && operations != null) {
0605: for (int i = 0; i < operations.length; i++) {
0606: if (((String) operations[i].getDescriptor()
0607: .getFieldValue("name")).equals(name))
0608: operations[i].setDescriptor(inDescriptor);
0609: break;
0610: }
0611: } else if (inDescriptorType.equals("constructor")
0612: && constructors != null) {
0613: for (int i = 0; i < constructors.length; i++) {
0614: if (((String) constructors[i].getDescriptor()
0615: .getFieldValue("name")).equals(name))
0616: constructors[i].setDescriptor(inDescriptor);
0617: break;
0618: }
0619: } else if (inDescriptorType.equals("notification")
0620: && notifications != null) {
0621: for (int i = 0; i < notifications.length; i++) {
0622: if (((String) notifications[i].getDescriptor()
0623: .getFieldValue("name")).equals(name))
0624: notifications[i].setDescriptor(inDescriptor);
0625: break;
0626: }
0627: } else
0628: throw new RuntimeOperationsException(new RuntimeException(
0629: "invalid descriptorType"));
0630: }
0631:
0632: /**
0633: * Returns a MBean's descriptor. This descriptor contains metadata about
0634: * the MBean and default policies for persistence and caching for the
0635: * entire MBean. Policies may be overridden by descriptors associated with
0636: * attribute, constructors, or operations with the same fieldNames.
0637: *
0638: * @return The MBeanDescriptor
0639: *
0640: * @exception MBeanException Wraps another exception
0641: *
0642: * @exception RuntimeOperationsException Wraps another exception
0643: */
0644: public Descriptor getMBeanDescriptor() throws MBeanException,
0645: RuntimeOperationsException {
0646: return descriptor;
0647: }
0648:
0649: /**
0650: * Sets the ModelMBean's descriptor (fully replace). This descriptor
0651: * contains metadata about the MBean and default policies for persistence
0652: * and caching for the entire MBean. Policies may be overridden by
0653: * descriptors associated with attribute, constructors, or operations with
0654: * the same fieldNames. This operation does a complete replacement of
0655: * the descriptor, no merging is done.
0656: *
0657: * @param inDescriptor This descriptor contains metadata about the MBean
0658: * and default policies for persistence and caching for
0659: * the entire MBean.
0660: *
0661: * @exception MBeanException Wraps another exception
0662: *
0663: * @exception RuntimeOperationsException Wraps another exception
0664: */
0665: public void setMBeanDescriptor(Descriptor inDescriptor)
0666: throws MBeanException, RuntimeOperationsException {
0667: if (inDescriptor == null)
0668: throw new RuntimeOperationsException(new RuntimeException(
0669: "invalid null Descriptor "));
0670:
0671: inDescriptor.isValid();
0672: this .descriptor = inDescriptor;
0673:
0674: //set the mbean common properties to attributes/operations/constructors if
0675: //the field is not present.
0676: if (attributes != null) {
0677: for (int i = 0; i < attributes.length; i++) {
0678: if (attributes[i].getDescriptor().getFieldValue(
0679: DescriptorSupport.PERSIST_POLICY) == null
0680: && descriptor
0681: .getFieldValue(DescriptorSupport.PERSIST_POLICY) != null)
0682: attributes[i]
0683: .getDescriptor()
0684: .setField(
0685: DescriptorSupport.PERSIST_POLICY,
0686: descriptor
0687: .getFieldValue(DescriptorSupport.PERSIST_POLICY));
0688:
0689: if (attributes[i].getDescriptor().getFieldValue(
0690: DescriptorSupport.PERSIST_PERIOD) == null
0691: && descriptor
0692: .getFieldValue(DescriptorSupport.PERSIST_PERIOD) != null)
0693: attributes[i]
0694: .getDescriptor()
0695: .setField(
0696: DescriptorSupport.PERSIST_PERIOD,
0697: descriptor
0698: .getFieldValue(DescriptorSupport.PERSIST_PERIOD));
0699:
0700: if (attributes[i].getDescriptor().getFieldValue(
0701: DescriptorSupport.CURRENCY_TIME_LIMIT) == null
0702: && descriptor
0703: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT) != null)
0704: attributes[i]
0705: .getDescriptor()
0706: .setField(
0707: DescriptorSupport.CURRENCY_TIME_LIMIT,
0708: descriptor
0709: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT));
0710:
0711: if (attributes[i].getDescriptor().getFieldValue(
0712: DescriptorSupport.VISIBILITY) == null
0713: && descriptor
0714: .getFieldValue(DescriptorSupport.VISIBILITY) != null)
0715: attributes[i]
0716: .getDescriptor()
0717: .setField(
0718: DescriptorSupport.VISIBILITY,
0719: descriptor
0720: .getFieldValue(DescriptorSupport.VISIBILITY));
0721:
0722: if (attributes[i].getDescriptor().getFieldValue(
0723: DescriptorSupport.PRESENTATION_STRING) == null
0724: && descriptor
0725: .getFieldValue(DescriptorSupport.PRESENTATION_STRING) != null)
0726: attributes[i]
0727: .getDescriptor()
0728: .setField(
0729: DescriptorSupport.PRESENTATION_STRING,
0730: descriptor
0731: .getFieldValue(DescriptorSupport.PRESENTATION_STRING));
0732: }
0733: }
0734:
0735: if (operations != null) {
0736: for (int i = 0; i < operations.length; i++) {
0737: if (operations[i].getDescriptor().getFieldValue(
0738: DescriptorSupport.CURRENCY_TIME_LIMIT) == null
0739: && descriptor
0740: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT) != null)
0741: operations[i]
0742: .getDescriptor()
0743: .setField(
0744: DescriptorSupport.CURRENCY_TIME_LIMIT,
0745: descriptor
0746: .getFieldValue(DescriptorSupport.CURRENCY_TIME_LIMIT));
0747:
0748: if (operations[i].getDescriptor().getFieldValue(
0749: DescriptorSupport.VISIBILITY) == null
0750: && descriptor
0751: .getFieldValue(DescriptorSupport.VISIBILITY) != null)
0752: operations[i]
0753: .getDescriptor()
0754: .setField(
0755: DescriptorSupport.VISIBILITY,
0756: descriptor
0757: .getFieldValue(DescriptorSupport.VISIBILITY));
0758:
0759: if (operations[i].getDescriptor().getFieldValue(
0760: DescriptorSupport.PRESENTATION_STRING) == null
0761: && descriptor
0762: .getFieldValue(DescriptorSupport.PRESENTATION_STRING) != null)
0763: operations[i]
0764: .getDescriptor()
0765: .setField(
0766: DescriptorSupport.PRESENTATION_STRING,
0767: descriptor
0768: .getFieldValue(DescriptorSupport.PRESENTATION_STRING));
0769: }
0770: }
0771:
0772: if (constructors != null) {
0773: for (int i = 0; i < constructors.length; i++) {
0774: if (constructors[i].getDescriptor().getFieldValue(
0775: DescriptorSupport.VISIBILITY) == null
0776: && descriptor
0777: .getFieldValue(DescriptorSupport.VISIBILITY) != null)
0778: constructors[i]
0779: .getDescriptor()
0780: .setField(
0781: DescriptorSupport.VISIBILITY,
0782: descriptor
0783: .getFieldValue(DescriptorSupport.VISIBILITY));
0784:
0785: if (constructors[i].getDescriptor().getFieldValue(
0786: DescriptorSupport.PRESENTATION_STRING) == null
0787: && descriptor
0788: .getFieldValue(DescriptorSupport.PRESENTATION_STRING) != null)
0789: constructors[i]
0790: .getDescriptor()
0791: .setField(
0792: DescriptorSupport.PRESENTATION_STRING,
0793: descriptor
0794: .getFieldValue(DescriptorSupport.PRESENTATION_STRING));
0795: }
0796: }
0797:
0798: if (notifications != null) {
0799: for (int i = 0; i < notifications.length; i++) {
0800: if (notifications[i].getDescriptor().getFieldValue(
0801: DescriptorSupport.LOG) == null
0802: && descriptor
0803: .getFieldValue(DescriptorSupport.LOG) != null)
0804: notifications[i]
0805: .getDescriptor()
0806: .setField(
0807: DescriptorSupport.LOG,
0808: descriptor
0809: .getFieldValue(DescriptorSupport.LOG));
0810:
0811: if (notifications[i].getDescriptor().getFieldValue(
0812: DescriptorSupport.LOG_FILE) == null
0813: && descriptor
0814: .getFieldValue(DescriptorSupport.LOG_FILE) != null)
0815: notifications[i]
0816: .getDescriptor()
0817: .setField(
0818: DescriptorSupport.LOG_FILE,
0819: descriptor
0820: .getFieldValue(DescriptorSupport.LOG_FILE));
0821:
0822: if (notifications[i].getDescriptor().getFieldValue(
0823: DescriptorSupport.VISIBILITY) == null
0824: && descriptor
0825: .getFieldValue(DescriptorSupport.VISIBILITY) != null)
0826: notifications[i]
0827: .getDescriptor()
0828: .setField(
0829: DescriptorSupport.VISIBILITY,
0830: descriptor
0831: .getFieldValue(DescriptorSupport.VISIBILITY));
0832:
0833: if (notifications[i].getDescriptor().getFieldValue(
0834: DescriptorSupport.PRESENTATION_STRING) == null
0835: && descriptor
0836: .getFieldValue(DescriptorSupport.PRESENTATION_STRING) != null)
0837: notifications[i]
0838: .getDescriptor()
0839: .setField(
0840: DescriptorSupport.PRESENTATION_STRING,
0841: descriptor
0842: .getFieldValue(DescriptorSupport.PRESENTATION_STRING));
0843: }
0844: }
0845: }
0846:
0847: /**
0848: * Returns a ModelMBeanAttributeInfo requested by name.
0849: *
0850: * @param inName The name of the ModelMBeanAttributeInfo to get.
0851: *
0852: * @return ModelMBeanAttributeInfo requested by name
0853: *
0854: * @exception MBeanException Wraps another exception
0855: *
0856: * @exception RuntimeOperationsException Wraps another exception for
0857: * invalid attribute name or ModelMBeanAttributeInfo to be returned.
0858: */
0859: public ModelMBeanAttributeInfo getAttribute(String inName)
0860: throws MBeanException, RuntimeOperationsException {
0861: if (inName == null)
0862: throw new RuntimeOperationsException(new RuntimeException(
0863: "invalid method param."));
0864:
0865: if (attributes != null) {
0866: for (int i = 0; i < attributes.length; i++)
0867: if (attributes[i].getName().equals(inName))
0868: return attributes[i];
0869: }
0870:
0871: return null;
0872: }
0873:
0874: /**
0875: * Returns a ModelMBeanOperationInfo requested by name.
0876: *
0877: * @param inName The name of the ModelMBeanOperationInfo to get.
0878: *
0879: * @return ModelMBeanOperationInfo requested by name
0880: *
0881: * @exception MBeanException Wraps another exception
0882: *
0883: * @exception RuntimeOperationsException Wraps another exception for invalid
0884: * attribute name or ModelMBeanOperationInfo to be returned.
0885: */
0886: public ModelMBeanOperationInfo getOperation(String inName)
0887: throws MBeanException, RuntimeOperationsException {
0888: if (inName == null)
0889: throw new RuntimeOperationsException(new RuntimeException(
0890: "invalid method param."));
0891:
0892: if (operations != null) {
0893: for (int i = 0; i < operations.length; i++)
0894: if (operations[i].getName().equals(inName))
0895: return operations[i];
0896: }
0897:
0898: return null;
0899: }
0900:
0901: /**
0902: * Returns a ModelMBeanConstructorInfo requested by name.
0903: *
0904: * @param String inName The name of the ModelMBeanConstructorInfo to get.
0905: *
0906: * @return ModelMBeanConstructorInfo requested by name
0907: *
0908: * @exception MBeanException Wraps another exception
0909: *
0910: * @exception RuntimeOperationsException Wraps another exception for invalid
0911: * attribute name or ModelMBeanConstructorInfo to be returned.
0912: */
0913: public ModelMBeanConstructorInfo getConstructor(String inName)
0914: throws MBeanException, RuntimeOperationsException {
0915: if (inName == null)
0916: throw new RuntimeOperationsException(new RuntimeException(
0917: "invalid method param."));
0918:
0919: if (constructors != null) {
0920: for (int i = 0; i < constructors.length; i++)
0921: if (constructors[i].getName().equals(inName))
0922: return constructors[i];
0923: }
0924:
0925: return null;
0926: }
0927:
0928: /**
0929: * Returns a ModelMBeanNotificationInfo requested by name.
0930: *
0931: * @param inName The name of the ModelMBeanNotificationInfo to get.
0932: *
0933: * @return ModelMBeanNotificationInfo requested by name
0934: *
0935: * @exception MBeanException Wraps another exception
0936: *
0937: * @exception RuntimeOperationsException Wraps another exception
0938: */
0939: public ModelMBeanNotificationInfo getNotification(String inName)
0940: throws MBeanException, RuntimeOperationsException {
0941: if (inName == null)
0942: throw new RuntimeOperationsException(new RuntimeException(
0943: "invalid method param."));
0944:
0945: if (notifications != null) {
0946: for (int i = 0; i < notifications.length; i++)
0947: if (notifications[i].getName().equals(inName))
0948: return notifications[i];
0949: }
0950:
0951: return null;
0952: }
0953:
0954: //-------------------------- Private methods --------------------------//
0955:
0956: private void appendWithDefaultDescriptors(Descriptor descriptor) {
0957: if (descriptor != null)
0958: descriptor.isValid();
0959: else
0960: descriptor = new DescriptorSupport();
0961:
0962: if (descriptor.getFieldValue("name") == null)
0963: descriptor.setField("name", "mbeanName");
0964:
0965: if (descriptor.getFieldValue("descriptorType") == null)
0966: descriptor.setField("descriptorType", "mbean");
0967:
0968: if (descriptor.getFieldValue("displayName") == null)
0969: descriptor.setField("displayName", getClassName());
0970:
0971: if (descriptor.getFieldValue("persistPolicy") == null)
0972: descriptor.setField("persistPolicy", "never");
0973:
0974: if (descriptor.getFieldValue("log") == null)
0975: descriptor.setField("log", "F");
0976:
0977: if (descriptor.getFieldValue("export") == null)
0978: descriptor.setField("export", "F");
0979:
0980: if (descriptor.getFieldValue("visibility") == null)
0981: descriptor.setField("visibility", "1");
0982: }
0983:
0984: private void readObject(ObjectInputStream objectinputstream)
0985: throws IOException, ClassNotFoundException {
0986:
0987: try {
0988: ObjectInputStream.GetField getfield = objectinputstream
0989: .readFields();
0990:
0991: ObjectStreamField[] fields = getfield
0992: .getObjectStreamClass().getFields();
0993:
0994: descriptor = (Descriptor) getfield.get(
0995: "modelMBeanDescriptor", null);
0996:
0997: attributes = (ModelMBeanAttributeInfo[]) getfield.get(
0998: "mmbAttributes", null);
0999: if (attributes == null) {
1000: attributes = (ModelMBeanAttributeInfo[]) super
1001: .getAttributes();
1002: }
1003:
1004: constructors = (ModelMBeanConstructorInfo[]) getfield.get(
1005: "mmbConstructors", null);
1006: if (constructors == null) {
1007: constructors = (ModelMBeanConstructorInfo[]) super
1008: .getConstructors();
1009: }
1010:
1011: notifications = (ModelMBeanNotificationInfo[]) getfield
1012: .get("mmbNotifications", null);
1013: if (notifications == null) {
1014: notifications = (ModelMBeanNotificationInfo[]) super
1015: .getNotifications();
1016: }
1017:
1018: operations = (ModelMBeanOperationInfo[]) getfield.get(
1019: "mmbOperations", null);
1020: if (operations == null) {
1021: operations = (ModelMBeanOperationInfo[]) super
1022: .getOperations();
1023: }
1024:
1025: className = (String) getfield.get("currClass", "");
1026: } catch (Exception exception) {
1027: exception.printStackTrace();
1028: throw new IOException(exception.toString());
1029: }
1030: }
1031:
1032: private void writeObject(ObjectOutputStream objectoutputstream)
1033: throws IOException {
1034: ObjectOutputStream.PutField putfield = objectoutputstream
1035: .putFields();
1036: putfield.put("modelMBeanDescriptor", descriptor);
1037: putfield.put("mmbAttributes", attributes);
1038: putfield.put("mmbConstructors", constructors);
1039: putfield.put("mmbNotifications", notifications);
1040: putfield.put("mmbOperations", operations);
1041: putfield.put("currClass", className);
1042: objectoutputstream.writeFields();
1043: }
1044: }
|