From 507fd42d01f3172f47e92d6639a415f7af56552e Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 1 Aug 2014 14:40:42 +0300 Subject: [PATCH] INT-3422: Add `ChannelInterceptorAware.remove()` JIRA: https://jira.spring.io/browse/INT-3422 * Add two methods: ``` boolean removeInterceptor(ChannelInterceptor interceptor); boolean removeInterceptor(int index); ``` The method `removeInterceptorsOfType(Class clazz)` isn't good, because we lead undesired behavior, when several provided interceptors might be of the same type (e.g. by superclass) INT-3422: PR comments --- .../channel/AbstractMessageChannel.java | 20 ++++++++++++ .../channel/ChannelInterceptorAware.java | 31 +++++++++++++++++++ .../interceptor/ChannelInterceptorTests.java | 22 ++++++++++--- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java index 9e6aa5ae66..222f830dbb 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java @@ -167,6 +167,16 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport return this.interceptors.getInterceptors(); } + @Override + public boolean removeInterceptor(ChannelInterceptor interceptor) { + return this.interceptors.remove(interceptor); + } + + @Override + public ChannelInterceptor removeInterceptor(int index) { + return this.interceptors.remove(index); + } + /** * Exposes the interceptor list for subclasses. * @@ -390,5 +400,15 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport public List getInterceptors() { return Collections.unmodifiableList(this.interceptors); } + + public boolean remove(ChannelInterceptor interceptor) { + return this.interceptors.remove(interceptor); + } + + public ChannelInterceptor remove(int index) { + return this.interceptors.remove(index); + } + } + } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/ChannelInterceptorAware.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/ChannelInterceptorAware.java index 1273fb9fd3..2bab05015c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/ChannelInterceptorAware.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/ChannelInterceptorAware.java @@ -32,12 +32,43 @@ import org.springframework.messaging.support.ChannelInterceptor; */ public interface ChannelInterceptorAware { + /** + * Populate the {@link ChannelInterceptor}s to the target implementation. + * @param interceptors the {@link ChannelInterceptor}s to populate. + */ void setInterceptors(List interceptors); + /** + * And a {@link ChannelInterceptor} to the target implementation. + * @param interceptor the {@link ChannelInterceptor} to add. + */ void addInterceptor(ChannelInterceptor interceptor); + /** + * And a {@link ChannelInterceptor} to the target implementation for the specific index. + * @param index the index for {@link ChannelInterceptor} to add. + * @param interceptor the {@link ChannelInterceptor} to add. + */ void addInterceptor(int index, ChannelInterceptor interceptor); + /** + * return the {@link ChannelInterceptor} list. + * @return the {@link ChannelInterceptor} list. + */ List getChannelInterceptors(); + /** + * Remove the provided {@link ChannelInterceptor} from the target implementation. + * @param interceptor {@link ChannelInterceptor} to remove. + * @return the {@code boolean} if {@link ChannelInterceptor} has been removed. + */ + boolean removeInterceptor(ChannelInterceptor interceptor); + + /** + * Remove a {@link ChannelInterceptor} from the target implementation for specific index. + * @param index the index for the {@link org.springframework.messaging.support.ChannelInterceptor} to remove. + * @return the {@code boolean} if the {@link ChannelInterceptor} has been removed. + */ + ChannelInterceptor removeInterceptor(int index); + } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/ChannelInterceptorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/ChannelInterceptorTests.java index 3afce01490..765db5721c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/ChannelInterceptorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/ChannelInterceptorTests.java @@ -30,7 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.Matchers; import org.junit.Test; -import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.AbstractMessageChannel; import org.springframework.integration.channel.ChannelInterceptorAware; @@ -70,8 +70,15 @@ public class ChannelInterceptorTests { Message message = new GenericMessage("test"); channel.send(message); assertEquals(1, interceptor.getCount()); + + assertTrue(channel.removeInterceptor(interceptor)); + + channel.send(new GenericMessage("TEST")); + assertEquals(1, interceptor.getCount()); + Message result = channel.receive(0); - assertNull(result); + assertNotNull(result); + assertEquals("TEST", result.getPayload()); } @Test @@ -116,6 +123,11 @@ public class ChannelInterceptorTests { singleItemChannel.send(new GenericMessage("test2"), 0); assertEquals(2, invokedCounter.get()); assertEquals(1, sentCounter.get()); + + assertNotNull(singleItemChannel.removeInterceptor(0)); + singleItemChannel.send(new GenericMessage("test2"), 0); + assertEquals(2, invokedCounter.get()); + assertEquals(1, sentCounter.get()); } @Test @@ -164,8 +176,9 @@ public class ChannelInterceptorTests { assertEquals(1, messageCount.get()); } @Test - public void testInterceptorBeanWithPnamespace(){ - ApplicationContext ac = new ClassPathXmlApplicationContext("ChannelInterceptorTests-context.xml", ChannelInterceptorTests.class); + public void testInterceptorBeanWithPNamespace(){ + ConfigurableApplicationContext ac = + new ClassPathXmlApplicationContext("ChannelInterceptorTests-context.xml", ChannelInterceptorTests.class); ChannelInterceptorAware channel = ac.getBean("input", AbstractMessageChannel.class); List interceptors = channel.getChannelInterceptors(); ChannelInterceptor channelInterceptor = interceptors.get(0); @@ -173,6 +186,7 @@ public class ChannelInterceptorTests { String foo = ((PreSendReturnsMessageInterceptor) channelInterceptor).getFoo(); assertTrue(StringUtils.hasText(foo)); assertEquals("foo", foo); + ac.close(); }