1 package org.jacorb.notification.filter; 2 3 23 24 import java.util.Map ; 25 import java.util.WeakHashMap ; 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 ; 42 43 48 49 public class EvaluationContext extends AbstractPoolable 50 { 51 private final ETCLEvaluator dynamicEvaluator_; 52 53 private Message message_; 54 55 private final Map resultCache_; 56 57 private final Map anyCache_; 58 59 private final Logger logger_ = LogUtil.getLogger(getClass().getName()); 60 61 63 public EvaluationContext(ETCLEvaluator evaluator) 64 { 65 dynamicEvaluator_ = evaluator; 66 67 resultCache_ = new WeakHashMap (); 68 anyCache_ = new WeakHashMap (); 69 } 70 71 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 name, EvaluationResult value) 95 { 96 resultCache_.put(name, value); 97 } 98 99 public EvaluationResult lookupResult(String name) 100 { 101 return (EvaluationResult) resultCache_.get(name); 102 } 103 104 public void eraseResult(String name) 105 { 106 resultCache_.remove(name); 107 } 108 109 public void storeAny(String name, Any any) 110 { 111 anyCache_.put(name, any); 112 } 113 114 public Any lookupAny(String name) 115 { 116 return (Any ) anyCache_.get(name); 117 } 118 119 public void eraseAny(String name) 120 { 121 anyCache_.remove(name); 122 } 123 124 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 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 any, String 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 _result = null; 164 165 AbstractTCLNode _currentOperator = expr; 166 Any _currentAny = any; 167 168 StringBuffer _currentPath = new StringBuffer (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 _result = lookupAny(_currentPath.toString()); 183 184 if (_result == null) 185 { 187 switch (_currentOperator.getType()) { 188 case AbstractTCLNode.DOT: 189 break; 191 192 case AbstractTCLNode.UNION_POS: 193 logger_.debug("evaluate union by position"); 194 UnionPositionOperator _upo = (UnionPositionOperator) _currentOperator; 195 196 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 _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 _assocName = ((AssocOperator) _currentOperator).getAssocName(); 253 254 _result = getDynamicEvaluator().evaluateNamedValueList(_currentAny, _assocName); 255 256 break; 257 258 default: 259 throw new RuntimeException ("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 _ret = EvaluationResult.fromAny(_result); 278 279 if (logger_.isDebugEnabled()) 280 { 281 logger_.debug("extracted: " + _ret); 282 } 283 284 return _ret; 285 } 286 } | Popular Tags |