View Javadoc

1   /*
2    * Copyright 1999,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.commons.modeler;
19  
20  
21  import java.util.HashSet;
22  
23  import javax.management.Notification;
24  import javax.management.NotificationFilter;
25  
26  
27  /***
28   * Special NotificationFilter that allows modeler to optimize its notifications.
29   *
30   * This class is immutable - after you construct it it'll filter based on
31   * a fixed set of notification names.
32   *
33   * The JMX specification requires the filters to be called before the
34   * notifications are sent. We can call this filter well in advance, when
35   * the listener is added. Based on the result we can maintain separate
36   * channels for each notification - and reduce the overhead.
37   *
38   * @author Costin Manolache
39   */
40  public class FixedNotificationFilter implements NotificationFilter {
41  
42      /***
43       * The set of attribute names that are accepted by this filter.  If this
44       * list is empty, all attribute names are accepted.
45       */
46      private HashSet names = new HashSet();
47      String namesA[]=null;
48  
49      /***
50       * Construct a new filter that accepts only the specified notification
51       * names.
52       *
53       * @param names Names of the notification types
54       */
55      public FixedNotificationFilter(String names[]) {
56          super();
57      }
58  
59      /***
60       * Return the set of names that are accepted by this filter.  If this
61       * filter accepts all attribute names, a zero length array will be
62       * returned.
63       */
64      public String[] getNames() {
65          synchronized (names) {
66              return ((String[]) names.toArray(new String[names.size()]));
67          }
68      }
69  
70  
71      /***
72       * <p>Test whether notification enabled for this event.
73       * Return true if:</p>
74       * <ul>
75       * <li>Either the set of accepted names is empty (implying that all
76       *     attribute names are of interest) or the set of accepted names
77       *     includes the name of the attribute in this notification</li>
78       * </ul>
79       */
80      public boolean isNotificationEnabled(Notification notification) {
81  
82          if (notification == null)
83              return (false);
84          synchronized (names) {
85              if (names.size() < 1)
86                  return (true);
87              else
88                  return (names.contains(notification.getType()));
89          }
90  
91      }
92  
93  
94  }