1 18 package org.apache.activemq.filter; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 import javax.jms.JMSException ; 26 27 74 public class MultiExpressionEvaluator { 75 76 HashMap rootExpressions = new HashMap (); 77 HashMap cachedExpressions = new HashMap (); 78 79 int view = 0; 80 81 86 public class CacheExpression extends UnaryExpression { 87 short refCount = 0; 88 int cview = view - 1; 89 Object cachedValue; 90 int cachedHashCode; 91 92 public CacheExpression(Expression realExpression) { 93 super(realExpression); 94 cachedHashCode = realExpression.hashCode(); 95 } 96 97 100 public Object evaluate(MessageEvaluationContext message) throws JMSException { 101 if (view == cview) { 102 return cachedValue; 103 } 104 cachedValue = right.evaluate(message); 105 cview = view; 106 return cachedValue; 107 } 108 109 public int hashCode() { 110 return cachedHashCode; 111 } 112 113 public boolean equals(Object o) { 114 if( o == null ) 115 return false; 116 return ((CacheExpression) o).right.equals(right); 117 } 118 119 public String getExpressionSymbol() { 120 return null; 121 } 122 123 public String toString() { 124 return right.toString(); 125 } 126 127 } 128 129 133 static class ExpressionListenerSet { 134 Expression expression; 135 ArrayList listeners = new ArrayList (); 136 } 137 138 142 static interface ExpressionListener { 143 public void evaluateResultEvent(Expression selector, MessageEvaluationContext message, Object result); 144 } 145 146 151 public void addExpressionListner(Expression selector, ExpressionListener c) { 152 ExpressionListenerSet data = (ExpressionListenerSet) rootExpressions.get(selector.toString()); 153 if (data == null) { 154 data = new ExpressionListenerSet(); 155 data.expression = addToCache(selector); 156 rootExpressions.put(selector.toString(), data); 157 } 158 data.listeners.add(c); 159 } 160 161 165 public boolean removeEventListner(String selector, ExpressionListener c) { 166 String expKey = selector; 167 ExpressionListenerSet d = (ExpressionListenerSet) rootExpressions.get(expKey); 168 if (d == null) { 170 return false; 171 } 172 if (!d.listeners.remove(c)) { 174 return false; 175 } 176 177 if (d.listeners.size() == 0) { 179 removeFromCache((CacheExpression) d.expression); 181 rootExpressions.remove(expKey); 182 } 183 return true; 184 } 185 186 196 private CacheExpression addToCache(Expression expr) { 197 198 CacheExpression n = (CacheExpression) cachedExpressions.get(expr); 199 if (n == null) { 200 n = new CacheExpression(expr); 201 cachedExpressions.put(expr, n); 202 if (expr instanceof UnaryExpression) { 203 204 UnaryExpression un = (UnaryExpression) expr; 206 un.setRight(addToCache(un.getRight())); 207 208 } 209 else if (expr instanceof BinaryExpression) { 210 211 BinaryExpression bn = (BinaryExpression) expr; 213 bn.setRight(addToCache(bn.getRight())); 214 bn.setLeft(addToCache(bn.getLeft())); 215 216 } 217 } 218 n.refCount++; 219 return n; 220 } 221 222 230 private void removeFromCache(CacheExpression cn) { 231 cn.refCount--; 232 Expression realExpr = cn.getRight(); 233 if (cn.refCount == 0) { 234 cachedExpressions.remove(realExpr); 235 } 236 if (realExpr instanceof UnaryExpression) { 237 UnaryExpression un = (UnaryExpression) realExpr; 238 removeFromCache((CacheExpression) un.getRight()); 239 } 240 if (realExpr instanceof BinaryExpression) { 241 BinaryExpression bn = (BinaryExpression) realExpr; 242 removeFromCache((CacheExpression) bn.getRight()); 243 } 244 } 245 246 253 public void evaluate(MessageEvaluationContext message) { 254 Collection expressionListeners = rootExpressions.values(); 255 for (Iterator iter = expressionListeners.iterator(); iter.hasNext();) { 256 ExpressionListenerSet els = (ExpressionListenerSet) iter.next(); 257 try { 258 Object result = els.expression.evaluate(message); 259 for (Iterator iterator = els.listeners.iterator(); iterator.hasNext();) { 260 ExpressionListener l = (ExpressionListener) iterator.next(); 261 l.evaluateResultEvent(els.expression, message, result); 262 } 263 } 264 catch (Throwable e) { 265 e.printStackTrace(); 266 } 267 } 268 } 269 } 270 | Popular Tags |