001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management.modelmbean;
010:
011: import javax.management.Descriptor;
012: import javax.management.DescriptorAccess;
013: import javax.management.MBeanNotificationInfo;
014: import javax.management.RuntimeOperationsException;
015:
016: /**
017: * @version $Revision: 1.12 $
018: */
019: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
020: // public class ModelMBeanNotificationInfo extends MBeanNotificationInfo implements DescriptorAccess
021: public class ModelMBeanNotificationInfo extends MBeanNotificationInfo
022: implements DescriptorAccess, Cloneable {
023: private static final long serialVersionUID = -7445681389570207141L;
024:
025: private Descriptor notificationDescriptor;
026:
027: public ModelMBeanNotificationInfo(String[] types, String name,
028: String description) {
029: this (types, name, description, null);
030: }
031:
032: public ModelMBeanNotificationInfo(String[] types, String name,
033: String description, Descriptor descriptor) {
034: super (types, name, description);
035: checkAndSetDescriptor(descriptor);
036: }
037:
038: public ModelMBeanNotificationInfo(ModelMBeanNotificationInfo copy) {
039: super (copy.getNotifTypes(), copy.getName(), copy
040: .getDescription());
041: checkAndSetDescriptor(copy.getDescriptor());
042: }
043:
044: public Object clone() {
045: return new ModelMBeanNotificationInfo(this );
046: }
047:
048: public Descriptor getDescriptor() {
049: return (Descriptor) notificationDescriptor.clone();
050: }
051:
052: public void setDescriptor(Descriptor descriptor) {
053: if (descriptor == null) {
054: notificationDescriptor = createDefaultDescriptor();
055: } else {
056: if (isDescriptorValid(descriptor)) {
057: notificationDescriptor = (Descriptor) descriptor
058: .clone();
059: } else {
060: // Not sure what to do here: javadoc says IllegalArgument, but for example ModelMBeanInfo throws RuntimeOperations
061: // which is consistent with the fact that all exception thrown by the JMX implementation should be JMX exceptions
062: // throw new IllegalArgumentException("Invalid descriptor");
063: throw new RuntimeOperationsException(
064: new IllegalArgumentException(
065: "Invalid descriptor"));
066: }
067: }
068: }
069:
070: private void checkAndSetDescriptor(Descriptor descriptor) {
071: if (descriptor == null) {
072: notificationDescriptor = createDefaultDescriptor();
073: } else if (isDescriptorValid(descriptor)) {
074: notificationDescriptor = (Descriptor) descriptor.clone();
075: if (notificationDescriptor.getFieldValue("displayname") == null) {
076: notificationDescriptor.setField("displayname",
077: getName());
078: }
079: } else {
080: throw new RuntimeOperationsException(
081: new IllegalArgumentException("Invalid Descriptor"));
082: }
083: }
084:
085: private boolean isDescriptorValid(Descriptor descriptor) {
086: if (!descriptor.isValid()) {
087: return false;
088: }
089:
090: // Spec compliance checks
091:
092: // Mandatory fields are: name, descriptorType, severity, messageId(?), log(?), logFile(?)
093: String[] names = descriptor.getFieldNames();
094:
095: if (!ModelMBeanInfoSupport.containsIgnoreCase(names, "name")
096: || !ModelMBeanInfoSupport.containsIgnoreCase(names,
097: "descriptortype")
098: || !ModelMBeanInfoSupport.containsIgnoreCase(names,
099: "severity")/* ||
100: !ModelMBeanInfoSupport.containsIgnoreCase(names, "messageid") ||
101: !ModelMBeanInfoSupport.containsIgnoreCase(names, "log")/* ||
102: !ModelMBeanInfoSupport.containsIgnoreCase(names, "logfile")*/) {
103: return false;
104: }
105: // Case sensitive name
106: String name = getName();
107: if (name == null) {
108: return false;
109: }
110: if (!name.equals(descriptor.getFieldValue("name"))) {
111: return false;
112: }
113: // Descriptor type must be 'notification'
114: String desctype = (String) descriptor
115: .getFieldValue("descriptortype");
116: if (desctype.compareToIgnoreCase("notification") != 0)
117: return false;
118: // Severity needn't be checked. It was checked in
119: // descriptor.isValid()
120: int severity = objectToInt(descriptor.getFieldValue("severity"));
121: if (severity < 0 || severity > 6) {
122: return false;
123: }
124:
125: return true;
126: }
127:
128: private Descriptor createDefaultDescriptor() {
129: String[] names = new String[] { "name", "descriptorType",
130: "severity", "displayName"/*, "messageId", "log", "logfile"*/};
131: Object[] values = new Object[] { getName(), "notification",
132: "5", getName() /*, "0", "???", "???"*/};
133: return new DescriptorSupport(names, values);
134: }
135:
136: private int objectToInt(Object value) {
137: if (value == null) {
138: return -1;
139: }
140:
141: if (value instanceof Number) {
142: return ((Number) value).intValue();
143: } else {
144: try {
145: return Integer.parseInt(value.toString());
146: } catch (NumberFormatException x) {
147: return -1;
148: }
149: }
150: }
151: }
|