[jboss-svn-commits] JBL Code SVN: r7479 - in labs/jbossesb/trunk/product/core: listeners/src/org/jboss/soa/esb/actions listeners/src/org/jboss/soa/esb/listeners listeners/src/org/jboss/soa/esb/listeners/message services services/rules services/src/org/jboss/internal/soa/esb/services/routing/cbr services/src/org/jboss/soa/esb/services/routing/cbr services/tests/src/org/jboss/soa/esb/services/routing/cbr

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Nov 8 13:53:44 EST 2006


Author: kurt.stam at jboss.com
Date: 2006-11-08 13:53:39 -0500 (Wed, 08 Nov 2006)
New Revision: 7479

Added:
   labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl
   labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl
   labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/DslHelper.java
Modified:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/CbrProxyAction.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/ListenerTagNames.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/CbrJmsQueueListener.java
   labs/jbossesb/trunk/product/core/services/build.xml
   labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
   labs/jbossesb/trunk/product/core/services/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRouter.java
   labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java
Log:
JBESB-134

Adding XPath based CBR

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/CbrProxyAction.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/CbrProxyAction.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/CbrProxyAction.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -129,8 +129,8 @@
 						//The selectors will do the job.
 						JMSEpr jpr = (JMSEpr)epr;
 						JMSEpr replyEpr =  new JMSEpr(jpr.getDestinationType()  ,jpr.getDestinationName()
-							,jpr.getConnectionFactory(),jpr.getJndiType(),jpr.getJndiURL()
-							,jmsQueueReplyListener.getReplySelector());
+							                         ,jpr.getConnectionFactory(),jpr.getJndiType()
+							                         ,jpr.getJndiURL()          ,jmsQueueReplyListener.getReplySelector());
 						if (message.getHeader().getCall()==null) {
 							Call call = new Call();
 							message.getHeader().setCall(call);

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/ListenerTagNames.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/ListenerTagNames.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/ListenerTagNames.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -27,6 +27,7 @@
 
     /** RuleSet reference (Filename) */
     public static final String RULE_SET_TAG                 = "ruleSet";
+    public static final String RULE_LANGUAGE_TAG            = "ruleLanguage";
 
     /** Poller tagnames   */
     public static final String POLL_LATENCY_SECS_TAG		= "pollLatencySeconds";

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/CbrJmsQueueListener.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/CbrJmsQueueListener.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/CbrJmsQueueListener.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -72,6 +72,7 @@
     	try {
 	    	ContentBasedRouter cbr =ContentBasedRouterFactory.getRouter();
 	    	String ruleSet=null;
+	    	String ruleLanguage=null;
 	    	if (_config.getFirstChild(ListenerTagNames.CBR_TAG)==null) {
 	    		_logger.error("Required child element " + ListenerTagNames.CBR_TAG + " not found in " + _config.getName() + ".");
 	    		throw new ListenerInitializationException("Required child element " + ListenerTagNames.CBR_TAG + " not found.");
@@ -80,6 +81,7 @@
 	    		if (ruleSet==null) {
 	    			throw new ListenerInitializationException("Required attribute " + ListenerTagNames.RULE_SET_TAG + " not found.");
 	    		}
+	    		ruleLanguage =_config.getFirstChild(ListenerTagNames.CBR_TAG).getAttribute(ListenerTagNames.RULE_LANGUAGE_TAG);
 	    	}
 	    	_controller.register(_config,_epr);
 	    	while (_controller.continueLooping()) {
@@ -87,7 +89,7 @@
 	    		Message message = (lwait > 0 ) ? receiveEsbMessage(100) : null;
 	        	if (null!=message) {
         			_logger.log(Priority.DEBUG, "Sending message to the Content Based Router.");
-        			Collection<String> destinationServices = cbr.route(ruleSet, message);
+        			Collection<String> destinationServices = cbr.route(ruleSet, ruleLanguage, message);
         			if (Boolean.FALSE.equals(message.getProperties().getProperty(MessageRouter.DELIVER_MESSAGES))) {
         				message.getProperties().setProperty(MessageRouter.ROUTING_DESTINATION_SERVICE_LIST, destinationServices);
         				try  { 

Modified: labs/jbossesb/trunk/product/core/services/build.xml
===================================================================
--- labs/jbossesb/trunk/product/core/services/build.xml	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/build.xml	2006-11-08 18:53:39 UTC (rev 7479)
@@ -119,7 +119,7 @@
                 />
     	<jar    destfile="${org.jboss.esb.services.dist.dir}/lib/jbossesb-rules.jar"
     	                basedir="${org.jboss.esb.services.rules.dir}"
-    	                includes="**/*.drl"
+    	                includes="**/*.drl,**/*.dsl"
     	                />
 
 	<antcall target="-ear"/>

Added: labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl
===================================================================
--- labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl	2006-11-08 18:53:39 UTC (rev 7479)
@@ -0,0 +1,29 @@
+#created on: Nov 8, 2006
+package com.jboss.soa.esb.routing.cbr
+
+#list any import classes here.
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageType;
+
+expander XPathLanguage.dsl
+
+#declare any global variables here
+global java.util.List destinationServices;
+
+rule "Routing Rule using XPATH match"
+	
+	when
+		xpathMatch "/jbossesb"
+	then 
+		Log : "XPath Matched";
+		Destination : "test_category:JBOSS_XML_XPath_Destination";
+end
+
+rule "Routing Rule using XPATH equals"
+	
+	when
+		xpathEquals "/Dave", "rocks"
+	then 
+		Log : "XPath Equals";
+		Destination : "test_category:JBOSS_XML_XPath_Dave_Destination";
+end

Added: labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl
===================================================================
--- labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl	2006-11-08 18:53:39 UTC (rev 7479)
@@ -0,0 +1,5 @@
+#JBossESB Content Based Routing using XPath DSL
+[when]xpathMatch "{xpath}"=msg : Message( type == MessageType.JBOSS_XML ) and eval( org.jboss.internal.soa.esb.services.routing.cbr.DslHelper.xmlContentMatches(msg, "{xpath}") )
+[when]xpathEquals "{xpath}", "{value}"=msg : Message( type == MessageType.JBOSS_XML ) and eval( org.jboss.internal.soa.esb.services.routing.cbr.DslHelper.xmlContentEquals(msg, "{xpath}", "{value}") )
+[then]Log : "{message}"=System.out.println("{message}");
+[then]Destination : "{message}"=destinationServices.add("{message}");

Added: labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/DslHelper.java
===================================================================
--- labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/DslHelper.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/DslHelper.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -0,0 +1,33 @@
+package org.jboss.internal.soa.esb.services.routing.cbr;
+
+
+import java.io.ByteArrayInputStream;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.jboss.soa.esb.message.Message;
+import org.xml.sax.InputSource;
+
+public class DslHelper {
+	private static XPathFactory xpf = XPathFactory.newInstance();
+	
+	public static boolean xmlContentMatches(Message message, String xpathExp) throws XPathExpressionException 
+	{
+		XPath xpath = xpf.newXPath();
+		InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
+		Object node = xpath.evaluate(xpathExp, inputSource, XPathConstants.NODE);
+		return node != null;
+	}
+	
+	public static boolean xmlContentEquals(Message message, String xpathExp, String value) throws XPathExpressionException 
+	{
+		XPath xpath = xpf.newXPath();
+		InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
+		String node = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);
+		return value.equals(node);
+	}
+}
+	

Modified: labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
===================================================================
--- labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -1,3 +1,24 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
 package org.jboss.internal.soa.esb.services.routing.cbr;
 
 import java.io.InputStream;
@@ -20,7 +41,12 @@
 import org.jboss.soa.esb.services.routing.MessageRouterException;
 import org.jboss.soa.esb.services.routing.cbr.ContentBasedRouter;
 
-
+/**
+ * 
+ * 
+ * @author kstam at redhat.com
+ *
+ */
 public class JBossRulesRouter extends ContentBasedRouter 
 {
 	private static Map<String,WorkingMemory> workingMemories=new HashMap<String,WorkingMemory>();
@@ -41,15 +67,18 @@
 			//Route to /dev/null?
 		return null;
 	}
-
+    public List<String> route(String ruleSet, Message message) {
+    	return route(ruleSet, null, message);
+    }
+	
 	@SuppressWarnings("unchecked")
-	public List<String> route(String ruleSet, Message message) 
+	public List<String> route(String ruleSet, String ruleLanguage, Message message) 
 	{
 		List<String> destinationServices = new ArrayList<String>();
 		try {
 			if (!workingMemories.containsKey(ruleSet)) {
 			    logger.log(Priority.INFO, "Reading ruleSet from file=" + ruleSet);
-				RuleBase ruleBase = readRuleBase(ruleSet);
+				RuleBase ruleBase = readRuleBase(ruleSet, ruleLanguage);
 				WorkingMemory workingMemory = ruleBase.newWorkingMemory();
 				workingMemories.put(ruleSet, workingMemory);
 			}
@@ -78,16 +107,22 @@
      * Reading the rules from a file, we may want to allow other ways to 
      * set the rule.
      */
-	private static RuleBase readRuleBase(String rulesFile) throws Exception
+	private static RuleBase readRuleBase(String rulesFile, String ruleLanguage) throws Exception
 	{
 		//read in the rules
 		logger.info("Going to read the rule: " + rulesFile);
-		InputStream inputStream = JBossRulesRouter.class.getResourceAsStream( "/" + rulesFile );
-		Reader source = new InputStreamReader(inputStream);
+		InputStream inputStreamDrl = JBossRulesRouter.class.getResourceAsStream( "/" + rulesFile );
+		Reader rules = new InputStreamReader(inputStreamDrl);
 		PackageBuilder builder = new PackageBuilder();
 		//this wil parse and compile in one step
-		//NOTE: There are 2 methods here, the one argument one is for normal DRL.
-		builder.addPackageFromDrl( source );
+		if (ruleLanguage==null) {
+			builder.addPackageFromDrl(rules);
+		} else {
+			logger.info("Going to read the language: " + ruleLanguage);
+			InputStream inputStreamDsl = JBossRulesRouter.class.getResourceAsStream( "/" + ruleLanguage );
+			Reader dsl = new InputStreamReader(inputStreamDsl);
+			builder.addPackageFromDrl(rules, dsl);
+		}
 		//get the compiled package (which is serializable)
 		Package pkg = builder.getPackage();
 		//add the package to a rulebase (deploy the rule package).

Modified: labs/jbossesb/trunk/product/core/services/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRouter.java
===================================================================
--- labs/jbossesb/trunk/product/core/services/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRouter.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRouter.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -34,10 +34,20 @@
  */
 public abstract class ContentBasedRouter extends MessageRouter {
 	/**
+	 * Route a message using a certain ruleSet wich and reference a custom
+	 * Domain Specific Language (DSL).
+	 * 
+	 * @param ruleSet      - String reference to a file which contains a ruleSet.
+	 * @param ruleLanguage - String reference to a file which contais a custom rule language definition
+	 * @param message      - Message that needs routing.
+	 * @return
+	 */
+	public abstract List<String> route(String ruleSet, String ruleLanguage, Message message);
+	/**
 	 * Route a message using a certain ruleSet.
 	 * 
-	 * @param ruleSet - String reference to a file which contains a ruleSet.
-	 * @param message - Message that needs routing.
+	 * @param ruleSet      - String reference to a file which contains a ruleSet.
+	 * @param message      - Message that needs routing.
 	 * @return
 	 */
 	public abstract List<String> route(String ruleSet, Message message);

Modified: labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java	2006-11-08 18:32:21 UTC (rev 7478)
+++ labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java	2006-11-08 18:53:39 UTC (rev 7479)
@@ -93,6 +93,48 @@
 		}
 	}
 	
+	@Test
+	public void routeXMLMessageUsingXPathMatch()
+	{
+		try {
+			//add new messages
+			Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+			//set some properties inside the Message
+			message.getProperties().setProperty("prop1", "val1");
+			message.getProperties().setProperty("prop2", "val2");
+			//set the body inside the Message
+			message.getBody().setContents(("<jbossesb>TEST BODY</jbossesb>").getBytes());
+			//set some object attachments inside the Message
+			message.getAttachment().addItem(new String("TEST ATTACHMENT1"));
+			message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
+			message.getProperties().setProperty(MessageRouter.DELIVER_MESSAGES, Boolean.FALSE);
+		
+			ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter();
+			List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",message);
+			assertEquals(destinationServices.iterator().next(),"test_category:JBOSS_XML_XPath_Destination");
+		} catch (MessageRouterException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	@Test
+	public void routeXMLMessageUsingXPathEquals()
+	{
+		try {
+			//add new messages
+			Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+			//set the body inside the Message
+			message.getBody().setContents(("<Dave>rocks</Dave>").getBytes());
+			message.getProperties().setProperty(MessageRouter.DELIVER_MESSAGES, Boolean.FALSE);
+		
+			ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter();
+			List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",message);
+			assertEquals(destinationServices.iterator().next(),"test_category:JBOSS_XML_XPath_Dave_Destination");
+		} catch (MessageRouterException e) {
+			e.printStackTrace();
+		}
+	}
+	
 	@BeforeClass
 	public static void runBeforeAllTests() throws Exception 
 	{




More information about the jboss-svn-commits mailing list