1 17 package org.apache.servicemix.components.drools.dsl; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.jbi.messaging.MessageExchange; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 27 import org.apache.servicemix.expression.JaxenVariableContext; 28 import org.apache.servicemix.expression.JaxenXPathExpression; 29 import org.drools.WorkingMemory; 30 import org.drools.rule.Declaration; 31 import org.drools.rule.Rule; 32 import org.drools.spi.Condition; 33 import org.drools.spi.Tuple; 34 35 40 public class JaxenCondition implements Condition { 41 private Rule rule; 42 private JaxenXPathExpression expression; 43 44 public JaxenCondition(Rule rule, JaxenXPathExpression expression) { 45 this.rule = rule; 46 this.expression = expression; 47 } 48 49 public Declaration[] getRequiredTupleMembers() { 50 Collection list = rule.getParameterDeclarations(); 51 Declaration[] answer = new Declaration[list.size()]; 52 list.toArray(answer); 53 return answer; 54 } 55 56 public boolean isAllowed(Tuple tuple) { 57 List list = (List ) rule.getParameterDeclarations(); 58 WorkingMemory memory = tuple.getWorkingMemory(); 59 JaxenVariableContext variableContext = expression.getVariableContext(); 60 variableContext.setVariables(null); 61 62 for (Iterator iter = list.iterator(); iter.hasNext();) { 63 Declaration declaration = (Declaration) iter.next(); 64 String name = declaration.getIdentifier(); 65 Object value = tuple.get(declaration); 66 variableContext.setVariableValue(name, value); 67 } 68 NormalizedMessage message = (NormalizedMessage) findFirst(memory, NormalizedMessage.class); 69 MessageExchange exchange = (MessageExchange) findFirst(memory, MessageExchange.class); 70 71 try { 72 return expression.matches(exchange, message); 73 } 74 catch (MessagingException e) { 75 throw new RuntimeException (e); 76 } 77 } 78 79 protected Object findFirst(WorkingMemory memory, Class type) { 80 List objects = memory.getObjects(type); 81 Object answer = null; 82 if (objects.size() > 0) { 83 answer = objects.get(0); 84 } 85 return answer; 86 } 87 } 88 | Popular Tags |