001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    
019    package org.apache.commons.modeler;
020    
021    
022    import java.util.HashSet;
023    
024    import javax.management.Notification;
025    import javax.management.NotificationFilter;
026    
027    
028    /**
029     * Special NotificationFilter that allows modeler to optimize its notifications.
030     *
031     * This class is immutable - after you construct it it'll filter based on
032     * a fixed set of notification names.
033     *
034     * The JMX specification requires the filters to be called before the
035     * notifications are sent. We can call this filter well in advance, when
036     * the listener is added. Based on the result we can maintain separate
037     * channels for each notification - and reduce the overhead.
038     *
039     * @author Costin Manolache
040     */
041    public class FixedNotificationFilter implements NotificationFilter {
042    
043        /**
044         * The set of attribute names that are accepted by this filter.  If this
045         * list is empty, all attribute names are accepted.
046         */
047        private HashSet names = new HashSet();
048        String namesA[]=null;
049    
050        /**
051         * Construct a new filter that accepts only the specified notification
052         * names.
053         *
054         * @param names Names of the notification types
055         */
056        public FixedNotificationFilter(String names[]) {
057            super();
058        }
059    
060        /**
061         * Return the set of names that are accepted by this filter.  If this
062         * filter accepts all attribute names, a zero length array will be
063         * returned.
064         */
065        public String[] getNames() {
066            synchronized (names) {
067                return ((String[]) names.toArray(new String[names.size()]));
068            }
069        }
070    
071    
072        /**
073         * <p>Test whether notification enabled for this event.
074         * Return true if:</p>
075         * <ul>
076         * <li>Either the set of accepted names is empty (implying that all
077         *     attribute names are of interest) or the set of accepted names
078         *     includes the name of the attribute in this notification</li>
079         * </ul>
080         */
081        public boolean isNotificationEnabled(Notification notification) {
082    
083            if (notification == null)
084                return (false);
085            synchronized (names) {
086                if (names.size() < 1)
087                    return (true);
088                else
089                    return (names.contains(notification.getType()));
090            }
091    
092        }
093    
094    
095    }