[jboss-svn-commits] JBL Code SVN: r30795 - in labs/jbossrules/trunk/drools-pipeline/drools-camel/src: main/java/org/drools/pipeline/camel and 3 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 21 22:49:58 EST 2009
Author: hzbarcea
Date: 2009-12-21 22:49:57 -0500 (Mon, 21 Dec 2009)
New Revision: 30795
Added:
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/dataformat/
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/dataformat/drools-xstream
Modified:
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/DroolsProxyEndpoint.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsXStreamDataFormat.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/pipeline/camel/DroolsCamelContextInit.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithMarshallersTest.java
Log:
JBRULES-2347 Drools Pipeline integration with Camel
* build route dynamically based on the un/marshalling formats.
Modified: 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 2009-12-22 03:41:42 UTC (rev 30794)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java 2009-12-22 03:49:57 UTC (rev 30795)
@@ -51,6 +51,9 @@
}
Command cmd = exchange.getIn().getBody(Command.class);
+ if (cmd == null) {
+ throw new RuntimeCamelException("Body of in message not of the expected type 'org.drools.command.Command'");
+ }
ExecutionResults results = exec.execute(cmd);
exchange.getOut().setBody(results);
}
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-22 03:41:42 UTC (rev 30794)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProxyEndpoint.java 2009-12-22 03:49:57 UTC (rev 30795)
@@ -22,6 +22,8 @@
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.RouteDefinition;
import org.apache.camel.spi.DataFormat;
import org.drools.pipeline.camel.DroolsCamelContextInit;
import org.drools.vsm.ServiceManager;
@@ -30,7 +32,7 @@
private String id;
private String uri;
- private String dataType;
+ private String dataFormat;
private String marshall;
private String unmarshall;
private RouteBuilder builder;
@@ -47,15 +49,29 @@
public Producer createProducer() throws Exception {
// let's setup a route first
// we'll come up with a better way later
- final DataFormat xstream = new DroolsXStreamDataFormat();
+ // final DataFormat xstream = new DroolsXStreamDataFormat();
if (builder == null) {
+ String smId = DroolsComponent.getSessionManagerId(uri);
+ final ServiceManager sm = (ServiceManager)getCamelContext().getRegistry().lookup(smId);
+ if (sm == null) {
+ throw new RuntimeCamelException("Cannot find ServiceManager instance with id=\"" +
+ smId + "\" in the CamelContext registry.");
+ }
+
+ final String inFormat = (dataFormat == null) ? unmarshall : dataFormat;
+ final String outFormat = (dataFormat == null) ? marshall : dataFormat;
builder = new RouteBuilder() {
public void configure() throws Exception {
- from("direct:" + id).bean(new DroolsCamelContextInit((ServiceManager)getCamelContext().getRegistry().lookup("sm")))
- //.unmarshal(xstream)
- .to("drools-embedded:" + uri);
- //.marshal(xstream);
+ // build the route step by step
+ ProcessorDefinition<?> pipeline = from("direct:" + id).bean(new DroolsCamelContextInit(sm));
+ if (inFormat != null) {
+ pipeline = pipeline.unmarshal(inFormat);
+ }
+ pipeline = pipeline.to("drools-embedded:" + uri);
+ if (inFormat != null) {
+ pipeline = pipeline.marshal(outFormat);
+ }
}
};
@@ -87,12 +103,12 @@
this.id = id;
}
- public String getDataType() {
- return dataType;
+ public String getDataFormat() {
+ return dataFormat;
}
- public void setDataType(String dataType) {
- this.dataType = dataType;
+ public void setDataFormat(String dataFormat) {
+ this.dataFormat = dataFormat;
}
public String getMarshall() {
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsXStreamDataFormat.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsXStreamDataFormat.java 2009-12-22 03:41:42 UTC (rev 30794)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsXStreamDataFormat.java 2009-12-22 03:49:57 UTC (rev 30795)
@@ -17,16 +17,13 @@
package org.drools.camel.component;
-import com.thoughtworks.xstream.XStream;
-import com.thoughtworks.xstream.io.xml.DomReader;
-import java.io.ByteArrayOutputStream;
import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
+
import javax.xml.parsers.DocumentBuilderFactory;
+
import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;
import org.drools.impl.StatefulKnowledgeSessionImpl;
@@ -34,7 +31,6 @@
import org.drools.io.Resource;
import org.drools.reteoo.ReteooRuleBase;
import org.drools.runtime.CommandExecutor;
-
import org.drools.runtime.help.BatchExecutionHelper;
import org.drools.runtime.pipeline.PipelineContext;
import org.drools.runtime.pipeline.impl.ServiceManagerPipelineContextImpl;
@@ -44,17 +40,20 @@
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomReader;
+
/**
- *
+ *
* @author salaboy
*/
-public class DroolsXStreamDataFormat implements DataFormat{
+public class DroolsXStreamDataFormat implements DataFormat {
private XStreamResolverStrategy xstreamStrategy = null;
private final XStreamFromXmlVsmTransformer transformer;
private String charset;
- public DroolsXStreamDataFormat() {
- xstreamStrategy = new XStreamResolverStrategy() {
+ public DroolsXStreamDataFormat() {
+ xstreamStrategy = new XStreamResolverStrategy() {
public XStream lookup(String name) {
return BatchExecutionHelper.newXStreamMarshaller();
}
@@ -62,18 +61,18 @@
this.transformer = new XStreamFromXmlVsmTransformer(xstreamStrategy);
}
+ public void marshal(Exchange exchange, Object graph, OutputStream stream)
+ throws Exception {
-
- public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
-
PipelineContext context = (PipelineContext) exchange.getProperty("drools-context");
- XStream xstream = (XStream) context.getProperties().get( "xstream-instance" );
+ XStream xstream = (XStream) context.getProperties().get("xstream-instance");
xstream.setClassLoader(context.getClassLoader());
String result = null;
+
try {
result = xstream.toXML(exchange.getIn().getBody());
} catch (Exception e) {
- //handleException(this, object, e);
+ // handleException(this, object, e);
e.printStackTrace();
}
@@ -86,49 +85,51 @@
stream.write(bytes);
-
}
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
- // this.transformer.processPayload(exchange.getIn().getBody(), (PipelineContext)exchange.getProperty("drools-context"));
-// exchange.getOut().setBody(this.transformer.getPayload());
-// exchange.setProperty("drools-context",this.transformer.getContext() );
+ // this.transformer.processPayload(exchange.getIn().getBody(),
+ // (PipelineContext)exchange.getProperty("drools-context"));
+ // exchange.getOut().setBody(this.transformer.getPayload());
+ // exchange.setProperty("drools-context",this.transformer.getContext()
+ // );
-
- PipelineContext context = (PipelineContext) exchange.getProperty("drools-context");
- //Document d = processTranslate(exchange.getIn().getBody());
- Document d = exchange.getIn().getBody(Document.class);
- String name = d.getDocumentElement().getAttribute("lookup");
- XStream xstream = this.xstreamStrategy.lookup(name);
- if (xstream == null) {
- throw new IllegalArgumentException("Unable to lookup XStream parser using name '" + name + "'");
- }
- ServiceManagerPipelineContextImpl vsmContext = (ServiceManagerPipelineContextImpl) exchange.getProperty("drools-context");
- ServiceManager sm = vsmContext.getServiceManager();
- CommandExecutor executor = sm.lookup(name);
- if (executor == null) {
- throw new IllegalArgumentException("Unable to lookup CommandExecutor using name '" + name + "'");
- }
- vsmContext.setCommandExecutor(executor);
- ClassLoader cl = null;
- if (executor instanceof StatefulKnowledgeSessionImpl) {
- cl = ((ReteooRuleBase) (( StatefulKnowledgeSessionImpl ) executor).getRuleBase()).getRootClassLoader();
- xstream.setClassLoader(cl);
- } else if (executor instanceof StatelessKnowledgeSessionImpl) {
- cl = ((ReteooRuleBase) (( StatelessKnowledgeSessionImpl ) executor).getRuleBase()).getRootClassLoader();
- } else {
- throw new IllegalArgumentException("Unable to set ClassLoader on " + executor);
- }
+ PipelineContext context = (PipelineContext) exchange.getProperty("drools-context");
+ Document d = exchange.getIn().getBody(Document.class);
+ String name = d.getDocumentElement().getAttribute("lookup");
+ XStream xstream = this.xstreamStrategy.lookup(name);
+ if (xstream == null) {
+ throw new IllegalArgumentException(
+ "Unable to lookup XStream parser using name '" + name + "'");
+ }
+ ServiceManagerPipelineContextImpl vsmContext =
+ (ServiceManagerPipelineContextImpl) exchange.getProperty("drools-context");
+ ServiceManager sm = vsmContext.getServiceManager();
+ CommandExecutor executor = sm.lookup(name);
+ if (executor == null) {
+ throw new IllegalArgumentException("Unable to lookup CommandExecutor using name '" + name + "'");
+ }
+ vsmContext.setCommandExecutor(executor);
+ ClassLoader cl = null;
+ if (executor instanceof StatefulKnowledgeSessionImpl) {
+ cl = ((ReteooRuleBase) ((StatefulKnowledgeSessionImpl) executor).getRuleBase()).getRootClassLoader();
xstream.setClassLoader(cl);
- vsmContext.setClassLoader(cl);
- Object payload = xstream.unmarshal(new DomReader(d));
+ } else if (executor instanceof StatelessKnowledgeSessionImpl) {
+ cl = ((ReteooRuleBase) ((StatelessKnowledgeSessionImpl) executor).getRuleBase()).getRootClassLoader();
+ } else {
+ throw new IllegalArgumentException("Unable to set ClassLoader on " + executor);
+ }
+ xstream.setClassLoader(cl);
+ vsmContext.setClassLoader(cl);
+ Object payload = xstream.unmarshal(new DomReader(d));
- context.getProperties().put("xstream-instance", xstream);
- exchange.setProperty("drools-context", context);
- return payload;
+ context.getProperties().put("xstream-instance", xstream);
+ exchange.setProperty("drools-context", context);
+ return payload;
}
- private Document processTranslate(Object object) {
+
+ private Document processTranslate(Object object) {
// Create a DOM builder and parse the fragment
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document d = null;
@@ -149,11 +150,9 @@
throw new IllegalArgumentException("signal object must be instance of InputStream or Resource");
}
} catch (Exception e) {
- //handleException(this, object, e);
+ // handleException(this, object, e);
e.printStackTrace();
}
return d;
}
-
-
}
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/pipeline/camel/DroolsCamelContextInit.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/pipeline/camel/DroolsCamelContextInit.java 2009-12-22 03:41:42 UTC (rev 30794)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/pipeline/camel/DroolsCamelContextInit.java 2009-12-22 03:49:57 UTC (rev 30795)
@@ -13,16 +13,14 @@
*/
public class DroolsCamelContextInit {
- private ServiceManagerPipelineContextImpl context;
+ private ServiceManagerPipelineContextImpl context;
- public DroolsCamelContextInit(ServiceManager serviceManager) {
+ public DroolsCamelContextInit(ServiceManager serviceManager) {
+ this.context = new ServiceManagerPipelineContextImpl(serviceManager, null);
+ }
- this.context = new ServiceManagerPipelineContextImpl(serviceManager, null);
- }
- @Handler
- public void initialize(Exchange exchange) throws Exception {
-
- exchange.setProperty("drools-context", context);
- }
-
+ @Handler
+ public void initialize(Exchange exchange) throws Exception {
+ exchange.setProperty("drools-context", context);
+ }
}
Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/dataformat/drools-xstream
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/dataformat/drools-xstream (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/resources/META-INF/services/org/apache/camel/dataformat/drools-xstream 2009-12-22 03:49:57 UTC (rev 30795)
@@ -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.DroolsXStreamDataFormat
+
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithMarshallersTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithMarshallersTest.java 2009-12-22 03:41:42 UTC (rev 30794)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithMarshallersTest.java 2009-12-22 03:49:57 UTC (rev 30795)
@@ -33,88 +33,87 @@
public void testSimple(){
}
-// public void testSessionInsert() throws Exception {
-//
-// String cmd = "" +
-// "<batch-execution lookup=\"ksession1\">\n" +
-// "<insert out-identifier=\"salaboy\">\n" +
-// "<org.drools.pipeline.camel.Person>\n" +
-// "<name>salaboy</name>\n" +
-// "</org.drools.pipeline.camel.Person>\n" +
-// "</insert>\n" +
-// "<fire-all-rules/>\n" +
-// "</batch-execution>\n";
-//
-// String outXml = new String((byte[])template.requestBody("direct:test-with-session", cmd));
-//
-// ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(outXml);
-// Person person = (Person) result.getValue("salaboy");
-// assertEquals("salaboy",
-// person.getName());
-//
-// String expectedXml = "<execution-results>\n";
-// expectedXml += " <result identifier=\"salaboy\">\n";
-// expectedXml += " <org.drools.pipeline.camel.Person>\n";
-// expectedXml += " <name>salaboy</name>\n";
-// expectedXml += " </org.drools.pipeline.camel.Person>\n";
-// expectedXml += " </result>\n";
-// expectedXml += " <fact-handle identifier=\"salaboy\" externalForm=\"" + ((InternalFactHandle) result.getFactHandle("salaboy")).toExternalForm() + "\"/>\n";
-// expectedXml += "</execution-results>";
-//
-// assertEquals(expectedXml, outXml);
-//
-// }
-//
-// public void testNoSessionInsert() throws Exception {
-//
-// String cmd = "" +
-// "<batch-execution lookup=\"ksession1\">\n" +
-// "<insert out-identifier=\"salaboy\">\n" +
-// "<org.drools.pipeline.camel.Person>\n" +
-// "<name>salaboy</name>\n" +
-// "</org.drools.pipeline.camel.Person>\n" +
-// "</insert>\n" +
-// "<fire-all-rules/>\n" +
-// "</batch-execution>\n";
-//
-// String outXml = new String((byte[])template.requestBodyAndHeader("direct:test-no-session", cmd,
-// DroolsComponent.DROOLS_LOOKUP, "ksession1"));
-//
-// ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(outXml);
-// Person person = (Person) result.getValue("salaboy");
-// assertEquals("salaboy",
-// person.getName());
-//
-// String expectedXml = "<execution-results>\n";
-// expectedXml += " <result identifier=\"salaboy\">\n";
-// expectedXml += " <org.drools.pipeline.camel.Person>\n";
-// expectedXml += " <name>salaboy</name>\n";
-// expectedXml += " </org.drools.pipeline.camel.Person>\n";
-// expectedXml += " </result>\n";
-// expectedXml += " <fact-handle identifier=\"salaboy\" externalForm=\"" + ((InternalFactHandle) result.getFactHandle("salaboy")).toExternalForm() + "\"/>\n";
-// expectedXml += "</execution-results>";
-//
-// assertEquals(expectedXml, outXml);
-// }
+ public void testSessionInsert() throws Exception {
-// public void testSessionGetObject() throws Exception {
-// FactHandle factHandle = new DisconnectedFactHandle(handle);
-// GetObjectCommand cmd = (GetObjectCommand) CommandFactory.newGetObject(factHandle);
-// cmd.setOutIdentifier("rider");
-//
-// ExecutionResults response = (ExecutionResults) template.requestBody("direct:test-with-session", cmd);
-// assertTrue("Expected valid ExecutionResults object", response != null);
-// assertTrue("ExecutionResults missing expected object", response.getValue("rider") != null);
-// assertTrue("FactHandle object not of expected type", response.getValue("rider") instanceof Person);
-// assertEquals("Hadrian", ((Person)response.getValue("rider")).getName());
-// }
+ String cmd = "" +
+ "<batch-execution lookup=\"ksession1\">\n" +
+ "<insert out-identifier=\"salaboy\">\n" +
+ "<org.drools.pipeline.camel.Person>\n" +
+ "<name>salaboy</name>\n" +
+ "</org.drools.pipeline.camel.Person>\n" +
+ "</insert>\n" +
+ "<fire-all-rules/>\n" +
+ "</batch-execution>\n";
+ String outXml = new String((byte[])template.requestBody("direct:test-with-session", cmd));
+
+ ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(outXml);
+ Person person = (Person) result.getValue("salaboy");
+ assertEquals("salaboy", person.getName());
+
+ String expectedXml = "<execution-results>\n";
+ expectedXml += " <result identifier=\"salaboy\">\n";
+ expectedXml += " <org.drools.pipeline.camel.Person>\n";
+ expectedXml += " <name>salaboy</name>\n";
+ expectedXml += " </org.drools.pipeline.camel.Person>\n";
+ expectedXml += " </result>\n";
+ expectedXml += " <fact-handle identifier=\"salaboy\" externalForm=\"" + ((InternalFactHandle) result.getFactHandle("salaboy")).toExternalForm() + "\"/>\n";
+ expectedXml += "</execution-results>";
+
+ assertEquals(expectedXml, outXml);
+
+ }
+
+ public void testNoSessionInsert() throws Exception {
+
+ String cmd = "" +
+ "<batch-execution lookup=\"ksession1\">\n" +
+ "<insert out-identifier=\"salaboy\">\n" +
+ "<org.drools.pipeline.camel.Person>\n" +
+ "<name>salaboy</name>\n" +
+ "</org.drools.pipeline.camel.Person>\n" +
+ "</insert>\n" +
+ "<fire-all-rules/>\n" +
+ "</batch-execution>\n";
+
+ String outXml = new String((byte[])template.requestBodyAndHeader("direct:test-no-session", cmd,
+ DroolsComponent.DROOLS_LOOKUP, "ksession1"));
+
+ ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(outXml);
+ Person person = (Person) result.getValue("salaboy");
+ assertEquals("salaboy", person.getName());
+
+ String expectedXml = "<execution-results>\n";
+ expectedXml += " <result identifier=\"salaboy\">\n";
+ expectedXml += " <org.drools.pipeline.camel.Person>\n";
+ expectedXml += " <name>salaboy</name>\n";
+ expectedXml += " </org.drools.pipeline.camel.Person>\n";
+ expectedXml += " </result>\n";
+ expectedXml += " <fact-handle identifier=\"salaboy\" externalForm=\"" + ((InternalFactHandle) result.getFactHandle("salaboy")).toExternalForm() + "\"/>\n";
+ expectedXml += "</execution-results>";
+
+ assertEquals(expectedXml, outXml);
+ }
+
+ public void testSessionGetObject() throws Exception {
+ FactHandle factHandle = new DisconnectedFactHandle(handle);
+ GetObjectCommand cmd = (GetObjectCommand) CommandFactory.newGetObject(factHandle);
+ cmd.setOutIdentifier("rider");
+/*
+ ExecutionResults response = (ExecutionResults) template.requestBody("direct:test-with-session", cmd);
+ assertTrue("Expected valid ExecutionResults object", response != null);
+ assertTrue("ExecutionResults missing expected object", response.getValue("rider") != null);
+ assertTrue("FactHandle object not of expected type", response.getValue("rider") instanceof Person);
+ assertEquals("Hadrian", ((Person)response.getValue("rider")).getName());
+*/
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
- from("direct:test-with-session").to("drools://sm/ksession1?dataType=xstream");
- from("direct:test-no-session").to("drools://sm?pipeline=test-no-session&dataType=xstream");
+ from("direct:test-with-session").to("drools:sm/ksession1?dataFormat=drools-xstream");
+ from("direct:test-no-session").to("drools:sm?dataFormat=drools-xstream");
}
};
}
More information about the jboss-svn-commits
mailing list