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.AttributeChangeNotification; 025 import javax.management.Notification; 026 import javax.management.NotificationFilter; 027 028 029 /** 030 * <p>Implementation of <code>NotificationFilter</code> for attribute change 031 * notifications. This class is used by <code>BaseModelMBean</code> to 032 * construct attribute change notification event filters when a filter is not 033 * supplied by the application.</p> 034 * 035 * @author Craig R. McClanahan 036 * @version $Revision: 480402 $ $Date: 2006-11-29 05:43:23 +0100 (Wed, 29 Nov 2006) $ 037 */ 038 039 public class BaseAttributeFilter implements NotificationFilter { 040 041 042 // ----------------------------------------------------------- Constructors 043 044 045 /** 046 * Construct a new filter that accepts only the specified attribute 047 * name. 048 * 049 * @param name Name of the attribute to be accepted by this filter, or 050 * <code>null</code> to accept all attribute names 051 */ 052 public BaseAttributeFilter(String name) { 053 054 super(); 055 if (name != null) 056 addAttribute(name); 057 058 } 059 060 061 // ----------------------------------------------------- Instance Variables 062 063 064 /** 065 * The set of attribute names that are accepted by this filter. If this 066 * list is empty, all attribute names are accepted. 067 */ 068 private HashSet names = new HashSet(); 069 070 071 // --------------------------------------------------------- Public Methods 072 073 074 /** 075 * Add a new attribute name to the set of names accepted by this filter. 076 * 077 * @param name Name of the attribute to be accepted 078 */ 079 public void addAttribute(String name) { 080 081 synchronized (names) { 082 names.add(name); 083 } 084 085 } 086 087 088 /** 089 * Clear all accepted names from this filter, so that it will accept 090 * all attribute names. 091 */ 092 public void clear() { 093 094 synchronized (names) { 095 names.clear(); 096 } 097 098 } 099 100 101 /** 102 * Return the set of names that are accepted by this filter. If this 103 * filter accepts all attribute names, a zero length array will be 104 * returned. 105 */ 106 public String[] getNames() { 107 108 synchronized (names) { 109 return ((String[]) names.toArray(new String[names.size()])); 110 } 111 112 } 113 114 115 /** 116 * <p>Test whether notification enabled for this event. 117 * Return true if:</p> 118 * <ul> 119 * <li>This is an attribute change notification</li> 120 * <li>Either the set of accepted names is empty (implying that all 121 * attribute names are of interest) or the set of accepted names 122 * includes the name of the attribute in this notification</li> 123 * </ul> 124 */ 125 public boolean isNotificationEnabled(Notification notification) { 126 127 if (notification == null) 128 return (false); 129 if (!(notification instanceof AttributeChangeNotification)) 130 return (false); 131 AttributeChangeNotification acn = 132 (AttributeChangeNotification) notification; 133 if (!AttributeChangeNotification.ATTRIBUTE_CHANGE.equals(acn.getType())) 134 return (false); 135 synchronized (names) { 136 if (names.size() < 1) 137 return (true); 138 else 139 return (names.contains(acn.getAttributeName())); 140 } 141 142 } 143 144 145 /** 146 * Remove an attribute name from the set of names accepted by this 147 * filter. 148 * 149 * @param name Name of the attribute to be removed 150 */ 151 public void removeAttribute(String name) { 152 153 synchronized (names) { 154 names.remove(name); 155 } 156 157 } 158 159 160 }