KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > drools > dsl > JaxenCondition


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc list = (List JavaDoc) rule.getParameterDeclarations();
58         WorkingMemory memory = tuple.getWorkingMemory();
59         JaxenVariableContext variableContext = expression.getVariableContext();
60         variableContext.setVariables(null);
61
62         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
63             Declaration declaration = (Declaration) iter.next();
64             String JavaDoc name = declaration.getIdentifier();
65             Object JavaDoc 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 JavaDoc(e);
76         }
77     }
78
79     protected Object JavaDoc findFirst(WorkingMemory memory, Class JavaDoc type) {
80         List JavaDoc objects = memory.getObjects(type);
81         Object JavaDoc answer = null;
82         if (objects.size() > 0) {
83             answer = objects.get(0);
84         }
85         return answer;
86     }
87 }
88
Popular Tags