KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > drools > DroolsComponent


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;
18
19 import java.net.URL JavaDoc;
20
21 import javax.jbi.JBIException;
22 import javax.jbi.messaging.DeliveryChannel;
23 import javax.jbi.messaging.ExchangeStatus;
24 import javax.jbi.messaging.Fault;
25 import javax.jbi.messaging.InOnly;
26 import javax.jbi.messaging.InOut;
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.MessageExchangeFactory;
29 import javax.jbi.messaging.MessagingException;
30 import javax.jbi.messaging.NormalizedMessage;
31 import javax.xml.namespace.QName JavaDoc;
32
33 import org.apache.servicemix.components.util.OutBinding;
34 import org.drools.FactException;
35 import org.drools.RuleBase;
36 import org.drools.WorkingMemory;
37 import org.drools.io.RuleBaseLoader;
38 import org.springframework.core.io.Resource;
39
40 /**
41  * A component which implements a rules based routing using <a HREF="http://drools.org/">Drools</a> to decide
42  * where to route the message.
43  *
44  * @version $Revision: 426415 $
45  */

46 public class DroolsComponent extends OutBinding {
47
48     private RuleBase ruleBase;
49     private Resource ruleBaseResource;
50     private URL JavaDoc ruleBaseURL;
51     private ThreadLocal JavaDoc routed = new ThreadLocal JavaDoc();
52
53     public RuleBase getRuleBase() {
54         return ruleBase;
55     }
56
57     public void setRuleBase(RuleBase ruleBase) {
58         this.ruleBase = ruleBase;
59     }
60
61     public Resource getRuleBaseResource() {
62         return ruleBaseResource;
63     }
64
65     public void setRuleBaseResource(Resource ruleBaseResource) {
66         this.ruleBaseResource = ruleBaseResource;
67     }
68
69     public URL JavaDoc getRuleBaseURL() {
70         return ruleBaseURL;
71     }
72
73     public void setRuleBaseURL(URL JavaDoc ruleBaseURL) {
74         this.ruleBaseURL = ruleBaseURL;
75     }
76
77     // Helper methods for the rule base
78
//-------------------------------------------------------------------------
79
public void forwardToService(MessageExchange exchange, NormalizedMessage in, QName JavaDoc name) throws MessagingException {
80         DeliveryChannel channel = getDeliveryChannel();
81         MessageExchangeFactory factory = channel.createExchangeFactoryForService(name);
82         InOnly outExchange = factory.createInOnlyExchange();
83         forwardToExchange(exchange, outExchange, in);
84     }
85
86
87     public void forwardToInterface(QName JavaDoc name, MessageExchange exchange, NormalizedMessage in) throws MessagingException {
88         DeliveryChannel channel = getDeliveryChannel();
89         MessageExchangeFactory factory = channel.createExchangeFactory(name);
90         InOnly outExchange = factory.createInOnlyExchange();
91         forwardToExchange(exchange, outExchange, in);
92     }
93
94     public void route(MessageExchange exchange, NormalizedMessage in, QName JavaDoc service, QName JavaDoc interfaceName, QName JavaDoc operation) throws MessagingException {
95         if (routed.get() != null) {
96             throw new IllegalStateException JavaDoc("Drools component has already routed this exchange");
97         }
98         routed.set(Boolean.TRUE);
99         DeliveryChannel channel = getDeliveryChannel();
100         MessageExchangeFactory factory = channel.createExchangeFactory();
101         MessageExchange me = factory.createExchange(exchange.getPattern());
102         me.setInterfaceName(interfaceName);
103         me.setService(service);
104         me.setOperation(operation);
105         NormalizedMessage nm = me.createMessage();
106         me.setMessage(nm, "in");
107         getMessageTransformer().transform(exchange, in, nm);
108         channel.sendSync(me);
109         if (me.getStatus() == ExchangeStatus.ERROR) {
110             fail(exchange, me.getError());
111         } else if (me.getStatus() == ExchangeStatus.DONE) {
112             done(exchange);
113         } else {
114             NormalizedMessage out = me.getMessage("out");
115             if (out != null) {
116                 nm = exchange.createMessage();
117                 exchange.setMessage(nm, "out");
118                 getMessageTransformer().transform(exchange, out, nm);
119             } else {
120                 Fault f = me.getFault();
121                 Fault of = exchange.createFault();
122                 exchange.setFault(of);
123                 getMessageTransformer().transform(exchange, f, of);
124             }
125             channel.send(exchange);
126             done(me);
127         }
128         
129     }
130
131
132     // Implementation methods
133
//-------------------------------------------------------------------------
134
protected void init() throws JBIException {
135         super.init();
136         try {
137             if (ruleBase == null) {
138                 if (ruleBaseResource != null) {
139                     ruleBase = RuleBaseLoader.loadFromInputStream(ruleBaseResource.getInputStream());
140                 }
141                 else if (ruleBaseURL != null) {
142                     ruleBase = RuleBaseLoader.loadFromUrl(ruleBaseURL);
143                 }
144                 else {
145                     throw new IllegalArgumentException JavaDoc("You must specify a ruleBase, ruleBaseResource or ruleBaseURL property");
146                 }
147             }
148         }
149         catch (Exception JavaDoc e) {
150             e.printStackTrace();
151             throw new JBIException(e);
152         }
153     }
154
155     protected void process(MessageExchange exchange, NormalizedMessage in) throws Exception JavaDoc {
156         WorkingMemory memory = ruleBase.newWorkingMemory();
157         populateWorkingMemory(memory, exchange, in);
158         routed.set(null);
159         memory.fireAllRules();
160         if (routed.get() == null) {
161             if (exchange instanceof InOut) {
162                 fail(exchange, new Exception JavaDoc("Drools component has not routed the exchange"));
163             } else {
164                 done(exchange);
165             }
166         }
167     }
168
169     protected void populateWorkingMemory(WorkingMemory memory, MessageExchange exchange, NormalizedMessage in) throws MessagingException, FactException {
170         memory.setApplicationData("context", getContext());
171         memory.setApplicationData("deliveryChannel", getDeliveryChannel());
172         memory.setApplicationData("jbi", new JbiHelper(this, exchange, in, memory));
173         memory.assertObject(in);
174         memory.assertObject(exchange);
175     }
176 }
177
Popular Tags