1 17 package org.apache.servicemix.components.drools; 18 19 import java.net.URL ; 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 ; 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 46 public class DroolsComponent extends OutBinding { 47 48 private RuleBase ruleBase; 49 private Resource ruleBaseResource; 50 private URL ruleBaseURL; 51 private ThreadLocal routed = new ThreadLocal (); 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 getRuleBaseURL() { 70 return ruleBaseURL; 71 } 72 73 public void setRuleBaseURL(URL ruleBaseURL) { 74 this.ruleBaseURL = ruleBaseURL; 75 } 76 77 public void forwardToService(MessageExchange exchange, NormalizedMessage in, QName 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 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 service, QName interfaceName, QName operation) throws MessagingException { 95 if (routed.get() != null) { 96 throw new IllegalStateException ("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 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 ("You must specify a ruleBase, ruleBaseResource or ruleBaseURL property"); 146 } 147 } 148 } 149 catch (Exception e) { 150 e.printStackTrace(); 151 throw new JBIException(e); 152 } 153 } 154 155 protected void process(MessageExchange exchange, NormalizedMessage in) throws Exception { 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 ("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 |