Skip to content

GH-2749: Deprecate ChannelInterceptorAware #2751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -178,7 +178,7 @@ public Message<?> receive(long timeout) {


protected Message<?> doReceive(Long timeout) {
ChannelInterceptorList interceptorList = getInterceptors();
ChannelInterceptorList interceptorList = getIChannelInterceptorList();
Deque<ChannelInterceptor> interceptorStack = null;
boolean counted = false;
boolean countsEnabled = isCountsEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Artem Bilan
*/
@IntegrationManagedResource
@SuppressWarnings("deprecation")
public abstract class AbstractMessageChannel extends IntegrationObjectSupport
implements MessageChannel, TrackableComponent, ChannelInterceptorAware, MessageChannelMetrics,
ConfigurableMetricsAware<AbstractMessageChannelMetrics> {
Expand Down Expand Up @@ -243,7 +244,7 @@ public void setMessageConverter(MessageConverter messageConverter) {
* Return a read-only list of the configured interceptors.
*/
@Override
public List<ChannelInterceptor> getChannelInterceptors() {
public List<ChannelInterceptor> getInterceptors() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do this; breaking change. Need to deprecate the old method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it clashes with the InterceptableChannel API. So, we in chicken-or-egg situation anyway, independently of the version we are going to fix it. Good deal that this method is just protected, so visible only for implementors, but not regular end-user code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved it to 5.1.4; let's talk when we have more time.

return this.interceptors.getInterceptors();
}

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

/**
* Exposes the interceptor list for subclasses.
* Exposes the interceptor list instance for subclasses.
* @return The channel interceptor list.
*/
protected ChannelInterceptorList getInterceptors() {
protected ChannelInterceptorList getIChannelInterceptorList() {
return this.interceptors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Message<?> receive() {
@Override
@Nullable
public Message<?> receive(long timeout) {
ChannelInterceptorList interceptorList = getInterceptors();
ChannelInterceptorList interceptorList = getIChannelInterceptorList();
Deque<ChannelInterceptor> interceptorStack = null;
boolean counted = false;
boolean countsEnabled = isCountsEnabled();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 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.
Expand All @@ -18,8 +18,8 @@

import java.util.List;

import org.springframework.lang.Nullable;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.InterceptableChannel;

/**
* A marker interface providing the ability to configure {@link ChannelInterceptor}s
Expand All @@ -30,48 +30,21 @@
* *
* @author Artem Bilan
* @author Gary Russell
*
* @since 4.0
*
* @deprecated since 5.2 in favor of {@link InterceptableChannel}.
* Will be removed in the next 5.3 version.
*/
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);
@Deprecated
public interface ChannelInterceptorAware extends InterceptableChannel {

/**
* 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.
*/
@Nullable
ChannelInterceptor removeInterceptor(int index);
default List<ChannelInterceptor> getChannelInterceptors() {
return getInterceptors();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2019 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.
Expand All @@ -23,9 +23,12 @@
* and the implementors require to know if they should make the
* {@link org.springframework.messaging.support.ExecutorChannelInterceptor}
* or not.
*
* @author Artem Bilan
*
* @since 4.2
*/
@SuppressWarnings("deprecation")
public interface ExecutorChannelInterceptorAware extends ChannelInterceptorAware {

boolean hasExecutorInterceptors();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 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.
Expand All @@ -16,16 +16,17 @@

package org.springframework.integration.channel.interceptor;

import org.springframework.integration.channel.ChannelInterceptorAware;
import org.springframework.messaging.support.InterceptableChannel;

/**
* {@link org.springframework.messaging.support.ChannelInterceptor}s implementing this
* interface can veto
* global interception of a particular channel. Could be used, for example,
* when an interceptor itself writes to an output channel (which should
* not be intercepted with this interceptor).
* interface can veto global interception of a particular channel.
* Could be used, for example, when an interceptor itself writes to an output channel
* (which should not be intercepted with this interceptor).
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.0
*
*/
Expand All @@ -36,6 +37,6 @@ public interface VetoCapableInterceptor {
* @param channel The channel that is about to be intercepted.
* @return false if the intercept wishes to veto the interception.
*/
boolean shouldIntercept(String beanName, ChannelInterceptorAware channel);
boolean shouldIntercept(String beanName, InterceptableChannel channel);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
Expand All @@ -23,7 +23,6 @@
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.Lifecycle;
import org.springframework.integration.channel.ChannelInterceptorAware;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
import org.springframework.jmx.export.annotation.ManagedAttribute;
Expand All @@ -32,6 +31,7 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.InterceptableChannel;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -173,7 +173,8 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
}

@Override
public boolean shouldIntercept(String beanName, ChannelInterceptorAware channel) {
public boolean shouldIntercept(String beanName, InterceptableChannel channel) {

return !getChannel().equals(channel);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
Expand All @@ -18,11 +18,8 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand All @@ -35,11 +32,11 @@
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.OrderComparator;
import org.springframework.integration.channel.ChannelInterceptorAware;
import org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper;
import org.springframework.integration.channel.interceptor.VetoCapableInterceptor;
import org.springframework.integration.support.utils.PatternMatchUtils;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.InterceptableChannel;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -79,36 +76,34 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
}

@Override
@SuppressWarnings("deprecation")
public void afterSingletonsInstantiated() {
Collection<GlobalChannelInterceptorWrapper> interceptors =
this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class).values();
if (CollectionUtils.isEmpty(interceptors)) {
logger.debug("No global channel interceptors.");
}
else {
for (GlobalChannelInterceptorWrapper channelInterceptor : interceptors) {
if (channelInterceptor.getOrder() >= 0) {
this.positiveOrderInterceptors.add(channelInterceptor);
interceptors.forEach(interceptor -> {
if (interceptor.getOrder() >= 0) {
this.positiveOrderInterceptors.add(interceptor);
}
else {
this.negativeOrderInterceptors.add(channelInterceptor);
this.negativeOrderInterceptors.add(interceptor);
}
}
});

Map<String, ChannelInterceptorAware> channels =
this.beanFactory.getBeansOfType(ChannelInterceptorAware.class);
for (Entry<String, ChannelInterceptorAware> entry : channels.entrySet()) {
addMatchingInterceptors(entry.getValue(), entry.getKey());
}
this.beanFactory.getBeansOfType(InterceptableChannel.class)
.forEach((key, value) -> addMatchingInterceptors(value, key));
}

this.singletonsInstantiated = true;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (this.singletonsInstantiated && bean instanceof ChannelInterceptorAware) {
addMatchingInterceptors((ChannelInterceptorAware) bean, beanName);
if (this.singletonsInstantiated && bean instanceof InterceptableChannel) {
addMatchingInterceptors((InterceptableChannel) bean, beanName);
}
return bean;
}
Expand All @@ -118,41 +113,34 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
* @param channel the message channel to add interceptors.
* @param beanName the message channel bean name to match the pattern.
*/
public void addMatchingInterceptors(ChannelInterceptorAware channel, String beanName) {
public void addMatchingInterceptors(InterceptableChannel channel, String beanName) {

if (logger.isDebugEnabled()) {
logger.debug("Applying global interceptors on channel '" + beanName + "'");
}

List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<>();
for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.positiveOrderInterceptors) {
String[] patterns = globalChannelInterceptorWrapper.getPatterns();
patterns = StringUtils.trimArrayElements(patterns);
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
tempInterceptors.add(globalChannelInterceptorWrapper);
}
}

Collections.sort(tempInterceptors, this.comparator);
this.positiveOrderInterceptors
.forEach(interceptorWrapper ->
addMatchingInterceptors(beanName, tempInterceptors, interceptorWrapper));

for (GlobalChannelInterceptorWrapper next : tempInterceptors) {
ChannelInterceptor channelInterceptor = next.getChannelInterceptor();
if (!(channelInterceptor instanceof VetoCapableInterceptor)
|| ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
channel.addInterceptor(channelInterceptor);
}
}
tempInterceptors.sort(this.comparator);

tempInterceptors
.stream()
.map(GlobalChannelInterceptorWrapper::getChannelInterceptor)
.filter(interceptor -> !(interceptor instanceof VetoCapableInterceptor)
|| ((VetoCapableInterceptor) interceptor).shouldIntercept(beanName, channel))
.forEach(channel::addInterceptor);

tempInterceptors.clear();

for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.negativeOrderInterceptors) {
String[] patterns = globalChannelInterceptorWrapper.getPatterns();
patterns = StringUtils.trimArrayElements(patterns);
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
tempInterceptors.add(globalChannelInterceptorWrapper);
}
}
this.negativeOrderInterceptors
.forEach(interceptorWrapper ->
addMatchingInterceptors(beanName, tempInterceptors, interceptorWrapper));

Collections.sort(tempInterceptors, this.comparator);
tempInterceptors.sort(this.comparator);

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

private static void addMatchingInterceptors(String beanName,
List<GlobalChannelInterceptorWrapper> tempInterceptors,
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper) {

String[] patterns = globalChannelInterceptorWrapper.getPatterns();
patterns = StringUtils.trimArrayElements(patterns);
if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
tempInterceptors.add(globalChannelInterceptorWrapper);
}
}

}
Loading