Added MessageSelectorChain and modified TargetEndpoint to provide a 'setMessageSelector' method rather than managing a list of selectors with the 'addMessageSelector' method. Also modified the endpoint parser so that the configuration of a selector chain is more consistent with that of MessageHandlerChain. Now, the XML for endpoints uses a "selector" attribute instead of 0..n <selector/> sub-elements (INT-159).
This commit is contained in:
@@ -24,7 +24,6 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
@@ -46,16 +45,14 @@ public abstract class AbstractTargetEndpointParser extends AbstractSingleBeanDef
|
||||
|
||||
private static final String SUBSCRIPTION_PROPERTY = "subscription";
|
||||
|
||||
private static final String SELECTOR_ELEMENT = "selector";
|
||||
|
||||
private static final String REF_ATTRIBUTE = "ref";
|
||||
|
||||
private static final String SELECTORS_PROPERTY = "messageSelectors";
|
||||
|
||||
private static final String ERROR_HANDLER_ATTRIBUTE = "error-handler";
|
||||
|
||||
private static final String ERROR_HANDLER_PROPERTY = "errorHandler";
|
||||
|
||||
private static final String SELECTOR_ATTRIBUTE = "selector";
|
||||
|
||||
private static final String SELECTOR_PROPERTY = "messageSelector";
|
||||
|
||||
private static final String PERIOD_ATTRIBUTE = "period";
|
||||
|
||||
private static final String SCHEDULE_ELEMENT = "schedule";
|
||||
@@ -90,7 +87,6 @@ public abstract class AbstractTargetEndpointParser extends AbstractSingleBeanDef
|
||||
this.parseTarget(element, this.getTargetAttributeName(), parserContext, builder);
|
||||
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
|
||||
Schedule schedule = null;
|
||||
ManagedList selectors = new ManagedList();
|
||||
NodeList childNodes = element.getChildNodes();
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node child = childNodes.item(i);
|
||||
@@ -99,10 +95,6 @@ public abstract class AbstractTargetEndpointParser extends AbstractSingleBeanDef
|
||||
if (CONCURRENCY_ELEMENT.equals(localName)) {
|
||||
parseConcurrencyPolicy((Element) child, builder);
|
||||
}
|
||||
else if (SELECTOR_ELEMENT.equals(localName)) {
|
||||
String ref = ((Element) child).getAttribute(REF_ATTRIBUTE);
|
||||
selectors.add(new RuntimeBeanReference(ref));
|
||||
}
|
||||
else if (SCHEDULE_ELEMENT.equals(localName)) {
|
||||
schedule = this.parseSchedule((Element) child);
|
||||
}
|
||||
@@ -118,13 +110,14 @@ public abstract class AbstractTargetEndpointParser extends AbstractSingleBeanDef
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(subscriptionDef, subscriptionBeanName));
|
||||
builder.addPropertyReference(SUBSCRIPTION_PROPERTY, subscriptionBeanName);
|
||||
}
|
||||
if (selectors.size() > 0) {
|
||||
builder.addPropertyValue(SELECTORS_PROPERTY, selectors);
|
||||
}
|
||||
String errorHandlerRef = element.getAttribute(ERROR_HANDLER_ATTRIBUTE);
|
||||
if (StringUtils.hasText(errorHandlerRef)) {
|
||||
builder.addPropertyReference(ERROR_HANDLER_PROPERTY, errorHandlerRef);
|
||||
}
|
||||
String selectorRef = element.getAttribute(SELECTOR_ATTRIBUTE);
|
||||
if (StringUtils.hasText(selectorRef)) {
|
||||
builder.addPropertyReference(SELECTOR_PROPERTY, selectorRef);
|
||||
}
|
||||
this.postProcess(builder, element);
|
||||
}
|
||||
|
||||
|
||||
@@ -235,17 +235,6 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="selector">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Provides a message selector reference.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="ref" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="handler-chain">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
@@ -378,10 +367,10 @@
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="schedule" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element name="concurrency" type="concurrencyType" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element ref="selector" minOccurs="0" maxOccurs="unbounded"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="input-channel" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="error-handler" type="xsd:string"/>
|
||||
<xsd:attribute name="selector" type="xsd:string"/>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
|
||||
@@ -16,10 +16,8 @@
|
||||
|
||||
package org.springframework.integration.endpoint;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
@@ -55,7 +53,7 @@ public class TargetEndpoint extends AbstractEndpoint implements Target, ChannelR
|
||||
|
||||
private volatile ErrorHandler errorHandler;
|
||||
|
||||
private final List<MessageSelector> selectors = new CopyOnWriteArrayList<MessageSelector>();
|
||||
private volatile MessageSelector selector;
|
||||
|
||||
private volatile ChannelRegistry channelRegistry;
|
||||
|
||||
@@ -82,14 +80,8 @@ public class TargetEndpoint extends AbstractEndpoint implements Target, ChannelR
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public void setMessageSelectors(List<MessageSelector> selectors) {
|
||||
this.selectors.clear();
|
||||
this.selectors.addAll(selectors);
|
||||
}
|
||||
|
||||
public void addMessageSelector(MessageSelector messageSelector) {
|
||||
Assert.notNull(messageSelector, "'messageSelector' must not be null");
|
||||
this.selectors.add(messageSelector);
|
||||
public void setMessageSelector(MessageSelector selector) {
|
||||
this.selector = selector;
|
||||
}
|
||||
|
||||
public Subscription getSubscription() {
|
||||
@@ -184,10 +176,8 @@ public class TargetEndpoint extends AbstractEndpoint implements Target, ChannelR
|
||||
if (!this.isRunning()) {
|
||||
throw new MessageHandlerNotRunningException(message);
|
||||
}
|
||||
for (MessageSelector selector : this.selectors) {
|
||||
if (!selector.accept(message)) {
|
||||
return false;
|
||||
}
|
||||
if (this.selector != null && !this.selector.accept(message)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return this.target.send(message);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.message.selector;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* A message selector implementation that passes incoming messages through a
|
||||
* chain of selectors. The chain will be broken by any selector that returns
|
||||
* <em>false</em>.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MessageSelectorChain implements MessageSelector {
|
||||
|
||||
private final List<MessageSelector> selectors = new CopyOnWriteArrayList<MessageSelector>();
|
||||
|
||||
|
||||
/**
|
||||
* Add a selector to the end of the chain.
|
||||
*/
|
||||
public void add(MessageSelector selector) {
|
||||
this.selectors.add(selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a selector to the chain at the specified index.
|
||||
*/
|
||||
public void add(int index, MessageSelector selector) {
|
||||
this.selectors.add(index, selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the selector chain. Removes any existing selectors.
|
||||
*/
|
||||
public void setSelectors(List<MessageSelector> selectors) {
|
||||
this.selectors.clear();
|
||||
this.selectors.addAll(selectors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the message through the selector chain. As soon as a
|
||||
* selector returns 'false', this method will return 'false'.
|
||||
* If all selectors accept, this method will return 'true'.
|
||||
*/
|
||||
public final boolean accept(Message<?> message) {
|
||||
for (MessageSelector next : this.selectors) {
|
||||
if (!next.accept(message)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user