KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26
27 import org.apache.avalon.framework.logger.Logger;
28 import org.jacorb.notification.AbstractMessage;
29 import org.jacorb.notification.filter.etcl.AbstractTCLNode;
30 import org.jacorb.notification.filter.etcl.ArrayOperator;
31 import org.jacorb.notification.filter.etcl.AssocOperator;
32 import org.jacorb.notification.filter.etcl.ETCLComponentName;
33 import org.jacorb.notification.filter.etcl.IdentValue;
34 import org.jacorb.notification.filter.etcl.ImplicitOperator;
35 import org.jacorb.notification.filter.etcl.ImplicitOperatorNode;
36 import org.jacorb.notification.filter.etcl.NumberValue;
37 import org.jacorb.notification.filter.etcl.UnionPositionOperator;
38 import org.jacorb.notification.interfaces.Message;
39 import org.jacorb.notification.util.AbstractPoolable;
40 import org.jacorb.notification.util.LogUtil;
41 import org.omg.CORBA.Any JavaDoc;
42
43 /**
44  * @todo remove the static dependeny to package filter.etcl.
45  * @author Alphonse Bendt
46  * @version $Id: EvaluationContext.java,v 1.7 2005/02/14 00:04:35 alphonse.bendt Exp $
47  */

48
49 public class EvaluationContext extends AbstractPoolable
50 {
51     private final ETCLEvaluator dynamicEvaluator_;
52
53     private Message message_;
54
55     private final Map JavaDoc resultCache_;
56
57     private final Map JavaDoc anyCache_;
58
59     private final Logger logger_ = LogUtil.getLogger(getClass().getName());
60
61     ////////////////////////////////////////
62

63     public EvaluationContext(ETCLEvaluator evaluator)
64     {
65         dynamicEvaluator_ = evaluator;
66
67         resultCache_ = new WeakHashMap JavaDoc();
68         anyCache_ = new WeakHashMap JavaDoc();
69     }
70
71     ////////////////////////////////////////
72

73     public void reset()
74     {
75         resultCache_.clear();
76         anyCache_.clear();
77     }
78
79     public ETCLEvaluator getDynamicEvaluator()
80     {
81         return dynamicEvaluator_;
82     }
83
84     public Message getCurrentMessage()
85     {
86         return message_;
87     }
88
89     public void setCurrentMessage(Message message)
90     {
91         message_ = message;
92     }
93
94     public void storeResult(String JavaDoc name, EvaluationResult value)
95     {
96         resultCache_.put(name, value);
97     }
98
99     public EvaluationResult lookupResult(String JavaDoc name)
100     {
101         return (EvaluationResult) resultCache_.get(name);
102     }
103
104     public void eraseResult(String JavaDoc name)
105     {
106         resultCache_.remove(name);
107     }
108
109     public void storeAny(String JavaDoc name, Any JavaDoc any)
110     {
111         anyCache_.put(name, any);
112     }
113
114     public Any JavaDoc lookupAny(String JavaDoc name)
115     {
116         return (Any JavaDoc) anyCache_.get(name);
117     }
118
119     public void eraseAny(String JavaDoc name)
120     {
121         anyCache_.remove(name);
122     }
123
124     /**
125      * resolve the RuntimeVariable (e.g. $curtime). then see if some more work has to be done (e.g.
126      * $curtime._repos_id)
127      */

128     public EvaluationResult extractFromMessage(EvaluationResult evaluationResult,
129             ComponentName componentName, RuntimeVariable runtimeVariable)
130             throws EvaluationException
131     {
132         ETCLComponentName _componentName = (ETCLComponentName) componentName;
133
134         if (_componentName.right() != null)
135         {
136             return extractFromAny(_componentName.right(), evaluationResult.getAny(),
137                     runtimeVariable.toString());
138         }
139
140         return evaluationResult;
141     }
142
143     /**
144      * fetch the values denoted by the provided ComponentName out of the Message.
145      */

146     public EvaluationResult extractFromMessage(AbstractMessage message,
147             ComponentName componentName) throws EvaluationException
148     {
149         ETCLComponentName _componentName = (ETCLComponentName) componentName;
150
151         return extractFromAny(_componentName.left(), message.toAny(), _componentName.toString());
152     }
153
154     private EvaluationResult extractFromAny(AbstractTCLNode expr, Any JavaDoc any, String JavaDoc rootName)
155             throws EvaluationException
156     {
157         if (logger_.isDebugEnabled())
158         {
159             logger_.debug("extractFromAny" + "\n\trootname=" + rootName + "\n\tvalue=" + any);
160         }
161
162         EvaluationResult _ret = null;
163         Any JavaDoc _result = null;
164
165         AbstractTCLNode _currentOperator = expr;
166         Any JavaDoc _currentAny = any;
167
168         StringBuffer JavaDoc _currentPath = new StringBuffer JavaDoc(rootName);
169
170         while (_currentOperator != null)
171         {
172             _currentPath.append(_currentOperator.toString());
173
174             if (logger_.isDebugEnabled())
175             {
176                 logger_.debug("current path=" + _currentPath.toString());
177                 logger_.debug("current operator=" + _currentOperator.toString());
178                 logger_.debug("current any=" + _currentAny);
179             }
180
181             // lookup result in cache
182
_result = lookupAny(_currentPath.toString());
183
184             if (_result == null)
185             // cache MISS
186
{
187                 switch (_currentOperator.getType()) {
188                 case AbstractTCLNode.DOT:
189                     // dots are skipped
190
break;
191
192                 case AbstractTCLNode.UNION_POS:
193                     logger_.debug("evaluate union by position");
194                     UnionPositionOperator _upo = (UnionPositionOperator) _currentOperator;
195
196                     // default union
197
if (_upo.isDefault())
198                     {
199                         _result = getDynamicEvaluator().evaluateUnion(_currentAny);
200                     }
201                     else
202                     {
203                         _result = getDynamicEvaluator().evaluateUnion(_currentAny,
204                                 _upo.getPosition());
205                     }
206
207                     break;
208
209                 case AbstractTCLNode.IDENTIFIER:
210                     String JavaDoc _identifer = ((IdentValue) _currentOperator).getIdentifier();
211
212                     _result = getDynamicEvaluator().evaluateIdentifier(_currentAny, _identifer);
213
214                     break;
215
216                 case AbstractTCLNode.NUMBER:
217                     int _pos = ((NumberValue) _currentOperator).getNumber().intValue();
218
219                     _result = getDynamicEvaluator().evaluateIdentifier(_currentAny, _pos);
220
221                     break;
222
223                 case AbstractTCLNode.IMPLICIT:
224                     ImplicitOperator _op = ((ImplicitOperatorNode) _currentOperator).getOperator();
225
226                     if (logger_.isDebugEnabled())
227                     {
228                         logger_.debug(_op + " is an implict Operator");
229                     }
230
231                     _result = _op.evaluateImplicit(getDynamicEvaluator(), _currentAny);
232
233                     _ret = EvaluationResult.fromAny(_result);
234
235                     _ret.addAny(_currentAny);
236
237                     if (logger_.isDebugEnabled())
238                     {
239                         logger_.debug("result=" + _result);
240                     }
241
242                     return _ret;
243
244                 case AbstractTCLNode.ARRAY:
245                     int _arrayIndex = ((ArrayOperator) _currentOperator).getArrayIndex();
246
247                     _result = getDynamicEvaluator().evaluateArrayIndex(_currentAny, _arrayIndex);
248
249                     break;
250
251                 case AbstractTCLNode.ASSOC:
252                     String JavaDoc _assocName = ((AssocOperator) _currentOperator).getAssocName();
253
254                     _result = getDynamicEvaluator().evaluateNamedValueList(_currentAny, _assocName);
255
256                     break;
257
258                 default:
259                     throw new RuntimeException JavaDoc("unexpected operator: "
260                             + AbstractTCLNode.getNameForType(_currentOperator.getType()));
261                 }
262             }
263             else
264             {
265                 logger_.debug("Any Cache HIT");
266             }
267
268             if (_result != null)
269             {
270                 storeAny(_currentPath.toString(), _result);
271                 _currentAny = _result;
272             }
273             _currentOperator = (AbstractTCLNode) _currentOperator.getNextSibling();
274         }
275
276         // Create the EvaluationResult
277
_ret = EvaluationResult.fromAny(_result);
278
279         if (logger_.isDebugEnabled())
280         {
281             logger_.debug("extracted: " + _ret);
282         }
283
284         return _ret;
285     }
286 }
Popular Tags