[jboss-svn-commits] JBL Code SVN: r7859 - in labs/jbossesb/trunk/product/core/services: rules src/org/jboss/internal/soa/esb/services/routing/cbr tests/src/org/jboss/soa/esb/services/routing/cbr
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Nov 27 19:38:47 EST 2006
Author: kurt.stam at jboss.com
Date: 2006-11-27 19:38:44 -0500 (Mon, 27 Nov 2006)
New Revision: 7859
Modified:
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
labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java
Log:
adding tests for GreaterThen and SmallerThen
Modified: labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl
===================================================================
--- labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl 2006-11-27 21:47:09 UTC (rev 7858)
+++ labs/jbossesb/trunk/product/core/services/rules/JBossESBRules-XPath.drl 2006-11-28 00:38:44 UTC (rev 7859)
@@ -27,3 +27,21 @@
Log : "XPath Equals";
Destination : "test_category:JBOSS_XML_XPath_Dave_Destination";
end
+
+rule "Routing Rule using XPATH greater than"
+
+ when
+ xpathGreaterThan "/price" , "1.00"
+ then
+ Log : "XPath Greater Then";
+ Destination : "test_category:JBOSS_XML_XPath_GreaterThan_Destination";
+end
+
+rule "Routing Rule using XPATH less than"
+
+ when
+ xpathLessThan "/price", "1.00"
+ then
+ Log : "XPath Less Then";
+ Destination : "test_category:JBOSS_XML_XPath_LessThan_Destination";
+end
Modified: labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl
===================================================================
--- labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl 2006-11-27 21:47:09 UTC (rev 7858)
+++ labs/jbossesb/trunk/product/core/services/rules/XPathLanguage.dsl 2006-11-28 00:38:44 UTC (rev 7859)
@@ -1,5 +1,7 @@
#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}") )
+[when]xpathGreaterThan "{xpath}", "{value}"=msg : Message( type == MessageType.JBOSS_XML ) and eval( org.jboss.internal.soa.esb.services.routing.cbr.DslHelper.xmlContentGreaterThan(msg, "{xpath}", "{value}") )
+[when]xpathLessThan "{xpath}", "{value}"=msg : Message( type == MessageType.JBOSS_XML ) and eval( org.jboss.internal.soa.esb.services.routing.cbr.DslHelper.xmlContentLessThan(msg, "{xpath}", "{value}") )
[then]Log : "{message}"=System.out.println("{message}");
[then]Destination : "{message}"=destinationServices.add("{message}");
Modified: 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-27 21:47:09 UTC (rev 7858)
+++ labs/jbossesb/trunk/product/core/services/src/org/jboss/internal/soa/esb/services/routing/cbr/DslHelper.java 2006-11-28 00:38:44 UTC (rev 7859)
@@ -28,8 +28,10 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
+import org.apache.log4j.Logger;
import org.jboss.soa.esb.message.Message;
import org.xml.sax.InputSource;
+
/**
* Domain Specific Language helper. Right now this supports the use of XPath, but this class can
* be beefed up upo to use other technologies as well.
@@ -39,6 +41,7 @@
*/
public class DslHelper
{
+ private static Logger log = Logger.getLogger(DslHelper.class);
/** XPath instance */
private static XPathFactory xpf = XPathFactory.newInstance();
/**
@@ -51,9 +54,15 @@
*/
public static boolean xmlContentMatches(Message message, String xpathExp) throws XPathExpressionException
{
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to match xpath: '" + xpathExp + "' in message=" + message);
+ }
XPath xpath = xpf.newXPath();
InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
Object node = xpath.evaluate(xpathExp, inputSource, XPathConstants.NODE);
+ if (log.isDebugEnabled()) {
+ log.debug("Found node=" + node);
+ }
return node != null;
}
/**
@@ -67,10 +76,16 @@
*/
public static boolean xmlContentEquals(Message message, String xpathExp, String value) throws XPathExpressionException
{
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to match xpath: '" + xpathExp + "' in message=" + message.getBody().getContents());
+ }
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);
+ String nodeValue = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);
+ if (log.isDebugEnabled()) {
+ log.debug("Found nodeValue=" + nodeValue + " which is matched to given value=" + value);
+ }
+ return value.equals(nodeValue);
}
/**
@@ -84,20 +99,35 @@
*/
public static boolean xmlContentGreaterThan(Message message, String xpathExp, String value) throws XPathExpressionException
{
+ String nodeValue=null;
+ double doubleValue=0;
+ double doubleNodeValue=0;
+
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to match xpath: '" + xpathExp + "' in message=" + message.getBody().getContents());
+ }
XPath xpath = xpf.newXPath();
InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
- String node = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);
- try {
- double lValue = Double.parseDouble(value);
- double lNode = Double.parseDouble(node);
- } catch (NumberFormationException ne) {
- throw ne;
+ nodeValue = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);if (log.isDebugEnabled()) {
+ log.debug("Found nodeValue=" + nodeValue + " which is matched to given value=" + value);
}
- if (lNode > lValue)
- return true;
- else
- return false;
-
+ if (nodeValue!=null && !"".equals(nodeValue)) {
+
+ try {
+ doubleValue = Double.parseDouble(value);
+ } catch (NumberFormatException ne) {
+ log.equals("Could not parse value=" + doubleValue + " to double");
+ }
+ try {
+ doubleNodeValue = Double.parseDouble(nodeValue);
+ } catch (NumberFormatException ne) {
+ log.equals("Could not parse nodeValue=" + doubleNodeValue + " to double");
+ }
+ if (doubleNodeValue > doubleValue) {
+ return true;
+ }
+ }
+ return false;
}
/**
@@ -111,19 +141,35 @@
*/
public static boolean xmlContentLessThan(Message message, String xpathExp, String value) throws XPathExpressionException
{
+ String nodeValue=null;
+ double doubleValue=0;
+ double doubleNodeValue=0;
+
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to match xpath: '" + xpathExp + "' in message=" + message.getBody().getContents());
+ }
XPath xpath = xpf.newXPath();
- InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
- String node = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);
- try {
- double lValue = Double.parseDouble(value);
- double lNode = Double.parseDouble(node);
- } catch (NumberFormationException ne) {
- throw ne;
- }
- if (lNode < lValue)
- return true;
- else
- return false;
+ InputSource inputSource = new InputSource(new ByteArrayInputStream(message.getBody().getContents()));
+ nodeValue = (String) xpath.evaluate(xpathExp, inputSource, XPathConstants.STRING);if (log.isDebugEnabled()) {
+ log.debug("Found nodeValue=" + nodeValue + " which is matched to given value=" + value);
+ }
+ if (nodeValue!=null && !"".equals(nodeValue)) {
+
+ try {
+ doubleValue = Double.parseDouble(value);
+ } catch (NumberFormatException ne) {
+ log.equals("Could not parse value=" + doubleValue + " to double");
+ }
+ try {
+ doubleNodeValue = Double.parseDouble(nodeValue);
+ } catch (NumberFormatException ne) {
+ log.equals("Could not parse nodeValue=" + doubleNodeValue + " to double");
+ }
+ if (doubleNodeValue < doubleValue) {
+ return true;
+ }
+ }
+ return false;
}
}
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-27 21:47:09 UTC (rev 7858)
+++ labs/jbossesb/trunk/product/core/services/tests/src/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java 2006-11-28 00:38:44 UTC (rev 7859)
@@ -135,6 +135,42 @@
}
}
+ @Test
+ public void routeXMLMessageUsingXPathGreaterThen()
+ {
+ try {
+ //add new messages
+ Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+ //set the body inside the Message
+ message.getBody().setContents(("<price>1.55</price>").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_GreaterThan_Destination");
+ } catch (MessageRouterException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void routeXMLMessageUsingXPathLessThen()
+ {
+ try {
+ //add new messages
+ Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+ //set the body inside the Message
+ message.getBody().setContents(("<price>0.55</price>").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_LessThan_Destination");
+ } catch (MessageRouterException e) {
+ e.printStackTrace();
+ }
+ }
+
@BeforeClass
public static void runBeforeAllTests() throws Exception
{
More information about the jboss-svn-commits
mailing list