servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r406054 - in /incubator/servicemix/trunk/servicemix-components/src: main/java/org/apache/servicemix/components/drools/ main/java/org/apache/servicemix/components/drools/dsl/ main/resources/META-INF/ test/java/org/apache/servicemix/component...
Date Sat, 13 May 2006 09:22:08 GMT
Author: gnodet
Date: Sat May 13 02:22:06 2006
New Revision: 406054

URL: http://svn.apache.org/viewcvs?rev=406054&view=rev
Log:
SM-316: Drools component should support InOut MEP

Added:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/dsl/RouteConsequenceFactory.java
    incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/drools/DroolsRouteTest.java
    incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-example-route.xml
    incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-rules-route.xml
Modified:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/DroolsComponent.java
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/JbiHelper.java
    incubator/servicemix/trunk/servicemix-components/src/main/resources/META-INF/drools-servicemix.conf

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/DroolsComponent.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/DroolsComponent.java?rev=406054&r1=406053&r2=406054&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/DroolsComponent.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/DroolsComponent.java
Sat May 13 02:22:06 2006
@@ -19,7 +19,10 @@
 
 import javax.jbi.JBIException;
 import javax.jbi.messaging.DeliveryChannel;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.Fault;
 import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.InOut;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessageExchangeFactory;
 import javax.jbi.messaging.MessagingException;
@@ -44,6 +47,7 @@
     private RuleBase ruleBase;
     private Resource ruleBaseResource;
     private URL ruleBaseURL;
+    private ThreadLocal routed = new ThreadLocal();
 
     public RuleBase getRuleBase() {
         return ruleBase;
@@ -86,6 +90,43 @@
         forwardToExchange(exchange, outExchange, in);
     }
 
+    public void route(MessageExchange exchange, NormalizedMessage in, QName service, QName
interfaceName, QName operation) throws MessagingException {
+        if (routed.get() != null) {
+            throw new IllegalStateException("Drools component has already routed this exchange");
+        }
+        routed.set(Boolean.TRUE);
+        DeliveryChannel channel = getDeliveryChannel();
+        MessageExchangeFactory factory = channel.createExchangeFactory();
+        MessageExchange me = factory.createExchange(exchange.getPattern());
+        me.setInterfaceName(interfaceName);
+        me.setService(service);
+        me.setOperation(operation);
+        NormalizedMessage nm = me.createMessage(); 
+        me.setMessage(nm, "in");
+        getMessageTransformer().transform(exchange, in, nm);
+        channel.sendSync(me); 
+        if (me.getStatus() == ExchangeStatus.ERROR) {
+            fail(exchange, me.getError());
+        } else if (me.getStatus() == ExchangeStatus.DONE) {
+            done(exchange);
+        } else {
+            NormalizedMessage out = me.getMessage("out");
+            if (out != null) {
+                nm = exchange.createMessage();
+                exchange.setMessage(nm, "out");
+                getMessageTransformer().transform(exchange, out, nm);
+            } else {
+                Fault f = me.getFault();
+                Fault of = exchange.createFault();
+                exchange.setFault(of);
+                getMessageTransformer().transform(exchange, f, of);
+            }
+            channel.send(exchange);
+            done(me);
+        }
+        
+    }
+
 
     // Implementation methods
     //-------------------------------------------------------------------------
@@ -113,14 +154,21 @@
     protected void process(MessageExchange exchange, NormalizedMessage in) throws Exception
{
         WorkingMemory memory = ruleBase.newWorkingMemory();
         populateWorkingMemory(memory, exchange, in);
+        routed.set(null);
         memory.fireAllRules();
-        done(exchange);
+        if (routed.get() == null) {
+            if (exchange instanceof InOut) {
+                fail(exchange, new Exception("Drools component has not routed the exchange"));
+            } else {
+                done(exchange);
+            }
+        }
     }
 
     protected void populateWorkingMemory(WorkingMemory memory, MessageExchange exchange,
NormalizedMessage in) throws MessagingException, FactException {
         memory.setApplicationData("context", getContext());
         memory.setApplicationData("deliveryChannel", getDeliveryChannel());
-        memory.setApplicationData("jbi", new JbiHelper(this, exchange, in));
+        memory.setApplicationData("jbi", new JbiHelper(this, exchange, in, memory));
         memory.assertObject(in);
         memory.assertObject(exchange);
     }

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/JbiHelper.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/JbiHelper.java?rev=406054&r1=406053&r2=406054&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/JbiHelper.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/JbiHelper.java
Sat May 13 02:22:06 2006
@@ -21,6 +21,8 @@
 import javax.jbi.messaging.NormalizedMessage;
 import javax.xml.namespace.QName;
 
+import org.drools.WorkingMemory;
+
 /**
  * A helper class for use inside a rule to forward a message to an endpoint
  *
@@ -30,11 +32,16 @@
     private DroolsComponent component;
     private MessageExchange exchange;
     private NormalizedMessage in;
+    private WorkingMemory memory;
 
-    public JbiHelper(DroolsComponent component, MessageExchange exchange, NormalizedMessage
in) {
+    public JbiHelper(DroolsComponent component, 
+                     MessageExchange exchange, 
+                     NormalizedMessage in,
+                     WorkingMemory memory) {
         this.component = component;
         this.exchange = exchange;
         this.in = in;
+        this.memory = memory;
     }
 
     /**
@@ -52,9 +59,12 @@
         component.forwardToService(exchange, in, name);
     }
 
-
     public void invoke(QName service, QName operation, QName interfaceName) throws MessagingException
{
         component.invoke(exchange, in, service, interfaceName, operation);
+    }
+
+    public void route(QName service, QName operation, QName interfaceName) throws MessagingException
{
+        component.route(exchange, in, service, interfaceName, operation);
     }
 
     public DeliveryChannel getDeliveryChannel() throws MessagingException {

Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/dsl/RouteConsequenceFactory.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/dsl/RouteConsequenceFactory.java?rev=406054&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/dsl/RouteConsequenceFactory.java
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/drools/dsl/RouteConsequenceFactory.java
Sat May 13 02:22:06 2006
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.components.drools.dsl;
+
+import org.apache.servicemix.components.drools.JbiHelper;
+import org.drools.rule.Rule;
+import org.drools.smf.Configuration;
+import org.drools.smf.ConsequenceFactory;
+import org.drools.smf.FactoryException;
+import org.drools.spi.Consequence;
+import org.drools.spi.RuleBaseContext;
+import org.drools.spi.Tuple;
+
+import javax.jbi.messaging.MessagingException;
+import javax.xml.namespace.QName;
+
+/**
+ * @version $Revision: 359186 $
+ */
+public class RouteConsequenceFactory implements ConsequenceFactory {
+    public Consequence newConsequence(Rule rule, RuleBaseContext ruleBaseContext, Configuration
configuration) throws FactoryException {
+        final QName operation = toQName(configuration, configuration.getAttribute("operation"));
+        final QName service = toQName(configuration, configuration.getAttribute("service"));
+        final QName interfaceName = toQName(configuration, configuration.getAttribute("interface"));
+        return new JbiConsequence() {
+            protected void invokeJbiOperation(JbiHelper helper, Tuple tuple) throws MessagingException
{
+                helper.route(service, operation, interfaceName);
+            }
+        };
+    }
+
+    /**
+     * Converts the String into a QName
+     */
+    protected QName toQName(Configuration configuration, String text) {
+        if (text == null) {
+            return null;
+        }
+        String[] names = configuration.getAttributeNames();
+        String localPart = text;
+        String prefix = null;
+        int idx = text.indexOf(':');
+        if (idx >= 0) {
+            prefix = "xmlns:" + text.substring(0, idx);
+            localPart = text.substring(idx + 1);
+        }
+        String uri = "";
+        for (int i = 0; i < names.length; i++) {
+            String name = names[i];
+            if (prefix == null) {
+                if ("xmlns".equals(name)) {
+                    uri = configuration.getAttribute(name);
+                    break;
+                }
+            }
+            else {
+                if (name.equals(prefix)) {
+                    uri = configuration.getAttribute(name);
+                    break;
+                }
+            }
+        }
+        System.out.println("Creating QName with uri: " + uri + " name: " + localPart);
+        return new QName(uri, localPart);
+    }
+}

Modified: incubator/servicemix/trunk/servicemix-components/src/main/resources/META-INF/drools-servicemix.conf
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/main/resources/META-INF/drools-servicemix.conf?rev=406054&r1=406053&r2=406054&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/resources/META-INF/drools-servicemix.conf
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/resources/META-INF/drools-servicemix.conf
Sat May 13 02:22:06 2006
@@ -5,3 +5,4 @@
 Condition(condition)     : org.apache.servicemix.components.drools.dsl.JaxenConditionFactory
 Consequence(invoke)      : org.apache.servicemix.components.drools.dsl.InvokeConsequenceFactory
 Consequence(forward)     : org.apache.servicemix.components.drools.dsl.ForwardConsequenceFactory
+Consequence(route)       : org.apache.servicemix.components.drools.dsl.RouteConsequenceFactory

Added: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/drools/DroolsRouteTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/drools/DroolsRouteTest.java?rev=406054&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/drools/DroolsRouteTest.java
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/drools/DroolsRouteTest.java
Sat May 13 02:22:06 2006
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.components.drools;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.TestSupport;
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * @version $Revision: 376451 $
+ */
+public class DroolsRouteTest extends TestSupport {
+
+    public void testFiringRules() throws Exception {
+        QName service = new QName("http://servicemix.org/cheese/", "droolsRouter");
+
+        InOut exchange = client.createInOutExchange();
+        exchange.setService(service);
+        NormalizedMessage message = exchange.getInMessage();
+        message.setContent(new StringSource(createMessageXmlText(1)));
+        client.sendSync(exchange);
+        assertEquals(ExchangeStatus.ACTIVE, exchange.getStatus());
+        Node node = transformer.toDOMNode(exchange.getOutMessage().getContent());
+        Element e = null;
+        if (node instanceof Element) {
+            e = (Element) node;
+        } else if (node instanceof Document) {
+            e = ((Document) node).getDocumentElement();
+        } else {
+            fail("Node should be an Element or a Document");
+        }
+        assertEquals("hello", e.getLocalName());
+        client.done(exchange);
+
+        exchange = client.createInOutExchange();
+        exchange.setService(service);
+        message = exchange.getInMessage();
+        message.setContent(new StringSource(createMessageXmlText(2)));
+        client.sendSync(exchange);
+        assertEquals(ExchangeStatus.ACTIVE, exchange.getStatus());
+        node = transformer.toDOMNode(exchange.getOutMessage().getContent());
+        e = null;
+        if (node instanceof Element) {
+            e = (Element) node;
+        } else if (node instanceof Document) {
+            e = ((Document) node).getDocumentElement();
+        } else {
+            fail("Node should be an Element or a Document");
+        }
+        assertEquals("world", e.getLocalName());
+        client.done(exchange);
+    }
+
+    protected AbstractXmlApplicationContext createBeanFactory() {
+        return new ClassPathXmlApplicationContext("org/apache/servicemix/components/drools/jbi-example-route.xml");
+    }
+}

Added: incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-example-route.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-example-route.xml?rev=406054&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-example-route.xml
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-example-route.xml
Sat May 13 02:22:06 2006
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:sm="http://servicemix.apache.org/config/1.0" 
+	   xmlns:foo="http://servicemix.org/cheese/">
+
+  <!-- the JBI container -->
+  <sm:container id="jbi" embedded="true">
+    <sm:activationSpecs>
+
+      <!-- START SNIPPET: drools -->
+      <sm:activationSpec componentName="droolsRouter" 
+      					 service="foo:droolsRouter">
+      	<sm:component><bean class="org.apache.servicemix.components.drools.DroolsComponent">
+          <property name="ruleBaseResource" value="classpath:org/apache/servicemix/components/drools/jbi-rules-route.xml"
/>
+        </bean></sm:component>
+      </sm:activationSpec>
+      <!-- END SNIPPET: drools -->
+
+      <sm:activationSpec componentName="target1" 
+      					 service="foo:target1">
+      	<sm:component>
+      	  <bean class="org.apache.servicemix.components.util.MockServiceComponent">
+      	     <property name="responseXml" value="&lt;hello/&gt;" />
+      	  </bean>
+      	</sm:component>
+      </sm:activationSpec>
+
+      <sm:activationSpec componentName="target2" 
+      					 service="foo:target2">
+      	<sm:component>
+      	  <bean class="org.apache.servicemix.components.util.MockServiceComponent">
+      	     <property name="responseXml" value="&lt;world/&gt;" />
+      	  </bean>
+      	</sm:component>
+      </sm:activationSpec>
+    </sm:activationSpecs>
+  </sm:container>
+
+  <bean id="client" class="org.apache.servicemix.client.DefaultServiceMixClient">
+    <constructor-arg ref="jbi"/>
+  </bean>
+</beans>

Added: incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-rules-route.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-rules-route.xml?rev=406054&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-rules-route.xml
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/drools/jbi-rules-route.xml
Sat May 13 02:22:06 2006
@@ -0,0 +1,29 @@
+<!-- START SNIPPET: drools -->
+<rule-set name="cheese rules"
+  xmlns="http://drools.org/rules"
+  xmlns:jbi="http://drools.org/semantics/servicemix"
+  xmlns:foo="http://servicemix.org/cheese/">
+
+  <application-data identifier="jbi">org.apache.servicemix.components.drools.JbiHelper</application-data>
+  <application-data identifier="context">javax.jbi.component.ComponentContext</application-data>
+  <application-data identifier="deliveryChannel">javax.jbi.messaging.DeliveryChannel</application-data>
+
+  <rule name="Ignore 2 message">
+    <parameter identifier="exchange">
+      <class>javax.jbi.messaging.MessageExchange</class>
+    </parameter>
+
+    <jbi:condition>/*/@id != 2</jbi:condition>
+    <jbi:route service="foo:target1"/>
+  </rule>
+
+  <rule name="Default">
+    <parameter identifier="exchange">
+      <class>javax.jbi.messaging.MessageExchange</class>
+    </parameter>
+    <jbi:condition>/*/@id = 2</jbi:condition>
+    <jbi:route service="foo:target2"/>
+  </rule>
+
+</rule-set>
+<!-- END SNIPPET: drools -->



Mime
View raw message