01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
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: /**
36: * Represents a Jaxen based condition using the W3C DOM.
37: *
38: * @version $Revision: 426415 $
39: */
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
60: .getVariableContext();
61: variableContext.setVariables(null);
62:
63: for (Iterator iter = list.iterator(); iter.hasNext();) {
64: Declaration declaration = (Declaration) iter.next();
65: String name = declaration.getIdentifier();
66: Object value = tuple.get(declaration);
67: variableContext.setVariableValue(name, value);
68: }
69: NormalizedMessage message = (NormalizedMessage) findFirst(
70: memory, NormalizedMessage.class);
71: MessageExchange exchange = (MessageExchange) findFirst(memory,
72: MessageExchange.class);
73:
74: try {
75: return expression.matches(exchange, message);
76: } catch (MessagingException e) {
77: throw new RuntimeException(e);
78: }
79: }
80:
81: protected Object findFirst(WorkingMemory memory, Class type) {
82: List objects = memory.getObjects(type);
83: Object answer = null;
84: if (objects.size() > 0) {
85: answer = objects.get(0);
86: }
87: return answer;
88: }
89: }
|