KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > JBossNotificationFilterSupport


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationFilter JavaDoc;
31
32 import org.jboss.util.collection.CollectionsFactory;
33
34 /**
35  * A replacement for {@link javax.management.NotificationFilterSupport}
36  * that avoids synchronization when reading the enabled notification types
37  * by using copy-on-write semantics.
38  *
39  * Reading operation operate on the latest snapshot of the enabledTypes.
40  *
41  * Mutating operations synchronize on 'this', only because of the
42  * addIfAbsent logic in enableType(). This could be avoided by
43  * using java.util.concurrent or EDU.oswego.cs.dl.util.concurrent
44  * directly, rather than org.jboss.util.CollectionsFactory.
45  *
46  * In any case, mutating operations are rare when dealing with
47  * NotificationFilters. The common usage is to configure it once and
48  * be done with it.
49  *
50  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
51  * @version $Revision: 41840 $
52  * @since 4.0.3
53  */

54 public class JBossNotificationFilterSupport
55    implements NotificationFilter JavaDoc, Serializable JavaDoc
56 {
57    // Constants ---------------------------------------------------
58

59    private static final long serialVersionUID = 6442164418782871672L;
60
61    // Attributes --------------------------------------------------
62

63    /**
64     * Enabled notification types.
65     */

66    private List JavaDoc enabledTypes;
67
68    // Static ------------------------------------------------------
69

70    // Constructors ------------------------------------------------
71

72    /**
73     * Default CTOR.
74     *
75     * Create a filter that filters out all notification types.
76     */

77    public JBossNotificationFilterSupport()
78    {
79       enabledTypes = CollectionsFactory.createCopyOnWriteList();
80    }
81
82    // Public ------------------------------------------------------
83

84    /**
85     * Disable all notification types. Rejects all notifications.
86     */

87    public void disableAllTypes()
88    {
89       synchronized(this)
90       {
91          enabledTypes.clear();
92       }
93    }
94
95    /**
96     * Disable a notification type.
97     *
98     * @param type the notification type to disable.
99     */

100    public void disableType(String JavaDoc type)
101    {
102       synchronized(this)
103       {
104          // Null won't be in the list anyway.
105
enabledTypes.remove(type);
106       }
107    }
108
109    /**
110     * Enable a notification type.
111     *
112     * @param type the notification type to enable.
113     * @exception IllegalArgumentException for a null type
114     */

115    public void enableType(String JavaDoc type) throws IllegalArgumentException JavaDoc
116    {
117       if (type == null)
118       {
119          throw new IllegalArgumentException JavaDoc("null notification type");
120       }
121       synchronized(this)
122       {
123          if (enabledTypes.contains(type) == false)
124          {
125             enabledTypes.add(type);
126          }
127       }
128    }
129
130    /**
131     * Get all the enabled notification types.<p>
132     *
133     * Returns a vector of enabled notification type.<br>
134     * An empty vector means all types disabled.
135     *
136     * @return the vector of enabled types.
137     */

138    public Vector JavaDoc getEnabledTypes()
139    {
140       return new Vector JavaDoc(enabledTypes);
141    }
142
143    /**
144     * @return human readable string.
145     */

146    public String JavaDoc toString()
147    {
148       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
149       
150       sb.append(getClass().getName()).append(':');
151       sb.append(" enabledTypes=").append(getEnabledTypes());
152       
153       return sb.toString();
154    }
155    
156    // NotificationFilter implementation ---------------------------
157

158    /**
159     * Test to see whether this notification is enabled
160     *
161     * @param notification the notification to filter
162     * @return true when the notification should be sent, false otherwise
163     * @exception IllegalArgumentException for null notification.
164     */

165    public boolean isNotificationEnabled(Notification JavaDoc notification)
166    {
167       if (notification == null)
168       {
169          throw new IllegalArgumentException JavaDoc("null notification");
170       }
171       // Check if it is enabled
172
String JavaDoc notificationType = notification.getType();
173       for (Iterator JavaDoc i = enabledTypes.iterator(); i.hasNext(); )
174       {
175          String JavaDoc type = (String JavaDoc)i.next();
176          if (notificationType.startsWith(type))
177          {
178             return true;
179          }
180       }
181       return false;
182    }
183
184    // Private -----------------------------------------------------
185

186 }
187
Popular Tags