Moved MessageSelector interface and implementations into a new 'org.springframework.integration.selector' package (instead of a sub-package under 'message'). Also added support for "Strategy" enum in MessageSelector with the following values available [ALL, ANY, AT_LEAST_HALF, MORE_THAN_HALF] (INT-308).
This commit is contained in:
@@ -40,10 +40,10 @@ import org.springframework.integration.channel.AbstractPollableChannel;
|
||||
import org.springframework.integration.channel.ChannelInterceptor;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.security.channel.ChannelAccessPolicy;
|
||||
import org.springframework.integration.security.channel.ChannelInvocationDefinitionSource;
|
||||
import org.springframework.integration.security.channel.ChannelSecurityInterceptor;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.security.ConfigAttribute;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,82 +13,77 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.selector;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
/**
|
||||
* Base class for XPath {@link MessageSelector} implementations
|
||||
* Base class for XPath {@link MessageSelector} implementations.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractXPathMessageSelector implements MessageSelector {
|
||||
|
||||
private final XPathExpression xPathExpresion;
|
||||
|
||||
|
||||
private XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param xPathExpression simple String expression
|
||||
*/
|
||||
public AbstractXPathMessageSelector(String xPathExpression){
|
||||
public AbstractXPathMessageSelector(String xPathExpression) {
|
||||
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param xPathExpression
|
||||
* @param prefix
|
||||
* @param namespace
|
||||
*/
|
||||
public AbstractXPathMessageSelector(String xPathExpression, String prefix, String namespace){
|
||||
public AbstractXPathMessageSelector(String xPathExpression, String prefix, String namespace) {
|
||||
Map<String,String> namespaces = new HashMap<String, String>();
|
||||
namespaces.put(prefix, namespace);
|
||||
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression,namespaces);
|
||||
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param xPathExpression
|
||||
* @param namespaces
|
||||
*/
|
||||
public AbstractXPathMessageSelector(String xPathExpression, Map<String,String> namespaces){
|
||||
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression,namespaces);
|
||||
public AbstractXPathMessageSelector(String xPathExpression, Map<String,String> namespaces) {
|
||||
this.xPathExpresion = XPathExpressionFactory.createXPathExpression(xPathExpression, namespaces);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param xPathExpression
|
||||
*/
|
||||
public AbstractXPathMessageSelector(XPathExpression xPathExpression){
|
||||
public AbstractXPathMessageSelector(XPathExpression xPathExpression) {
|
||||
this.xPathExpresion = xPathExpression;
|
||||
}
|
||||
|
||||
|
||||
protected XmlPayloadConverter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converter used to convert payloads prior to XPAth testing
|
||||
* @param converter
|
||||
* Specify the converter used to convert payloads prior to XPath testing.
|
||||
*/
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
protected XmlPayloadConverter getConverter() {
|
||||
return this.converter;
|
||||
}
|
||||
|
||||
protected XPathExpression getXPathExpresion() {
|
||||
return xPathExpresion;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,69 +13,77 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.selector;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
|
||||
/**
|
||||
* Boolean XPath testing {@link MessageSelector}. Requires an XPathExpression
|
||||
* which can be evaluated using {@link XPathExpression} evaluateAsBoolean.
|
||||
* Supports payloads of type {@link Document} or {@link String}
|
||||
* @author Jonas Partner
|
||||
* which can be evaluated using {@link XPathExpression#evaluateAsBoolean(Node)}.
|
||||
* Supports payloads of type {@link Document} or {@link String}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class BooleanTestXPathMessageSelector extends AbstractXPathMessageSelector {
|
||||
|
||||
/**
|
||||
* Create a boolean testing XPath {@link MessageSelector} supporting
|
||||
* mutliple namespaces
|
||||
* @param pathExpression
|
||||
* mutliple namespaces.
|
||||
*
|
||||
* @param expression
|
||||
* @param namespaces
|
||||
*/
|
||||
public BooleanTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces) {
|
||||
super(pathExpression, namespaces);
|
||||
public BooleanTestXPathMessageSelector(String expression, Map<String, String> namespaces) {
|
||||
super(expression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a boolean testing XPath {@link MessageSelector} supporting a
|
||||
* single namespace
|
||||
* @param pathExpression
|
||||
* single namespace.
|
||||
*
|
||||
* @param expression
|
||||
* @param prefix
|
||||
* @param namespace
|
||||
*/
|
||||
public BooleanTestXPathMessageSelector(String pathExpression, String prefix, String namespace) {
|
||||
super(pathExpression, prefix, namespace);
|
||||
public BooleanTestXPathMessageSelector(String expression, String prefix, String namespace) {
|
||||
super(expression, prefix, namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean testing XPath {@link MessageSelector} with no namespace
|
||||
* support
|
||||
* @param pathExpression
|
||||
* Create a boolean testing XPath {@link MessageSelector} with no namespace
|
||||
* support.
|
||||
*
|
||||
* @param expression
|
||||
*/
|
||||
public BooleanTestXPathMessageSelector(String pathExpression) {
|
||||
super(pathExpression);
|
||||
public BooleanTestXPathMessageSelector(String expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean testing XPath {@link MessageSelector} using the
|
||||
* provided {@link XPathExpression}
|
||||
* @param pathExpression
|
||||
* Create a boolean testing XPath {@link MessageSelector} using the
|
||||
* provided {@link XPathExpression}.
|
||||
*
|
||||
* @param expression
|
||||
*/
|
||||
public BooleanTestXPathMessageSelector(XPathExpression pathExpression) {
|
||||
super(pathExpression);
|
||||
public BooleanTestXPathMessageSelector(XPathExpression expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* return true if the {@link XPathExpression} evaluates to <code>true</code>
|
||||
* Return true if the {@link XPathExpression} evaluates to <code>true</code>
|
||||
*/
|
||||
public boolean accept(Message<?> message) {
|
||||
Node node = getConverter().convertToNode(message.getPayload());
|
||||
return getXPathExpresion().evaluateAsBoolean(node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,21 +13,23 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.selector;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
|
||||
/**
|
||||
* XPath {@link MessageSelector} which tests for a provided value Supports
|
||||
* payloads of type {@link Document} or {@link String}
|
||||
* @author Jonas Partner
|
||||
* XPath {@link MessageSelector} that tests if a provided value supports
|
||||
* payloads of type {@link Document} or {@link String}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class StringValueTestXPathMessageSelector extends AbstractXPathMessageSelector {
|
||||
|
||||
@@ -35,75 +37,79 @@ public class StringValueTestXPathMessageSelector extends AbstractXPathMessageSel
|
||||
|
||||
private volatile boolean caseSensitive = true;
|
||||
|
||||
|
||||
/**
|
||||
* Create a selector which tests for the given value and supports multiple
|
||||
* namespaces
|
||||
* @param pathExpression
|
||||
* namespaces.
|
||||
*
|
||||
* @param expression
|
||||
* @param namespaces
|
||||
* @param valueToTestFor
|
||||
*/
|
||||
public StringValueTestXPathMessageSelector(String pathExpression, Map<String, String> namespaces,
|
||||
String valueToTestFor) {
|
||||
super(pathExpression, namespaces);
|
||||
public StringValueTestXPathMessageSelector(String expression, Map<String, String> namespaces, String valueToTestFor) {
|
||||
super(expression, namespaces);
|
||||
this.valueToTestFor = valueToTestFor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a single namespace Xpath selector
|
||||
* @param pathExpression
|
||||
* Creates a single namespace Xpath selector.
|
||||
*
|
||||
* @param expression
|
||||
* @param prefix
|
||||
* @param namespace
|
||||
* @param valueToTestFor
|
||||
*/
|
||||
public StringValueTestXPathMessageSelector(String pathExpression, String prefix, String namespace,
|
||||
String valueToTestFor) {
|
||||
super(pathExpression, prefix, namespace);
|
||||
public StringValueTestXPathMessageSelector(String expression, String prefix, String namespace, String valueToTestFor) {
|
||||
super(expression, prefix, namespace);
|
||||
this.valueToTestFor = valueToTestFor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates non namespaced testing selector
|
||||
* @param pathExpression
|
||||
* Creates non-namespaced testing selector.
|
||||
*
|
||||
* @param expression
|
||||
* @param valueToTestFor
|
||||
*/
|
||||
public StringValueTestXPathMessageSelector(String pathExpression, String valueToTestFor) {
|
||||
|
||||
super(pathExpression);
|
||||
public StringValueTestXPathMessageSelector(String expression, String valueToTestFor) {
|
||||
super(expression);
|
||||
this.valueToTestFor = valueToTestFor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates selector with provided {@link XPathExpression}
|
||||
* @param pathExpression
|
||||
* Creates a selector with the provided {@link XPathExpression}.
|
||||
*
|
||||
* @param expression
|
||||
* @param valueToTestFor
|
||||
*/
|
||||
public StringValueTestXPathMessageSelector(XPathExpression pathExpression, String valueToTestFor) {
|
||||
super(pathExpression);
|
||||
public StringValueTestXPathMessageSelector(XPathExpression expression, String valueToTestFor) {
|
||||
super(expression);
|
||||
this.valueToTestFor = valueToTestFor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the payload returning true if the value returned by the
|
||||
* {@link XPathExpression} is equal to the valueToTestFor
|
||||
*/
|
||||
public boolean accept(Message<?> message) {
|
||||
Node nodeToTest = getConverter().convertToNode(message.getPayload());
|
||||
String xPathResult = getXPathExpresion().evaluateAsString(nodeToTest);
|
||||
if (caseSensitive) {
|
||||
return valueToTestFor.equals(xPathResult);
|
||||
}
|
||||
else {
|
||||
return valueToTestFor.equalsIgnoreCase(xPathResult);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* should comparison of value returned by {@link XPathExpression} to test
|
||||
* value be case sensitive
|
||||
* Specify whether comparison of value returned by {@link XPathExpression}
|
||||
* to test value should be case sensitive. Default is 'true'.
|
||||
*
|
||||
* @param caseSensitive
|
||||
*/
|
||||
public void setCaseSensitive(boolean caseSensitive) {
|
||||
this.caseSensitive = caseSensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the payload and return true if the value returned by the
|
||||
* {@link XPathExpression} is equal to the <code>valueToTestFor</code>.
|
||||
*/
|
||||
public boolean accept(Message<?> message) {
|
||||
Node nodeToTest = getConverter().convertToNode(message.getPayload());
|
||||
String xPathResult = getXPathExpresion().evaluateAsString(nodeToTest);
|
||||
if (this.caseSensitive) {
|
||||
return this.valueToTestFor.equals(xPathResult);
|
||||
}
|
||||
else {
|
||||
return this.valueToTestFor.equalsIgnoreCase(xPathResult);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,17 +13,22 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XPathSelectorParserTests {
|
||||
|
||||
@Test
|
||||
@@ -43,8 +48,7 @@ public class XPathSelectorParserTests {
|
||||
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>"))));
|
||||
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithNestedMap() throws Exception {
|
||||
StringBuffer contextXml = new StringBuffer("<si-xml:xpath-selector id='selector' evaluation-result-type='boolean'>");
|
||||
@@ -57,7 +61,8 @@ public class XPathSelectorParserTests {
|
||||
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public MessageSelector getSelector( String testcontextXml) throws Exception{
|
||||
TestXmlApplicationContext ctx =
|
||||
TestXmlApplicationContextHelper.getTestAppContext(testcontextXml);
|
||||
@@ -65,6 +70,4 @@ public class XPathSelectorParserTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* A channel implementation that essentially behaves like "/dev/null".
|
||||
|
||||
@@ -19,7 +19,7 @@ package org.springframework.integration.channel;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* A channel implementation that stores messages in a thread-bound queue. In
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.List;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* A {@link org.springframework.integration.channel.ChannelInterceptor} that
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.integration.channel.BlockingChannel;
|
||||
import org.springframework.integration.channel.ChannelInterceptor;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.channel.interceptor.MessageSelectingInterceptor;
|
||||
import org.springframework.integration.message.selector.PayloadTypeSelector;
|
||||
import org.springframework.integration.selector.PayloadTypeSelector;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.integration.message.selector.MessageSelectorChain;
|
||||
import org.springframework.integration.selector.MessageSelectorChain;
|
||||
|
||||
/**
|
||||
* Parser for the <selector-chain/> element.
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.integration.message.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageProducer;
|
||||
import org.springframework.integration.message.MessageRejectedException;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ package org.springframework.integration.filter;
|
||||
import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer;
|
||||
import org.springframework.integration.endpoint.ReplyMessageHolder;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMappingMethodInvoker;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.message.selector;
|
||||
package org.springframework.integration.selector;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
@@ -14,12 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.message.selector;
|
||||
package org.springframework.integration.selector;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A message selector implementation that passes incoming messages through a
|
||||
@@ -30,9 +31,19 @@ import org.springframework.integration.message.Message;
|
||||
*/
|
||||
public class MessageSelectorChain implements MessageSelector {
|
||||
|
||||
public static enum Strategy { ALL, MORE_THAN_HALF, AT_LEAST_HALF, ANY };
|
||||
|
||||
|
||||
private volatile Strategy strategy = Strategy.ALL;
|
||||
|
||||
private final List<MessageSelector> selectors = new CopyOnWriteArrayList<MessageSelector>();
|
||||
|
||||
|
||||
public void setStrategy(Strategy strategy) {
|
||||
Assert.notNull(strategy, "strategy must not be null");
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a selector to the end of the chain.
|
||||
*/
|
||||
@@ -51,8 +62,11 @@ public class MessageSelectorChain implements MessageSelector {
|
||||
* Initialize the selector chain. Removes any existing selectors.
|
||||
*/
|
||||
public void setSelectors(List<MessageSelector> selectors) {
|
||||
this.selectors.clear();
|
||||
this.selectors.addAll(selectors);
|
||||
Assert.notEmpty(selectors, "selectors must not be empty");
|
||||
synchronized (this.selectors) {
|
||||
this.selectors.clear();
|
||||
this.selectors.addAll(selectors);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,12 +75,39 @@ public class MessageSelectorChain implements MessageSelector {
|
||||
* If all selectors accept, this method will return 'true'.
|
||||
*/
|
||||
public final boolean accept(Message<?> message) {
|
||||
int count = 0;
|
||||
int accepted = 0;
|
||||
for (MessageSelector next : this.selectors) {
|
||||
if (!next.accept(message)) {
|
||||
count++;
|
||||
if (next.accept(message)) {
|
||||
if (this.strategy.equals(Strategy.ANY)) {
|
||||
return true;
|
||||
}
|
||||
accepted++;
|
||||
}
|
||||
else if (this.strategy.equals(Strategy.ALL)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return this.decide(accepted, count);
|
||||
}
|
||||
|
||||
private boolean decide(int accepted, int total) {
|
||||
if (accepted == 0) {
|
||||
return false;
|
||||
}
|
||||
switch (this.strategy) {
|
||||
case ANY:
|
||||
return true;
|
||||
case ALL:
|
||||
return (accepted == total);
|
||||
case MORE_THAN_HALF:
|
||||
return (2 * accepted) > total;
|
||||
case AT_LEAST_HALF:
|
||||
return (2 * accepted) >= total;
|
||||
default:
|
||||
throw new IllegalArgumentException("unsupported strategy " + this.strategy);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.message.selector;
|
||||
package org.springframework.integration.selector;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.message.selector;
|
||||
package org.springframework.integration.selector;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.UnexpiredMessageSelector;
|
||||
import org.springframework.integration.selector.UnexpiredMessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -24,8 +24,8 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.message.selector.MessageSelectorChain;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelectorChain;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.integration.config;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.integration.config;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
ref="testHandler" selector="typeSelector">
|
||||
</service-activator>
|
||||
|
||||
<beans:bean id="typeSelector" class="org.springframework.integration.message.selector.PayloadTypeSelector">
|
||||
<beans:bean id="typeSelector" class="org.springframework.integration.selector.PayloadTypeSelector">
|
||||
<beans:constructor-arg value="java.lang.String"/>
|
||||
</beans:bean>
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.MessageRejectedException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.TestHandlers;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -37,8 +37,8 @@ import org.springframework.integration.message.MessageRejectedException;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.TestHandlers;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.message.selector.MessageSelectorChain;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelectorChain;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.integration.endpoint.SubscribingConsumerEndpoint;
|
||||
import org.springframework.integration.filter.MessageFilter;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.selector.MessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.integration.message.ErrorMessage;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.selector.PayloadTypeSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.selector.UnexpiredMessageSelector;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.selector;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MessageSelectorChainTests {
|
||||
|
||||
private final Message<?> message = new StringMessage("test");
|
||||
|
||||
|
||||
@Test
|
||||
public void anyStrategyAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.ANY);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anyStrategyRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.ANY);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allStrategyAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.ALL);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allStrategyRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.ALL);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atLeastHalfStrategyWithOddNumberAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.AT_LEAST_HALF);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atLeastHalfStrategyWithEvenNumberAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.AT_LEAST_HALF);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atLeastHalfStrategyWithOddNumberRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.AT_LEAST_HALF);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atLeastHalfStrategyWithEvenNumberRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.AT_LEAST_HALF);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(false));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moreThanHalfStrategyWithOddNumberAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.MORE_THAN_HALF);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moreThanHalfStrategyWithEvenNumberAccepts() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.MORE_THAN_HALF);
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
assertTrue(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moreThanHalfStrategyWithOddNumberRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.MORE_THAN_HALF);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moreThanHalfStrategyWithEvenNumberRejects() {
|
||||
MessageSelectorChain chain = new MessageSelectorChain();
|
||||
chain.setStrategy(MessageSelectorChain.Strategy.MORE_THAN_HALF);
|
||||
chain.add(new TestSelector(false));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(true));
|
||||
chain.add(new TestSelector(false));
|
||||
assertFalse(chain.accept(message));
|
||||
}
|
||||
|
||||
|
||||
private static class TestSelector implements MessageSelector {
|
||||
|
||||
private final boolean accept;
|
||||
|
||||
private TestSelector(boolean accept) {
|
||||
this.accept = accept;
|
||||
}
|
||||
|
||||
public boolean accept(Message<?> message) {
|
||||
return this.accept;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user