KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > engine > FilterProxySupplierTask


1 package org.jacorb.notification.engine;
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.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.jacorb.notification.interfaces.FilterStage;
29 import org.jacorb.notification.interfaces.Message;
30 import org.omg.CORBA.AnyHolder JavaDoc;
31 import org.omg.CosNotifyFilter.UnsupportedFilterableData;
32
33 /**
34  * @author Alphonse Bendt
35  * @version $Id: FilterProxySupplierTask.java,v 1.19 2005/04/27 10:48:40 alphonse.bendt Exp $
36  */

37
38 public class FilterProxySupplierTask extends AbstractFilterTask
39 {
40     static class AlternateMessageMap
41     {
42         private final Map JavaDoc alternateMessages_;
43
44         //////////////////////////////
45

46         public AlternateMessageMap()
47         {
48             this(new HashMap JavaDoc());
49         }
50
51         AlternateMessageMap(Map JavaDoc m)
52         {
53             alternateMessages_ = m;
54         }
55
56         //////////////////////////////
57

58         public Message getAlternateMessage(FilterStage s)
59         {
60             if (alternateMessages_.containsKey(s))
61             {
62                 return (Message) alternateMessages_.get(s);
63             }
64             return null;
65         }
66
67         public void putAlternateMessage(FilterStage s, Message e)
68         {
69             alternateMessages_.put(s, e);
70         }
71
72         public void clear()
73         {
74             alternateMessages_.clear();
75         }
76     }
77
78     public static final AlternateMessageMap EMPTY_MAP = new AlternateMessageMap(
79             Collections.EMPTY_MAP)
80     {
81         public void clear()
82         {
83         }
84     };
85
86     ////////////////////////////////////////
87

88     final AlternateMessageMap changedMessages_ = new AlternateMessageMap();
89
90     private static int sCount = 0;
91
92     private int id_ = ++sCount;
93
94     ////////////////////////////////////////
95

96     public FilterProxySupplierTask(TaskFactory taskFactory, TaskExecutor taskExecutor)
97     {
98         super(taskFactory, taskExecutor);
99     }
100
101     ////////////////////////////////////////
102

103     public String JavaDoc toString()
104     {
105         return "[FilterProxySupplierTask#" + id_ + "]";
106     }
107
108     public void reset()
109     {
110         super.reset();
111
112         changedMessages_.clear();
113     }
114
115     public void doFilter() throws InterruptedException JavaDoc
116     {
117         filter();
118
119         getTaskFactory().enqueueMessage(this);
120     }
121
122     private Message updatePriority(int indexOfCurrentEvent, Message message)
123     {
124         AnyHolder JavaDoc _priorityFilterResult = new AnyHolder JavaDoc();
125
126         Message _currentMessage = message;
127
128         try
129         {
130             boolean priorityMatch = message.match(arrayCurrentFilterStage_[indexOfCurrentEvent]
131                     .getPriorityFilter(), _priorityFilterResult);
132
133             if (priorityMatch)
134             {
135                 _currentMessage = (Message) getMessage().clone();
136
137                 _currentMessage.setPriority(_priorityFilterResult.value.extract_long());
138             }
139         } catch (UnsupportedFilterableData e)
140         {
141             // logger_.error("error evaluating PriorityFilter", e);
142
}
143
144         return _currentMessage;
145     }
146
147     private Message updateTimeout(int indexOfCurrentFilterStage, Message message)
148     {
149         AnyHolder JavaDoc _lifetimeFilterResult = new AnyHolder JavaDoc();
150         Message _currentMessage = message;
151
152         try
153         {
154             boolean lifetimeMatch = _currentMessage.match(
155                     arrayCurrentFilterStage_[indexOfCurrentFilterStage].getLifetimeFilter(),
156                     _lifetimeFilterResult);
157
158             if (lifetimeMatch && (_currentMessage == getMessage()))
159             {
160                 // LifeTime Mapping Filter matched and current Message
161
// was not copied yet. This depends on the fact that
162
// updatePriority was run before.
163

164                 _currentMessage = (Message) getMessage().clone();
165
166                 _currentMessage.setTimeout(_lifetimeFilterResult.value.extract_long());
167             }
168
169         } catch (UnsupportedFilterableData e)
170         {
171             // logger_.error("error evaluating PriorityFilter", e);
172
}
173
174         return _currentMessage;
175     }
176
177     private void filter()
178     {
179         for (int x = 0; x < arrayCurrentFilterStage_.length; ++x)
180         {
181             boolean _forward = false;
182
183             if (!arrayCurrentFilterStage_[x].isDisposed())
184             {
185                 Message _currentMessage = getMessage();
186
187                 if (arrayCurrentFilterStage_[x].hasPriorityFilter())
188                 {
189                     _currentMessage = updatePriority(x, _currentMessage);
190                 }
191
192                 if (arrayCurrentFilterStage_[x].hasLifetimeFilter())
193                 {
194                     _currentMessage = updateTimeout(x, _currentMessage);
195                 }
196
197                 if (_currentMessage != getMessage())
198                 {
199                     // MappingFilter attached to a particular
200
// FilterStage did change (Timeout or Priority)
201
// the current Message.
202
// store changed Message in Map for later use.
203
changedMessages_.putAlternateMessage(arrayCurrentFilterStage_[x],
204                             _currentMessage);
205                 }
206
207                 _forward = _currentMessage.match(arrayCurrentFilterStage_[x]);
208             }
209
210             if (_forward)
211             {
212                 // the subsequent destination filters need to be eval'd
213
addFilterStage(arrayCurrentFilterStage_[x].getSubsequentFilterStages());
214             }
215         }
216     }
217 }
Popular Tags