Skip to content

Commit 82ecd5a

Browse files
artembilangaryrussell
authored andcommitted
GH-2749: Deprecate ChannelInterceptorAware (#2751)
* GH-2749: Deprecate ChannelInterceptorAware Fixes #2749 The `org.springframework.messaging.support.InterceptableChannel` provides exact functionality as `ChannelInterceptorAware` * Make `ChannelInterceptorAware extends InterceptableChannel` * Suppress deprecation warning whenever we need to keep backward compatibility * Fix all other places to deal with `InterceptableChannel` already * GH-2749: Deprecate ChannelInterceptorAware Fixes #2749 The `org.springframework.messaging.support.InterceptableChannel` provides exact functionality as `ChannelInterceptorAware` * Make `ChannelInterceptorAware extends InterceptableChannel` * Suppress deprecation warning whenever we need to keep backward compatibility * Fix all other places to deal with `InterceptableChannel` already * * Fix `channel.adoc` for the current version
1 parent d6ac866 commit 82ecd5a

File tree

20 files changed

+174
-208
lines changed

20 files changed

+174
-208
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/channel/PollableAmqpChannel.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -178,7 +178,7 @@ public Message<?> receive(long timeout) {
178178

179179

180180
protected Message<?> doReceive(Long timeout) {
181-
ChannelInterceptorList interceptorList = getInterceptors();
181+
ChannelInterceptorList interceptorList = getIChannelInterceptorList();
182182
Deque<ChannelInterceptor> interceptorStack = null;
183183
boolean counted = false;
184184
boolean countsEnabled = isCountsEnabled();

spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Artem Bilan
6767
*/
6868
@IntegrationManagedResource
69+
@SuppressWarnings("deprecation")
6970
public abstract class AbstractMessageChannel extends IntegrationObjectSupport
7071
implements MessageChannel, TrackableComponent, ChannelInterceptorAware, MessageChannelMetrics,
7172
ConfigurableMetricsAware<AbstractMessageChannelMetrics> {
@@ -243,7 +244,7 @@ public void setMessageConverter(MessageConverter messageConverter) {
243244
* Return a read-only list of the configured interceptors.
244245
*/
245246
@Override
246-
public List<ChannelInterceptor> getChannelInterceptors() {
247+
public List<ChannelInterceptor> getInterceptors() {
247248
return this.interceptors.getInterceptors();
248249
}
249250

@@ -259,10 +260,10 @@ public ChannelInterceptor removeInterceptor(int index) {
259260
}
260261

261262
/**
262-
* Exposes the interceptor list for subclasses.
263+
* Exposes the interceptor list instance for subclasses.
263264
* @return The channel interceptor list.
264265
*/
265-
protected ChannelInterceptorList getInterceptors() {
266+
protected ChannelInterceptorList getIChannelInterceptorList() {
266267
return this.interceptors;
267268
}
268269

spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Message<?> receive() {
9292
@Override
9393
@Nullable
9494
public Message<?> receive(long timeout) {
95-
ChannelInterceptorList interceptorList = getInterceptors();
95+
ChannelInterceptorList interceptorList = getIChannelInterceptorList();
9696
Deque<ChannelInterceptor> interceptorStack = null;
9797
boolean counted = false;
9898
boolean countsEnabled = isCountsEnabled();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,8 @@
1818

1919
import java.util.List;
2020

21-
import org.springframework.lang.Nullable;
2221
import org.springframework.messaging.support.ChannelInterceptor;
22+
import org.springframework.messaging.support.InterceptableChannel;
2323

2424
/**
2525
* A marker interface providing the ability to configure {@link ChannelInterceptor}s
@@ -30,48 +30,21 @@
3030
* *
3131
* @author Artem Bilan
3232
* @author Gary Russell
33+
*
3334
* @since 4.0
35+
*
36+
* @deprecated since 5.2 in favor of {@link InterceptableChannel}.
37+
* Will be removed in the next 5.3 version.
3438
*/
35-
public interface ChannelInterceptorAware {
36-
37-
/**
38-
* Populate the {@link ChannelInterceptor}s to the target implementation.
39-
* @param interceptors the {@link ChannelInterceptor}s to populate.
40-
*/
41-
void setInterceptors(List<ChannelInterceptor> interceptors);
42-
43-
/**
44-
* And a {@link ChannelInterceptor} to the target implementation.
45-
* @param interceptor the {@link ChannelInterceptor} to add.
46-
*/
47-
void addInterceptor(ChannelInterceptor interceptor);
48-
49-
/**
50-
* And a {@link ChannelInterceptor} to the target implementation for the specific index.
51-
* @param index the index for {@link ChannelInterceptor} to add.
52-
* @param interceptor the {@link ChannelInterceptor} to add.
53-
*/
54-
void addInterceptor(int index, ChannelInterceptor interceptor);
39+
@Deprecated
40+
public interface ChannelInterceptorAware extends InterceptableChannel {
5541

5642
/**
5743
* return the {@link ChannelInterceptor} list.
5844
* @return the {@link ChannelInterceptor} list.
5945
*/
60-
List<ChannelInterceptor> getChannelInterceptors();
61-
62-
/**
63-
* Remove the provided {@link ChannelInterceptor} from the target implementation.
64-
* @param interceptor {@link ChannelInterceptor} to remove.
65-
* @return the {@code boolean} if {@link ChannelInterceptor} has been removed.
66-
*/
67-
boolean removeInterceptor(ChannelInterceptor interceptor);
68-
69-
/**
70-
* Remove a {@link ChannelInterceptor} from the target implementation for specific index.
71-
* @param index the index for the {@link org.springframework.messaging.support.ChannelInterceptor} to remove.
72-
* @return the {@code boolean} if the {@link ChannelInterceptor} has been removed.
73-
*/
74-
@Nullable
75-
ChannelInterceptor removeInterceptor(int index);
46+
default List<ChannelInterceptor> getChannelInterceptors() {
47+
return getInterceptors();
48+
}
7649

7750
}

spring-integration-core/src/main/java/org/springframework/integration/channel/ExecutorChannelInterceptorAware.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,9 +23,12 @@
2323
* and the implementors require to know if they should make the
2424
* {@link org.springframework.messaging.support.ExecutorChannelInterceptor}
2525
* or not.
26+
*
2627
* @author Artem Bilan
28+
*
2729
* @since 4.2
2830
*/
31+
@SuppressWarnings("deprecation")
2932
public interface ExecutorChannelInterceptorAware extends ChannelInterceptorAware {
3033

3134
boolean hasExecutorInterceptors();

spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/VetoCapableInterceptor.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,17 @@
1616

1717
package org.springframework.integration.channel.interceptor;
1818

19-
import org.springframework.integration.channel.ChannelInterceptorAware;
19+
import org.springframework.messaging.support.InterceptableChannel;
2020

2121
/**
2222
* {@link org.springframework.messaging.support.ChannelInterceptor}s implementing this
23-
* interface can veto
24-
* global interception of a particular channel. Could be used, for example,
25-
* when an interceptor itself writes to an output channel (which should
26-
* not be intercepted with this interceptor).
23+
* interface can veto global interception of a particular channel.
24+
* Could be used, for example, when an interceptor itself writes to an output channel
25+
* (which should not be intercepted with this interceptor).
2726
*
2827
* @author Gary Russell
28+
* @author Artem Bilan
29+
*
2930
* @since 4.0
3031
*
3132
*/
@@ -36,6 +37,6 @@ public interface VetoCapableInterceptor {
3637
* @param channel The channel that is about to be intercepted.
3738
* @return false if the intercept wishes to veto the interception.
3839
*/
39-
boolean shouldIntercept(String beanName, ChannelInterceptorAware channel);
40+
boolean shouldIntercept(String beanName, InterceptableChannel channel);
4041

4142
}

spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/WireTap.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323
import org.springframework.beans.factory.BeanFactory;
2424
import org.springframework.beans.factory.BeanFactoryAware;
2525
import org.springframework.context.Lifecycle;
26-
import org.springframework.integration.channel.ChannelInterceptorAware;
2726
import org.springframework.integration.core.MessageSelector;
2827
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
2928
import org.springframework.jmx.export.annotation.ManagedAttribute;
@@ -32,6 +31,7 @@
3231
import org.springframework.messaging.Message;
3332
import org.springframework.messaging.MessageChannel;
3433
import org.springframework.messaging.support.ChannelInterceptor;
34+
import org.springframework.messaging.support.InterceptableChannel;
3535
import org.springframework.util.Assert;
3636

3737
/**
@@ -173,7 +173,8 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
173173
}
174174

175175
@Override
176-
public boolean shouldIntercept(String beanName, ChannelInterceptorAware channel) {
176+
public boolean shouldIntercept(String beanName, InterceptableChannel channel) {
177+
177178
return !getChannel().equals(channel);
178179
}
179180

spring-integration-core/src/main/java/org/springframework/integration/config/GlobalChannelInterceptorProcessor.java

+40-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,8 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collection;
21-
import java.util.Collections;
2221
import java.util.LinkedHashSet;
2322
import java.util.List;
24-
import java.util.Map;
25-
import java.util.Map.Entry;
2623
import java.util.Set;
2724

2825
import org.apache.commons.logging.Log;
@@ -35,11 +32,11 @@
3532
import org.springframework.beans.factory.SmartInitializingSingleton;
3633
import org.springframework.beans.factory.config.BeanPostProcessor;
3734
import org.springframework.core.OrderComparator;
38-
import org.springframework.integration.channel.ChannelInterceptorAware;
3935
import org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper;
4036
import org.springframework.integration.channel.interceptor.VetoCapableInterceptor;
4137
import org.springframework.integration.support.utils.PatternMatchUtils;
4238
import org.springframework.messaging.support.ChannelInterceptor;
39+
import org.springframework.messaging.support.InterceptableChannel;
4340
import org.springframework.util.Assert;
4441
import org.springframework.util.CollectionUtils;
4542
import org.springframework.util.StringUtils;
@@ -79,36 +76,34 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
7976
}
8077

8178
@Override
79+
@SuppressWarnings("deprecation")
8280
public void afterSingletonsInstantiated() {
8381
Collection<GlobalChannelInterceptorWrapper> interceptors =
8482
this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class).values();
8583
if (CollectionUtils.isEmpty(interceptors)) {
8684
logger.debug("No global channel interceptors.");
8785
}
8886
else {
89-
for (GlobalChannelInterceptorWrapper channelInterceptor : interceptors) {
90-
if (channelInterceptor.getOrder() >= 0) {
91-
this.positiveOrderInterceptors.add(channelInterceptor);
87+
interceptors.forEach(interceptor -> {
88+
if (interceptor.getOrder() >= 0) {
89+
this.positiveOrderInterceptors.add(interceptor);
9290
}
9391
else {
94-
this.negativeOrderInterceptors.add(channelInterceptor);
92+
this.negativeOrderInterceptors.add(interceptor);
9593
}
96-
}
94+
});
9795

98-
Map<String, ChannelInterceptorAware> channels =
99-
this.beanFactory.getBeansOfType(ChannelInterceptorAware.class);
100-
for (Entry<String, ChannelInterceptorAware> entry : channels.entrySet()) {
101-
addMatchingInterceptors(entry.getValue(), entry.getKey());
102-
}
96+
this.beanFactory.getBeansOfType(InterceptableChannel.class)
97+
.forEach((key, value) -> addMatchingInterceptors(value, key));
10398
}
10499

105100
this.singletonsInstantiated = true;
106101
}
107102

108103
@Override
109104
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
110-
if (this.singletonsInstantiated && bean instanceof ChannelInterceptorAware) {
111-
addMatchingInterceptors((ChannelInterceptorAware) bean, beanName);
105+
if (this.singletonsInstantiated && bean instanceof InterceptableChannel) {
106+
addMatchingInterceptors((InterceptableChannel) bean, beanName);
112107
}
113108
return bean;
114109
}
@@ -118,41 +113,34 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
118113
* @param channel the message channel to add interceptors.
119114
* @param beanName the message channel bean name to match the pattern.
120115
*/
121-
public void addMatchingInterceptors(ChannelInterceptorAware channel, String beanName) {
116+
public void addMatchingInterceptors(InterceptableChannel channel, String beanName) {
117+
122118
if (logger.isDebugEnabled()) {
123119
logger.debug("Applying global interceptors on channel '" + beanName + "'");
124120
}
125121

126122
List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<>();
127-
for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.positiveOrderInterceptors) {
128-
String[] patterns = globalChannelInterceptorWrapper.getPatterns();
129-
patterns = StringUtils.trimArrayElements(patterns);
130-
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
131-
tempInterceptors.add(globalChannelInterceptorWrapper);
132-
}
133-
}
134123

135-
Collections.sort(tempInterceptors, this.comparator);
124+
this.positiveOrderInterceptors
125+
.forEach(interceptorWrapper ->
126+
addMatchingInterceptors(beanName, tempInterceptors, interceptorWrapper));
136127

137-
for (GlobalChannelInterceptorWrapper next : tempInterceptors) {
138-
ChannelInterceptor channelInterceptor = next.getChannelInterceptor();
139-
if (!(channelInterceptor instanceof VetoCapableInterceptor)
140-
|| ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
141-
channel.addInterceptor(channelInterceptor);
142-
}
143-
}
128+
tempInterceptors.sort(this.comparator);
129+
130+
tempInterceptors
131+
.stream()
132+
.map(GlobalChannelInterceptorWrapper::getChannelInterceptor)
133+
.filter(interceptor -> !(interceptor instanceof VetoCapableInterceptor)
134+
|| ((VetoCapableInterceptor) interceptor).shouldIntercept(beanName, channel))
135+
.forEach(channel::addInterceptor);
144136

145137
tempInterceptors.clear();
146138

147-
for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.negativeOrderInterceptors) {
148-
String[] patterns = globalChannelInterceptorWrapper.getPatterns();
149-
patterns = StringUtils.trimArrayElements(patterns);
150-
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
151-
tempInterceptors.add(globalChannelInterceptorWrapper);
152-
}
153-
}
139+
this.negativeOrderInterceptors
140+
.forEach(interceptorWrapper ->
141+
addMatchingInterceptors(beanName, tempInterceptors, interceptorWrapper));
154142

155-
Collections.sort(tempInterceptors, this.comparator);
143+
tempInterceptors.sort(this.comparator);
156144

157145
if (!tempInterceptors.isEmpty()) {
158146
for (int i = tempInterceptors.size() - 1; i >= 0; i--) {
@@ -165,4 +153,15 @@ public void addMatchingInterceptors(ChannelInterceptorAware channel, String bean
165153
}
166154
}
167155

156+
private static void addMatchingInterceptors(String beanName,
157+
List<GlobalChannelInterceptorWrapper> tempInterceptors,
158+
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper) {
159+
160+
String[] patterns = globalChannelInterceptorWrapper.getPatterns();
161+
patterns = StringUtils.trimArrayElements(patterns);
162+
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
163+
tempInterceptors.add(globalChannelInterceptorWrapper);
164+
}
165+
}
166+
168167
}

0 commit comments

Comments
 (0)