001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.modelmbean;
009:
010: import javax.management.RuntimeOperationsException;
011: import javax.management.Descriptor;
012: import javax.management.DescriptorAccess;
013: import javax.management.MBeanNotificationInfo;
014:
015: /**
016: * The ModelMBeanNotificationInfo object describes a notification emitted by a ModelMBean.
017: * It is a subclass of MBeanNotificationInfo with the addition of an associated Descriptor
018: * and an implementation of the Descriptor interface.
019: * <P>
020: * <PRE>
021: * The fields in the descriptor are defined, but not limited to, the following: <P>
022: * name : notification name <P>
023: * descriptorType : must be "notification" <P>
024: * severity : 1-5 where 1: fatal 2: severe 3: error 4: warn 5: info <P>
025: * messageID : unique key for message text (to allow translation,analysis) <P>
026: * messageText : text of notification <P>
027: * log : T - log message F - do not log message <P>
028: * logfile : string fully qualified file name appropriate for operating system<P>
029: * visibility : 1-4 where 1: always visible 4: rarely visible <P>
030: * presentationString : xml formatted string to allow presentation of data <P>
031: * </PRE>
032: * The default descriptor contains the name, descriptorType, and severity=5 fields.
033: *
034: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
035: */
036:
037: public class ModelMBeanNotificationInfo extends MBeanNotificationInfo
038: implements DescriptorAccess, Cloneable {
039:
040: private Descriptor descriptor;
041:
042: /**
043: * Constructs a ModelMBeanNotificationInfo object with a default descxriptor.
044: *
045: * @param types The notification type string (in dot notation).
046: * @param name The name of the Notification class.
047: * @param description A human readable description of the Notification. Optional.
048: * <P>
049: * The descriptor must be set before this
050: * ModelMBeanNotificationInfo instance can be used.
051: */
052: public ModelMBeanNotificationInfo(String[] types, String name,
053: String description) {
054: super (types, name, description);
055: descriptor = createDefaultDescriptor();
056: }
057:
058: /**
059: * Constructs a ModelMBeanNotificationInfo object.
060: *
061: * @param types The notification type string (in dot notation).
062: * @param name The name of the Notification class.
063: * @param description A human readable description of the Notification. Optional.
064: * @param descriptor An instance of Descriptor containing the appropriate metadata
065: * for this instance of the MBeanNotificationInfo.If it is null or invalid then
066: * a default desriptor will be created.
067: *
068: */
069: public ModelMBeanNotificationInfo(String[] types, String name,
070: String description, Descriptor descriptor) {
071: super (types, name, description);
072: setDescriptor(descriptor);
073: }
074:
075: /**
076: * Constructs a new ModelMBeanNotificationInfo object from this ModelMBeanNotfication Object.
077: *
078: * @param notifInfo the ModelMBeanNotificationInfo to be duplicated
079: *
080: */
081: public ModelMBeanNotificationInfo(
082: ModelMBeanNotificationInfo notifInfo) {
083: super (notifInfo.getNotifTypes(), notifInfo.getName(), notifInfo
084: .getDescription());
085: descriptor = notifInfo.getDescriptor();
086: }
087:
088: /**
089: * Creates and returns a new ModelMBeanNotificationInfo which is a duplicate of this ModelMBeanNotificationInfo.
090: *
091: */
092: public Object clone() {
093: return new ModelMBeanNotificationInfo(this );
094: }
095:
096: /**
097: * Returns a copy of the associated Descriptor for the ModelMBeanNotificationInfo
098: *
099: * @return Descriptor associated with the ModelMBeanNotificationInfo object.
100: */
101: public Descriptor getDescriptor() {
102: return (Descriptor) descriptor.clone();
103: }
104:
105: /**
106: * Sets associated Descriptor (full replace) for the ModelMBeanNotificationInfo
107: * If the new Descriptor is null, then the associated Descriptor reverts to
108: * a default descriptor. The Descriptor is validated before it is assigned. If
109: * the new Descriptor is invalid, then an IllegalArgumentException is thrown.
110: *
111: * @param descriptor - replaces the Descriptor associated with the
112: * ModelMBeanNotification interface
113: *
114: */
115: public void setDescriptor(Descriptor descriptor) {
116: // System.out.println("ModelMBeanNitificationInfo:50 " + descriptor + descriptor.isValidDescriptor() + descriptor.getFieldValue(DescriptorConstants.NAME) + " " + getNameSpace());
117: if (descriptor == null) {
118: this .descriptor = createDefaultDescriptor();
119: } else if (isValidDescriptor(descriptor))
120: this .descriptor = (Descriptor) descriptor.clone();
121: else
122: throw new RuntimeOperationsException(
123: new IllegalArgumentException(
124: "Invalid descriptor passed in parameter"),
125: "Exception occured in ModelMBeanNotificationInfo setDescriptor");
126: }
127:
128: /**
129: * Returns a human readable string containing ModelMBeanNotificationInfo
130: *
131: * @exception RuntimeOperationsException for illegal value for field URLName or field Values.
132: * If the descriptor string fails for any reason, this exception will be thrown.
133: */
134: public String toString() {
135: String desc = "ModelMBeanNotificationInfo: " + getName()
136: + " ; Description: " + getDescription()
137: + " ; Descriptor: " + getDescriptor() + " ; Types: ";
138: String[] types = getNotifTypes();
139: for (int i = 0; i < types.length; i++)
140: desc += types[i] + ", ";
141:
142: return desc;
143: }
144:
145: /**
146: * Creates default descriptor for notification as follows:
147: * descriptorType=notification,
148: * name=this.getNameSpace(),displayname=this.getNameSpace(),log=F,severity=5,visibility=1
149: */
150: private Descriptor createDefaultDescriptor() {
151: return new DescriptorSupport(
152: new String[] {
153: DescriptorConstants.DESCRIPTORTYPE + "="
154: + DescriptorConstants.NOTIFICATION_TYPE,
155: DescriptorConstants.NAME + "=" + getName(),
156: DescriptorConstants.DISPLAYNAME + "="
157: + getName(),
158: DescriptorConstants.NOTIFICATION_SEVERITY
159: + "="
160: + DescriptorConstants.NOTIFICATION_SEVERITY_WARN });
161: }
162:
163: /**
164: * Tests that the descriptor is valid and adds appropriate default fields not already
165: * specified. Field values must be correct for field names.
166: * Descriptor must have the same name as the notification,the descriptorType field must be "notification",
167: * The following fields will be defaulted if they are not already set:
168: * displayName=this.getNameSpace(),displayname=this.getNameSpace(),log=F,severity=5,visibility=1
169: */
170: private boolean isValidDescriptor(Descriptor descriptor) {
171: boolean isValid = true;
172: if (descriptor == null) {
173: return false;
174: }
175: if (!descriptor.isValid()) {
176: isValid = false;
177: } else {
178: if (!((String) descriptor
179: .getFieldValue(DescriptorConstants.NAME))
180: .equalsIgnoreCase(getName())) {
181: isValid = false;
182: }
183: if (!((String) descriptor
184: .getFieldValue(DescriptorConstants.DESCRIPTORTYPE))
185: .equalsIgnoreCase(DescriptorConstants.NOTIFICATION_TYPE)) {
186: isValid = false;
187: }
188: if (descriptor
189: .getFieldValue(DescriptorConstants.DISPLAYNAME) == null)
190: descriptor.setField(DescriptorConstants.DISPLAYNAME,
191: getName());
192: if (descriptor
193: .getFieldValue(DescriptorConstants.NOTIFICATION_SEVERITY) == null)
194: descriptor.setField(
195: DescriptorConstants.NOTIFICATION_SEVERITY,
196: "" + DescriptorConstants.NOTIFICATION_SEVERITY);
197: }
198: return isValid;
199: }
200:
201: }
|