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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Dec 6 21:11:08 EST 2009


Author: hzbarcea
Date: 2009-12-06 21:11:07 -0500 (Sun, 06 Dec 2009)
New Revision: 30495

Added:
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/
   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/DroolsConverter.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/DroolsPayload.java
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/component/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/component/drools
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/
   labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java
Modified:
   labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
Log:
JBRULES-2347 Drools Pipeline integration with Camel
- Initial drop, not fully functional, for camel component



Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml	2009-12-06 13:37:19 UTC (rev 30494)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml	2009-12-07 02:11:07 UTC (rev 30495)
@@ -9,6 +9,11 @@
   
     <artifactId>drools-camel</artifactId>
     <name>Drools :: Pipeline :: Camel </name>
+
+    <properties>
+        <camel-version>2.1.0</camel-version>
+    </properties>
+
     <repositories>
         <repository>
             <id>camel</id>
@@ -54,12 +59,12 @@
          <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-core</artifactId>
-            <version>2.1-SNAPSHOT</version>
+            <version>${camel-version}</version>
         </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-spring</artifactId>
-            <version>2.1-SNAPSHOT</version>
+            <version>${camel-version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
@@ -70,9 +75,15 @@
             <groupId>xmlunit</groupId>
             <artifactId>xmlunit</artifactId>
             <version>1.2</version>
+        </dependency>
 
+         <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+            <version>${camel-version}</version>
+            <type>test-jar</type>
+            <scope>test</scope>
         </dependency>
-
     </dependencies>
 
     

Added: 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	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,58 @@
+/*
+ *  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 java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.impl.DefaultComponent;
+import org.drools.vsm.ServiceManager;
+
+public class DroolsComponent extends DefaultComponent {
+    private String smId = "";
+    private ServiceManager serviceManager;
+
+    public DroolsComponent() {
+    }
+
+    public DroolsComponent(CamelContext context) {
+        super(context);
+    }
+
+    public String getServiceManagerId() {
+        return smId;
+    }
+
+    public void setServiceManagerId (String smId) {
+        this.smId = smId == null ? "" : smId;
+    }
+
+    public ServiceManager getServiceManager() {
+        return serviceManager;
+    }
+
+    public void setServiceManager (ServiceManager sm) {
+        serviceManager = sm;
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        Endpoint endpoint = new DroolsEndpoint(uri, remaining, this);
+        setProperties(endpoint, parameters);
+        return endpoint;
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,33 @@
+/*
+ *  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.Converter;
+import org.apache.camel.Exchange;
+import org.w3c.dom.Document;
+
+ at Converter
+public final class DroolsConverter {
+
+    private DroolsConverter() {
+    	// Utility class
+    }
+
+    @Converter
+    public static DroolsPayload toVsmPayload(Document payload, Exchange exchange) {
+    	return null;
+    }
+}

Added: 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	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,107 @@
+/*
+ *  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 java.net.URISyntaxException;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.drools.runtime.CommandExecutor;
+import org.drools.vsm.ServiceManager;
+
+public class DroolsEndpoint extends DefaultEndpoint {
+    private String id;
+    private String method;
+    private CommandExecutor executor;
+
+    protected DroolsEndpoint(String endpointUri, String remaining, DroolsComponent component) throws URISyntaxException {
+        super(endpointUri, component);
+        configure(component, remaining);
+    }
+
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new RuntimeCamelException("Drools consumers not supported.");
+    }
+
+    public Producer createProducer() throws Exception {
+        return new DroolsProducer(this);
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public String getMethod() {
+        return method;
+    }
+
+    public void setMethod(String method) {
+        this.method = method;
+    }
+
+    public CommandExecutor getExecutor() {
+        return executor;
+    }
+
+    protected void configure(DroolsComponent component, String uri) {
+        int pos = uri.indexOf('/');
+        String smId = (pos < 0) ? uri : uri.substring(0, pos);
+        id = (pos < 0) ? "" : uri.substring(pos + 1);
+        
+        if (smId.length() > 0) {
+            // initialize the component if needed
+            ServiceManager sm = component.getServiceManager();
+            if (sm == null) {
+                // let's look it up
+                sm = component.getCamelContext().getRegistry().lookup(smId, ServiceManager.class);
+                if (sm == null) {
+                    throw new RuntimeCamelException("Could not find ServiceManager with id=\""
+                        + smId + "\" in CamelContext. Check configuration.");
+                }
+                // use this ServiceManager
+                component.setServiceManagerId(smId);
+                component.setServiceManager(sm);
+            } else if (!smId.equals(component.getServiceManagerId())) {
+                // make sure we deal with the same ServiceManager.
+                // having multiple ServiceManagers instances in the same process is not supported
+                throw new RuntimeCamelException("ServiceManager already initialized from id=\""
+                    + component.getServiceManagerId() + "\" yet current endpoint requries id=\"" + id + "\"");
+            }
+            
+            // if id is empty this endpoint is not attached to a CommandExecutor and will have to look it up at runtime.
+            if (id.length() > 0) {
+                // lookup command executor on 
+                executor = sm.lookup(id);
+                if (executor == null) {
+                    throw new RuntimeCamelException("Failed to instantiate DroolsEndpoint. " 
+                        + "Lookup of CommandExecutor with id=\"" + uri + "\" failed. Check configuration.");
+                }
+            }
+        } else {
+            // this is a hanging entity, not attached to an SM
+            executor = component.getCamelContext().getRegistry().lookup(id, CommandExecutor.class);
+            
+            // TODO: test this scenario...
+        }
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,28 @@
+/*
+ *  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;
+
+public class DroolsPayload {
+    private Object value;
+
+    public Object getValue() {
+    	return value;
+    }
+
+    public void setValue(Object value) {
+    	this.value = value;
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -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.CamelException;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.impl.DefaultProducer;
+import org.w3c.dom.Document;
+
+public class DroolsProducer extends DefaultProducer {
+
+    public DroolsProducer(Endpoint endpoint) {
+        super(endpoint);
+    }
+
+    public void process(Exchange exchange) throws Exception {
+	    
+        TypeConverter converter = exchange.getContext().getTypeConverter();
+        Document body = exchange.getIn().getBody(Document.class);
+        DroolsPayload payload = converter.convertTo(DroolsPayload.class, exchange, body);
+        if (payload == null) {
+            // throw new CamelException("foo");
+        }
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/TypeConverter	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/TypeConverter	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+org.drools.camel.component
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/component/drools
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/component/drools	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/component/drools	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+class=org.drools.camel.component.DroolsComponent
+

Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java	2009-12-07 02:11:07 UTC (rev 30495)
@@ -0,0 +1,100 @@
+/*
+ *  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 java.util.Collection;
+
+import javax.naming.Context;
+
+import org.apache.camel.CamelException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.drools.KnowledgeBase;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.ResourceType;
+import org.drools.definition.KnowledgePackage;
+import org.drools.io.ResourceFactory;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.vsm.ServiceManager;
+import org.drools.vsm.local.ServiceManagerLocalClient;
+import org.w3c.dom.Document;
+
+public class CamelEndpointTest extends ContextTestSupport {
+    private ServiceManager sm;
+
+    public void testBasic() throws Exception {
+        String inXml = "";
+        inXml += "<batch-execution>";
+        inXml += "  <insert out-identifier='salaboy'>";
+        inXml += "    <org.drools.pipeline.camel.Person>";
+        inXml += "      <name>salaboy</name>";
+        inXml += "    </org.drools.pipeline.camel.Person>";
+        inXml += "  </insert>";
+        inXml += "  <fire-all-rules />";
+        inXml += "</batch-execution>";
+
+        Object response = template.requestBody("direct:in", inXml);
+        // assertEquals("Hello World", response);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:in").to("drools:sm/ksession1?method=execute");
+            }
+        };
+    }
+    
+    @Override
+    protected Context createJndiContext() throws Exception {
+        Context context = super.createJndiContext();
+
+        String rule = "";
+        rule += "package org.drools.pipeline.camel;\n" +
+                "import org.drools.pipeline.camel.Person;\n" +
+                "rule 'Check for Person'\n" +
+                " when\n" +
+                "   $p: Person()\n" +
+                " then\n" +
+                "   System.out.println(\"Person Name: \" + $p.getName());\n" +
+                "end\n";
+
+        sm = new ServiceManagerLocalClient();
+        StatefulKnowledgeSession ksession = getVmsSessionStateful(sm, rule);
+        sm.register("ksession1", ksession);
+
+        context.bind("sm", sm);
+        return context;
+    }
+    
+    private StatefulKnowledgeSession getVmsSessionStateful(ServiceManager sm, String rule) throws Exception {
+        KnowledgeBuilder kbuilder = sm.getKnowledgeBuilderFactory().newKnowledgeBuilder();
+        kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
+
+        if (kbuilder.hasErrors()) {
+            throw new CamelException(kbuilder.getErrors().toString());
+        }
+
+        Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
+        KnowledgeBase kbase = sm.getKnowledgeBaseFactory().newKnowledgeBase();
+        kbase.addKnowledgePackages(pkgs);
+        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+
+        return session;
+    }
+
+}



More information about the jboss-svn-commits mailing list