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.relation;
009:
010: import java.util.Vector;
011: import java.util.Set;
012: import java.util.HashSet;
013: import javax.management.NotificationFilterSupport;
014: import javax.management.MBeanServerNotification;
015: import javax.management.ObjectName;
016: import javax.management.Notification;
017:
018: /**
019: * This filter allows to filter MBeanServerNotification notifications by
020: * selecting the ObjectNames of interest and the operations (registration,
021: * unregistration, both) of interest (corresponding to notification
022: * types).
023: *
024: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
025: */
026:
027: public class MBeanServerNotificationFilter extends
028: NotificationFilterSupport {
029:
030: private Set selectedNames = new HashSet(); // null for select all
031: private Set deselectedNames = new HashSet(); // null for deselect all
032:
033: private boolean enableAll = false;
034: private boolean disableAll = false;
035:
036: /**
037: * Creates a filter selecting all MBeanServerNotification notifications for
038: * all ObjectNames
039: */
040: public MBeanServerNotificationFilter() {
041:
042: super ();
043: enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
044: enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
045:
046: }
047:
048: /**
049: * Disables any MBeanServerNotification (i.e. all ObjectNames
050: * deselected)
051: */
052: public synchronized void disableAllObjectNames() {
053: selectedNames.clear();
054: deselectedNames.clear();
055: enableAll = false;
056: disableAll = true;
057: }
058:
059: /**
060: * Disables MBeanServerNotifications concerning given ObjectName.
061: *
062: * @param objectName ObjectName no longer of interest
063: *
064: * @exception IllegalArgumentException if the given ObjectName is null
065: */
066: public synchronized void disableObjectName(ObjectName objectName)
067: throws IllegalArgumentException {
068:
069: if (objectName == null)
070: throw new IllegalArgumentException(
071: "Invalid parameter:Null ObjectName");
072:
073: if (selectedNames.contains(objectName))
074: selectedNames.remove(objectName);
075:
076: if (!(deselectedNames.contains(objectName)))
077: deselectedNames.add(objectName);
078: }
079:
080: synchronized void disableObjectNames(ObjectName[] objectNames)
081: throws IllegalArgumentException {
082: for (int i = 0; i < objectNames.length; i++) {
083: this .disableObjectName(objectNames[i]);
084: }
085: }
086:
087: /**
088: * Enables all MBeanServerNotifications (i.e. all ObjectNames selected)
089: */
090: public synchronized void enableAllObjectNames() {
091: selectedNames.clear();
092: deselectedNames.clear();
093: enableAll = true;
094: disableAll = false;
095: }
096:
097: /**
098: * Enables MBeanServerNotifications concerning given ObjectName.
099: *
100: * @param objectName ObjectName of interest
101: *
102: * @exception IllegalArgumentException if the given ObjectName is null
103: */
104: public synchronized void enableObjectName(ObjectName objectName)
105: throws IllegalArgumentException {
106:
107: if (objectName == null)
108: throw new IllegalArgumentException(
109: "Invalid parameter:Null ObjectName");
110:
111: if (deselectedNames.contains(objectName))
112: deselectedNames.remove(objectName);
113:
114: if (!(selectedNames.contains(objectName)))
115: selectedNames.add(objectName);
116: }
117:
118: synchronized void enableObjectNames(ObjectName[] objectNames)
119: throws IllegalArgumentException {
120: for (int i = 0; i < objectNames.length; i++) {
121: this .enableObjectName(objectNames[i]);
122: }
123: }
124:
125: /**
126: * Gets all the ObjectNames enabled.
127: *
128: * @return Vector of ObjectNames:
129: * <P>- null means all ObjectNames are implicitly selected, except the
130: * ObjectNames explicitly deselected
131: * <P>- empty means all ObjectNames are deselected, i.e. no ObjectName
132: * selected.
133: */
134: public synchronized Vector getEnabledObjectNames() {
135: if (enableAll)
136: return null;
137: return new Vector(selectedNames);
138: }
139:
140: /**
141: * Gets all the ObjectNames disabled.
142: *
143: * @return Vector of ObjectNames:
144: * <P>- null means all ObjectNames are implicitly deselected, except the
145: * ObjectNames explicitly selected
146: * <P>- empty means all ObjectNames are selected, i.e. no ObjectName
147: * deselected.
148: */
149: public synchronized Vector getDisabledObjectNames() {
150: if (disableAll)
151: return null;
152: return new Vector(deselectedNames);
153: }
154:
155: //
156: // NotificationFilter interface
157: //
158:
159: /**
160: * Invoked before sending the specified notification to the listener.
161: * <P>If:
162: * <P>- the ObjectName of the concerned MBean is selected (explicitly OR
163: * (implicitly and not explicitly deselected))
164: * <P>AND
165: * <P>- the type of the operation (registration or unregistration) is
166: * selected
167: * <P>then the notification is sent to the listener.
168: *
169: * @param notif The notification to be sent.
170: *
171: * @return true if the notification has to be sent to the listener, false
172: * otherwise.
173: *
174: * @exception IllegalArgumentException if null parameter
175: */
176: public synchronized boolean isNotificationEnabled(Notification notif)
177: throws IllegalArgumentException {
178:
179: if (notif == null)
180: throw new IllegalArgumentException(
181: "Invalid parameter: null notification");
182:
183: if (!super .isNotificationEnabled(notif))
184: return false; // type check
185:
186: // Check the ObjectName
187: MBeanServerNotification mbsNtf = (MBeanServerNotification) notif;
188: ObjectName objName = mbsNtf.getMBeanName();
189:
190: if (selectedNames.contains(objName)) { // select explicit, return true
191: return true;
192: } else if (enableAll && !deselectedNames.contains(objName)) { // not select explicit,but enable all && not deselect explicit
193: // if enable all ,must be not disable all
194: return true;
195: }
196:
197: return false; // else return false;
198:
199: }
200:
201: }
|