View Javadoc

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