KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > FilterManager


1 package org.jacorb.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.avalon.framework.logger.Logger;
31 import org.jacorb.notification.util.LogUtil;
32 import org.omg.CosNotifyFilter.Filter;
33 import org.omg.CosNotifyFilter.FilterAdminOperations;
34 import org.omg.CosNotifyFilter.FilterNotFound;
35
36 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
37
38 /**
39  * @author Alphonse Bendt
40  * @version $Id: FilterManager.java,v 1.15 2005/05/04 13:19:56 alphonse.bendt Exp $
41  */

42
43 public class FilterManager implements FilterAdminOperations
44 {
45     public static final FilterManager EMPTY_FILTER_MANAGER = new FilterManager(Collections.EMPTY_MAP);
46
47     private static final Integer JavaDoc[] INTEGER_ARRAY_TEMPLATE = new Integer JavaDoc[0];
48
49     ////////////////////////////////////////
50

51     private final Map JavaDoc filters_;
52
53     private final Object JavaDoc filtersLock_ = new Object JavaDoc();
54
55     private boolean filtersModified_;
56
57     private final List JavaDoc filterList_ = new ArrayList JavaDoc();
58
59     private final List JavaDoc filtersReadOnlyView_ = Collections.unmodifiableList(filterList_);
60
61     private final SynchronizedInt filterIdPool_ = new SynchronizedInt(0);
62
63     private final Logger logger_;
64     
65     ////////////////////////////////////////
66

67     protected FilterManager(Map JavaDoc filters)
68     {
69         filters_ = filters;
70
71         filtersModified_ = true;
72         
73         logger_ = LogUtil.getLogger(getClass().getName());
74     }
75
76     public FilterManager()
77     {
78         this(new HashMap JavaDoc());
79     }
80
81     ////////////////////////////////////////
82

83     private Integer JavaDoc getFilterId()
84     {
85         return new Integer JavaDoc(filterIdPool_.increment());
86     }
87
88     public int add_filter(Filter filter)
89     {
90         Integer JavaDoc _key = getFilterId();
91
92         if (logger_.isWarnEnabled())
93         {
94             try
95             {
96                 if (!((org.omg.CORBA.portable.ObjectImpl JavaDoc) filter)._is_local())
97                 {
98                     logger_.warn("filter is not local!");
99                 }
100             } catch (Exception JavaDoc e)
101             {
102                 // ignored
103
}
104         }
105
106         synchronized (filtersLock_)
107         {
108             filters_.put(_key, filter);
109
110             filtersModified_ = true;
111         }
112
113         return _key.intValue();
114     }
115
116     public void remove_filter(int filterId) throws FilterNotFound
117     {
118         Integer JavaDoc _key = new Integer JavaDoc(filterId);
119
120         synchronized (filtersLock_)
121         {
122             if (filters_.containsKey(_key))
123             {
124                 filters_.remove(_key);
125                 filtersModified_ = true;
126             }
127             else
128             {
129                 throwFilterNotFound(_key);
130             }
131         }
132     }
133
134     public Filter get_filter(int filterId) throws FilterNotFound
135     {
136         Integer JavaDoc _key = new Integer JavaDoc(filterId);
137
138         final Filter _filter;
139
140         synchronized (filtersLock_)
141         {
142             _filter = (Filter) filters_.get(_key);
143         }
144
145         if (_filter == null)
146         {
147             throwFilterNotFound(_key);
148         }
149
150         return _filter;
151     }
152
153     private void throwFilterNotFound(Integer JavaDoc filterId) throws FilterNotFound
154     {
155         throw new FilterNotFound("Filter with ID=" + filterId + " does not exist");
156     }
157
158     public int[] get_all_filters()
159     {
160         final Integer JavaDoc[] _keys;
161
162         synchronized (filtersLock_)
163         {
164             _keys = (Integer JavaDoc[]) filters_.keySet().toArray(INTEGER_ARRAY_TEMPLATE);
165         }
166
167         final int[] _intKeys = new int[_keys.length];
168
169         for (int x = 0; x < _keys.length; ++x)
170         {
171             _intKeys[x] = _keys[x].intValue();
172         }
173
174         return _intKeys;
175     }
176
177     public void remove_all_filters()
178     {
179         synchronized (filtersLock_)
180         {
181             filters_.clear();
182             filtersModified_ = true;
183         }
184     }
185
186     public List JavaDoc getFilters()
187     {
188         synchronized (filtersLock_)
189         {
190             if (filtersModified_)
191             {
192                 filterList_.clear();
193
194                 filterList_.addAll(filters_.values());
195
196                 filtersModified_ = false;
197             }
198         }
199         return filtersReadOnlyView_;
200     }
201 }
202
Popular Tags