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
This commit is contained in:
Artem Bilan
2014-08-01 14:40:42 +03:00
committed by Gary Russell
parent 463c185b38
commit 507fd42d01
3 changed files with 69 additions and 4 deletions

View File

@@ -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<ChannelInterceptor> 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);
}
}
}

View File

@@ -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<ChannelInterceptor> 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<ChannelInterceptor> 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);
}

View File

@@ -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<String>("test");
channel.send(message);
assertEquals(1, interceptor.getCount());
assertTrue(channel.removeInterceptor(interceptor));
channel.send(new GenericMessage<String>("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<String>("test2"), 0);
assertEquals(2, invokedCounter.get());
assertEquals(1, sentCounter.get());
assertNotNull(singleItemChannel.removeInterceptor(0));
singleItemChannel.send(new GenericMessage<String>("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<ChannelInterceptor> 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();
}