KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > MappingFilterImpl


1 package org.jacorb.notification.filter;
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.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.logger.Logger;
29 import org.jacorb.notification.conf.Attributes;
30 import org.jacorb.notification.conf.Default;
31 import org.jacorb.notification.interfaces.Disposable;
32 import org.jacorb.notification.interfaces.GCDisposable;
33 import org.jacorb.notification.util.DisposableManager;
34 import org.jacorb.notification.util.LogUtil;
35 import org.omg.CORBA.Any JavaDoc;
36 import org.omg.CORBA.AnyHolder JavaDoc;
37 import org.omg.CORBA.ORB JavaDoc;
38 import org.omg.CORBA.TypeCode JavaDoc;
39 import org.omg.CosNotification.Property;
40 import org.omg.CosNotification.StructuredEvent;
41 import org.omg.CosNotifyFilter.ConstraintExp;
42 import org.omg.CosNotifyFilter.ConstraintInfo;
43 import org.omg.CosNotifyFilter.ConstraintNotFound;
44 import org.omg.CosNotifyFilter.InvalidConstraint;
45 import org.omg.CosNotifyFilter.InvalidValue;
46 import org.omg.CosNotifyFilter.MappingConstraintInfo;
47 import org.omg.CosNotifyFilter.MappingConstraintPair;
48 import org.omg.CosNotifyFilter.MappingFilterOperations;
49 import org.omg.CosNotifyFilter.MappingFilterPOATie;
50 import org.omg.CosNotifyFilter.UnsupportedFilterableData;
51
52 /**
53  * @author Alphonse Bendt
54  * @version $Id: MappingFilterImpl.java,v 1.3 2005/04/10 14:21:32 alphonse.bendt Exp $
55  */

56
57 public class MappingFilterImpl implements GCDisposable, MappingFilterOperations
58 {
59     /**
60      * map constraint ids used by class FilterImpl to default values used by MappingFilterImpl.
61      */

62     private static class ValueMap
63     {
64         private final Map JavaDoc valueMap_ = new Hashtable JavaDoc();
65
66         public void put(int key, Any JavaDoc value)
67         {
68             valueMap_.put(new Integer JavaDoc(key), value);
69         }
70
71         public Any JavaDoc get(int key)
72         {
73             return (Any JavaDoc) valueMap_.get(new Integer JavaDoc(key));
74         }
75
76         public Any JavaDoc remove(int key)
77         {
78             return (Any JavaDoc) valueMap_.remove(new Integer JavaDoc(key));
79         }
80
81         public void dispose()
82         {
83             valueMap_.clear();
84         }
85     }
86
87     // //////////////////////////////////////
88

89     private final AbstractFilter filterDelegate_;
90
91     private final Any JavaDoc defaultValue_;
92
93     private final ValueMap valueMap_ = new ValueMap();
94
95     private final Logger logger_;
96
97     private final MappingFilterPOATie servant_;
98
99     private final MappingFilterUsageDecorator usageDecorator_;
100
101     private final ORB JavaDoc orb_;
102
103     private final DisposableManager disposeHooks_ = new DisposableManager();
104
105     private final long maxIdleTime_;
106
107     // //////////////////////////////////////
108

109     public MappingFilterImpl(ORB JavaDoc orb, Configuration config, AbstractFilter filter, Any JavaDoc defaultValue)
110     {
111         orb_ = orb;
112         logger_ = LogUtil.getLogger(config, getClass().getName());
113
114         filterDelegate_ = filter;
115         defaultValue_ = defaultValue;
116
117         usageDecorator_ = new MappingFilterUsageDecorator(this);
118
119         servant_ = new MappingFilterPOATie(usageDecorator_.getMappingFilterOperations());
120
121         maxIdleTime_ = config.getAttributeAsLong(Attributes.DEAD_FILTER_INTERVAL,
122                 Default.DEFAULT_DEAD_FILTER_INTERVAL);
123     }
124
125     // /////////////////////////////////////
126

127     public org.omg.CORBA.Object JavaDoc activate()
128     {
129         return servant_._this(orb_);
130     }
131
132     public void destroy()
133     {
134         logger_.info("destroy MappingFilter");
135
136         dispose();
137     }
138
139     public void dispose()
140     {
141         disposeHooks_.dispose();
142
143         filterDelegate_.dispose();
144
145         valueMap_.dispose();
146     }
147
148     public String JavaDoc constraint_grammar()
149     {
150         return filterDelegate_.constraint_grammar();
151     }
152
153     public TypeCode JavaDoc value_type()
154     {
155         return defaultValue_.type();
156     }
157
158     public Any JavaDoc default_value()
159     {
160         return defaultValue_;
161     }
162
163     public MappingConstraintInfo[] add_mapping_constraints(MappingConstraintPair[] mcp)
164             throws InvalidValue, InvalidConstraint
165     {
166         ConstraintExp[] _constraintExpArray = new ConstraintExp[mcp.length];
167
168         for (int x = 0; x < mcp.length; ++x)
169         {
170             _constraintExpArray[x] = mcp[x].constraint_expression;
171         }
172
173         ConstraintInfo[] _constraintInfo = filterDelegate_.add_constraints(_constraintExpArray);
174
175         MappingConstraintInfo[] _mappingConstraintInfo = new MappingConstraintInfo[_constraintInfo.length];
176
177         for (int x = 0; x < _constraintInfo.length; ++x)
178         {
179             _mappingConstraintInfo[x] = new MappingConstraintInfo(
180                     _constraintInfo[x].constraint_expression, _constraintInfo[x].constraint_id,
181                     mcp[x].result_to_set);
182
183             valueMap_.put(_constraintInfo[x].constraint_id, mcp[x].result_to_set);
184         }
185
186         return _mappingConstraintInfo;
187     }
188
189     public void modify_mapping_constraints(int[] intArray,
190             MappingConstraintInfo[] mappingConstraintInfos) throws ConstraintNotFound,
191             InvalidValue, InvalidConstraint
192     {
193         ConstraintInfo[] _constraintInfo = new ConstraintInfo[mappingConstraintInfos.length];
194
195         for (int x = 0; x < _constraintInfo.length; ++x)
196         {
197             _constraintInfo[x] = new ConstraintInfo(
198                     mappingConstraintInfos[x].constraint_expression,
199                     mappingConstraintInfos[x].constraint_id);
200
201             valueMap_.remove(mappingConstraintInfos[x].constraint_id);
202         }
203
204         filterDelegate_.modify_constraints(intArray, _constraintInfo);
205
206         for (int x = 0; x < mappingConstraintInfos.length; ++x)
207         {
208             valueMap_.put(mappingConstraintInfos[x].constraint_id, mappingConstraintInfos[x].value);
209         }
210     }
211
212     public MappingConstraintInfo[] get_mapping_constraints(int[] constraintIds)
213             throws ConstraintNotFound
214     {
215         ConstraintInfo[] _constraintInfo = filterDelegate_.get_constraints(constraintIds);
216
217         MappingConstraintInfo[] _mappingConstraintInfo = new MappingConstraintInfo[_constraintInfo.length];
218
219         for (int x = 0; x < _constraintInfo.length; ++x)
220         {
221             _mappingConstraintInfo[x] = new MappingConstraintInfo(
222                     _constraintInfo[x].constraint_expression, _constraintInfo[x].constraint_id,
223                     valueMap_.get(_constraintInfo[x].constraint_id));
224         }
225
226         return _mappingConstraintInfo;
227     }
228
229     public MappingConstraintInfo[] get_all_mapping_constraints()
230     {
231         ConstraintInfo[] _constraintInfo = filterDelegate_.get_all_constraints();
232
233         MappingConstraintInfo[] _mappingConstraintInfo = new MappingConstraintInfo[_constraintInfo.length];
234
235         for (int x = 0; x < _constraintInfo.length; ++x)
236         {
237             _mappingConstraintInfo[x] = new MappingConstraintInfo(
238                     _constraintInfo[x].constraint_expression, _constraintInfo[x].constraint_id,
239                     valueMap_.get(_constraintInfo[x].constraint_id));
240         }
241
242         return _mappingConstraintInfo;
243     }
244
245     public void remove_all_mapping_constraints()
246     {
247         filterDelegate_.remove_all_constraints();
248
249         valueMap_.dispose();
250     }
251
252     public boolean match(Any JavaDoc any, AnyHolder JavaDoc anyHolder) throws UnsupportedFilterableData
253     {
254         int _filterId = filterDelegate_.match_internal(any);
255
256         if (_filterId >= 0)
257         {
258             anyHolder.value = valueMap_.get(_filterId);
259
260             return true;
261         }
262         return false;
263     }
264
265     public boolean match_structured(StructuredEvent structuredEvent, AnyHolder JavaDoc anyHolder)
266             throws UnsupportedFilterableData
267     {
268         final int _filterId = filterDelegate_.match_structured_internal(structuredEvent);
269
270         if (_filterId >= 0)
271         {
272             anyHolder.value = valueMap_.get(_filterId);
273
274             return true;
275         }
276
277         return false;
278     }
279
280     public boolean match_typed(Property[] propertyArray, AnyHolder JavaDoc anyHolder)
281             throws UnsupportedFilterableData
282     {
283         final int _filterId = filterDelegate_.match_typed_internal(propertyArray);
284
285         if (_filterId >= 0)
286         {
287             anyHolder.value = valueMap_.get(_filterId);
288
289             return true;
290         }
291
292         return false;
293     }
294
295     public void attemptDispose()
296     {
297         AbstractFilter.attemptDispose(this, usageDecorator_.getLastUsage(), maxIdleTime_);
298     }
299
300     public void addDisposeHook(Disposable d)
301     {
302         disposeHooks_.addDisposable(d);
303     }
304 }
Popular Tags