[jboss-svn-commits] JBL Code SVN: r30691 - in labs/jbossrules/trunk/drools-pipeline/drools-camel: src/main/java/org/drools/camel/component and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Dec 15 19:04:47 EST 2009


Author: hzbarcea
Date: 2009-12-15 19:04:46 -0500 (Tue, 15 Dec 2009)
New Revision: 30691

Added:
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyProducer.java
Modified:
   labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyEndpoint.java
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java
Log:
JBRULES-2347 Drools Pipeline integration with Camel
* Added embedded camel route



Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml	2009-12-15 21:25:43 UTC (rev 30690)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml	2009-12-16 00:04:46 UTC (rev 30691)
@@ -32,28 +32,25 @@
 
         <dependency>
             <groupId>org.drools</groupId>
+            <artifactId>drools-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.drools</groupId>
             <artifactId>drools-core</artifactId>
-            <version>${project.version}</version>
         </dependency>
-
         <dependency>
             <groupId>org.drools</groupId>
             <artifactId>drools-compiler</artifactId>
-            <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
-
         <dependency>
             <groupId>org.drools</groupId>
             <artifactId>drools-vsm</artifactId>
-            <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
-
         <dependency>
             <groupId>org.drools</groupId>
             <artifactId>drools-transformer-xstream</artifactId>
-            <version>5.1.0.SNAPSHOT</version>
         </dependency>
 
          <dependency>

Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java	2009-12-15 21:25:43 UTC (rev 30690)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java	2009-12-16 00:04:46 UTC (rev 30691)
@@ -16,10 +16,13 @@
 package org.drools.camel.component;
 
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.camel.util.UuidGenerator;
 import org.drools.vsm.ServiceManager;
 
 public class DroolsComponent extends DefaultComponent {
@@ -28,6 +31,9 @@
     public static final String DROOLS_OUT_IDENTIFIER = "DroolsOutIdentifier";
     public static final String DROOLS_HANDLE = "DroolsHandle";
     
+    private static final String UUID_PREFIX = "drools-";
+    private static final AtomicInteger counter = new AtomicInteger();
+    
     private CamelContext embeddedContext;
     private ServiceManager serviceManager;
     private String smId = "";
@@ -40,6 +46,9 @@
     }
 
     public CamelContext getEmbeddedContext() {
+        if (embeddedContext == null) {
+            createEmbeddedContext();
+        }
         return embeddedContext;
     }
 
@@ -59,7 +68,7 @@
         return serviceManager;
     }
 
-    public void setServiceManager (ServiceManager sm) {
+    public void setServiceManager(ServiceManager sm) {
         serviceManager = sm;
     }
 
@@ -77,9 +86,48 @@
      */
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        Endpoint endpoint = parameters.containsKey("pipeline") ? 
-            new DroolsEndpoint(uri, remaining, this) : new DroolsProxyEndpoint(uri, remaining, this);
+        Endpoint endpoint;
+        boolean isProxy = !parameters.containsKey("pipeline");
+        if (isProxy) {
+            endpoint = new DroolsProxyEndpoint(uri, remaining, this);
+        } else {
+            endpoint = new DroolsEndpoint(uri, remaining, this);
+        }
         setProperties(endpoint, parameters);
         return endpoint;
     }
+
+    protected void createEmbeddedContext() {
+        // TODO: fix this temporary solution. Should be able to create a context from spring configuration as well
+        if (embeddedContext == null) {
+            CamelContext context;
+            try {
+                context = new DefaultCamelContext(getCamelContext().getRegistry());
+                context.disableJMX();
+                context.start();
+            } catch (Exception e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                return;
+            }
+            embeddedContext = context;
+        }
+    }
+    
+    public static final String getSessionManagerId(String uri) {
+        int pos = uri.indexOf('/');
+        return (pos < 0) ? uri : uri.substring(0, pos);
+
+    }
+
+    public static final String getKsessionId(String uri) {
+        int pos = uri.indexOf('/');
+        return (pos < 0) ? "" : uri.substring(pos + 1);
+    }
+
+    public static final String generateUuid() {
+        // Using the Camel uuid generator would be an option, but those are pretty long ids
+        // UuidGenerator.get().generateUuid()
+        return UUID_PREFIX + counter.incrementAndGet();
+    }
 }

Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java	2009-12-15 21:25:43 UTC (rev 30690)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java	2009-12-16 00:04:46 UTC (rev 30691)
@@ -74,9 +74,8 @@
     }
 
     protected void configure(DroolsComponent component, String uri) {
-        int pos = uri.indexOf('/');
-        String smId = (pos < 0) ? uri : uri.substring(0, pos);
-        ksession = (pos < 0) ? "" : uri.substring(pos + 1);
+        String smId = DroolsComponent.getSessionManagerId(uri);
+        ksession = DroolsComponent.getKsessionId(uri);
 
         if (smId.length() > 0) {
             // initialize the component if needed

Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyEndpoint.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyEndpoint.java	2009-12-15 21:25:43 UTC (rev 30690)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyEndpoint.java	2009-12-16 00:04:46 UTC (rev 30691)
@@ -15,21 +15,24 @@
 
 package org.drools.camel.component;
 
-import java.net.URISyntaxException;
-
+import org.apache.camel.CamelContext;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultEndpoint;
-import org.drools.vsm.ServiceManager;
 
 public class DroolsProxyEndpoint extends DefaultEndpoint {
 
     private String id;
-    private ServiceManager serviceManager;
+    private String uri;
+    private String dataType;
+    private String marshall;
+    private String unmarshall;
+    private RouteBuilder builder;
 
-    public DroolsProxyEndpoint(String endpointUri, String remaining, DroolsComponent component) throws URISyntaxException {
+    public DroolsProxyEndpoint(String endpointUri, String remaining, DroolsComponent component) throws Exception {
         super(endpointUri, component);
         configure(component, remaining);
     }
@@ -39,21 +42,63 @@
     }
 
     public Producer createProducer() throws Exception {
-        return new DroolsProducer(this, serviceManager);
+        // let's setup a route first
+        // we'll come up with a better way later
+        if (builder == null) {
+            builder = new RouteBuilder() {
+                public void configure() throws Exception {
+                    from("direct:" + id).to("drools:" + uri + "?pipeline=" + id);
+                }
+            };
+            
+            getEmbeddedContext().addRoutes(builder);
+        }
+        return new DroolsProxyProducer(this);
     }
 
     public boolean isSingleton() {
         return true;
     }
 
+    protected void configure(DroolsComponent component, String uri) throws Exception {
+        this.uri = uri;
+        // create unique id for embedded route
+        id = DroolsComponent.generateUuid();
+    }
+
     public String getId() {
         return id;
     }
 
-    public ServiceManager getServiceManager() {
-        return serviceManager;
+    public void setId(String id) {
+        this.id = id;
     }
 
-    protected void configure(DroolsComponent component, String uri) {
+    public String getDataType() {
+        return dataType;
     }
+
+    public void setDataType(String dataType) {
+        this.dataType = dataType;
+    }
+
+    public String getMarshall() {
+        return marshall;
+    }
+
+    public void setMarshall(String marshall) {
+        this.marshall = marshall;
+    }
+
+    public String getUnmarshall() {
+        return unmarshall;
+    }
+
+    public void setUnmarshall(String unmarshall) {
+        this.unmarshall = unmarshall;
+    }
+
+    public CamelContext getEmbeddedContext() {
+        return ((DroolsComponent)getComponent()).getEmbeddedContext();
+    }
 }

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyProducer.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyProducer.java	2009-12-16 00:04:46 UTC (rev 30691)
@@ -0,0 +1,40 @@
+/*
+ *  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.
+ *  under the License.
+ */
+
+package org.drools.camel.component;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ExchangeHelper;
+
+public class DroolsProxyProducer extends DefaultProducer {
+    private Endpoint droolsEndpoint;
+    private ProducerTemplate template;
+
+    public DroolsProxyProducer(Endpoint endpoint) {
+        super(endpoint);
+        DroolsProxyEndpoint de = (DroolsProxyEndpoint)endpoint;
+        droolsEndpoint = de.getEmbeddedContext().getEndpoint("direct:" + de.getId());
+        template = endpoint.getCamelContext().createProducerTemplate();
+        
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        Exchange result = template.send(droolsEndpoint, exchange.copy());
+        exchange.getOut().copyFrom(ExchangeHelper.getResultMessage(result));
+    }
+}

Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java	2009-12-15 21:25:43 UTC (rev 30690)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java	2009-12-16 00:04:46 UTC (rev 30691)
@@ -16,6 +16,7 @@
 package org.drools.camel.component;
 
 import org.apache.camel.builder.RouteBuilder;
+import org.drools.command.CommandFactory;
 import org.drools.command.runtime.rule.InsertObjectCommand;
 import org.drools.pipeline.camel.Person;
 import org.drools.runtime.ExecutionResults;
@@ -26,6 +27,13 @@
     private String handle;
 
     public void testSessionInsert() throws Exception {
+        Person person = new Person();
+        person.setName("Mauricio");
+
+        InsertObjectCommand cmd = (InsertObjectCommand) CommandFactory.newInsert(person,"salaboy");
+        ExecutionResults response = (ExecutionResults) template.requestBody("direct:test-no-marshal", cmd);
+        assertTrue("Expected valid ExecutionResults object", response != null);
+        assertTrue("ExecutionResults missing expected fact", response.getFactHandle("salaboy") != null);
     }
 
     @Override



More information about the jboss-svn-commits mailing list