Skip to content

Commit

Permalink
INT-3749: Get rid of IECA logic
Browse files Browse the repository at this point in the history
JIRA: https://jira.spring.io/browse/INT-3749

Since `IntegrationEvaluationContextAware` isn't so "context-free" resource like `BeanFactory`
 and `ApplicationContext`, but just a specific bean in the context, we can't follow with `BeanPostProcessor` logic - bad architecture by level of responsibility.
 Therefore we should follow with standard Dependency Injection mechanism to retrieve `evaluationContext` for the particular component.

 Since we can't rely on the `@Autowired` because SI can be used from the raw XML configuration,
 we use utility method instead. The pattern to get the proper `integrationEvaluationContext` is:
 ```
 @OverRide
 protected void onInit() throws Exception {
 		super.onInit();
 		this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
 }
 ```
  • Loading branch information
artembilan authored and garyrussell committed Jun 24, 2015
1 parent fe98cbc commit 3392d4e
Show file tree
Hide file tree
Showing 26 changed files with 271 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.locks.Lock;

import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
Expand All @@ -38,7 +34,7 @@
import org.springframework.expression.Expression;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.expression.IntegrationEvaluationContextAware;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.store.MessageGroup;
import org.springframework.integration.store.MessageGroupStore;
Expand All @@ -56,6 +52,10 @@
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Abstract Message handler that holds a buffer of correlated messages in a
* {@link MessageStore}. This class takes care of correlated groups of messages
Expand All @@ -82,7 +82,7 @@
* @since 2.0
*/
public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageProducingHandler
implements DisposableBean, IntegrationEvaluationContextAware, ApplicationEventPublisherAware {
implements DisposableBean, ApplicationEventPublisherAware {

private static final Log logger = LogFactory.getLog(AbstractCorrelatingMessageHandler.class);

Expand Down Expand Up @@ -185,11 +185,6 @@ public void setForceReleaseAdviceChain(List<Advice> forceReleaseAdviceChain) {
this.forceReleaseAdviceChain = forceReleaseAdviceChain;
}

@Override
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
}

@Override
public void setTaskScheduler(TaskScheduler taskScheduler) {
super.setTaskScheduler(taskScheduler);
Expand Down Expand Up @@ -229,6 +224,8 @@ protected void onInit() throws Exception {
((SequenceSizeReleaseStrategy) this.releaseStrategy).setReleasePartialSequences(releasePartialSequences);
}

this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());

/*
* Disallow any further changes to the lock registry
* (checked in the setter).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -47,12 +44,14 @@
import org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.context.IntegrationProperties;
import org.springframework.integration.expression.IntegrationEvaluationContextAwareBeanPostProcessor;
import org.springframework.integration.support.DefaultMessageBuilderFactory;
import org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.util.ClassUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* {@link ImportBeanDefinitionRegistrar} implementation that configures integration infrastructure.
*
Expand Down Expand Up @@ -87,9 +86,11 @@ public void setBeanClassLoader(ClassLoader classLoader) {
* to register the messaging annotation post processors (for {@code <int:annotation-config/>}).
*/
@Override
@SuppressWarnings("deprecation")
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
this.registerImplicitChannelCreator(registry);
this.registerIntegrationConfigurationBeanFactoryPostProcessor(registry);
//TODO remove this line in the 4.3
this.registerIntegrationEvaluationContext(registry);
this.registerIntegrationProperties(registry);
this.registerHeaderChannelRegistry(registry);
Expand Down Expand Up @@ -174,9 +175,14 @@ private void registerIntegrationProperties(BeanDefinitionRegistry registry) {

/**
* Register {@link IntegrationEvaluationContextFactoryBean} bean
* and {@link IntegrationEvaluationContextAwareBeanPostProcessor}, if necessary.
* and {@code IntegrationEvaluationContextAwareBeanPostProcessor}, if necessary.
* @param registry The {@link BeanDefinitionRegistry} to register additional {@link BeanDefinition}s.
* @deprecated since 4.2 in favor of {@link IntegrationContextUtils#getEvaluationContext}
* direct usage from the {@code afterPropertiesSet} implementation.
* Will be removed in the next release.
*/
@Deprecated
@SuppressWarnings("deprecation")
private void registerIntegrationEvaluationContext(BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)) {
BeanDefinitionBuilder integrationEvaluationContextBuilder = BeanDefinitionBuilder
Expand All @@ -191,7 +197,7 @@ private void registerIntegrationEvaluationContext(BeanDefinitionRegistry registr
registry);

RootBeanDefinition integrationEvalContextBPP =
new RootBeanDefinition(IntegrationEvaluationContextAwareBeanPostProcessor.class);
new RootBeanDefinition(org.springframework.integration.expression.IntegrationEvaluationContextAwareBeanPostProcessor.class);
BeanDefinitionReaderUtils.registerWithGeneratedName(integrationEvalContextBPP, registry);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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
Expand All @@ -10,13 +10,13 @@
* 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.endpoint;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.expression.IntegrationEvaluationContextAware;
import org.springframework.util.Assert;

/**
Expand All @@ -29,7 +29,7 @@
* @since 2.1
*
*/
public abstract class ExpressionMessageProducerSupport extends MessageProducerSupport implements IntegrationEvaluationContextAware {
public abstract class ExpressionMessageProducerSupport extends MessageProducerSupport {

private static final SpelExpressionParser PARSER = new SpelExpressionParser();

Expand All @@ -56,17 +56,10 @@ public void setExpressionPayload(Expression payloadExpression) {
this.payloadExpression = payloadExpression;
}

@Override
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
}

@Override
protected void onInit() {
super.onInit();
if (this.evaluationContext == null) {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
}
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
}

protected Object evaluatePayloadExpression(Object payload){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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,9 +16,6 @@

package org.springframework.integration.expression;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.MapAccessor;
Expand All @@ -28,6 +25,9 @@
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.support.utils.IntegrationUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Utility class with static methods for helping with establishing environments for
* SpEL expressions.
Expand Down Expand Up @@ -76,7 +76,7 @@ public static StandardEvaluationContext createStandardEvaluationContext() {
*/
public static StandardEvaluationContext createStandardEvaluationContext(BeanFactory beanFactory) {
if (beanFactory == null) {
logger.warn("Creating EvaluationContext with no beanFactory", new RuntimeException("No beanfactory"));
logger.warn("Creating EvaluationContext with no beanFactory", new RuntimeException("No beanFactory"));
}
return doCreateContext(beanFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package org.springframework.integration.expression;

import org.springframework.expression.EvaluationContext;
import org.springframework.integration.context.IntegrationContextUtils;

/**
* Interface to be implemented by beans that wish to be aware of their
* owning integration {@link EvaluationContext}, which is the result of
* {@link org.springframework.integration.config.IntegrationEvaluationContextFactoryBean}
* <p>
* The {@link #setIntegrationEvaluationContext} is invoked from
* the {@link IntegrationEvaluationContextAwareBeanPostProcessor#afterSingletonsInstantiated()},
* the {@code IntegrationEvaluationContextAwareBeanPostProcessor#afterSingletonsInstantiated()},
* not during standard {@code postProcessBefore(After)Initialization} to avoid any
* {@code BeanFactory} early access during integration {@link EvaluationContext} retrieval.
* Therefore, if it is necessary to use {@link EvaluationContext} in the {@code afterPropertiesSet()},
Expand All @@ -33,8 +34,11 @@
*
* @author Artem Bilan
* @since 3.0
* @see IntegrationEvaluationContextAwareBeanPostProcessor
* @deprecated since 4.2 in favor of {@link IntegrationContextUtils#getEvaluationContext}
* direct usage from the {@code afterPropertiesSet} implementation.
* Will be removed in the next release.
*/
@Deprecated
public interface IntegrationEvaluationContextAware {

void setIntegrationEvaluationContext(EvaluationContext evaluationContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
* @author Artem Bilan
* @author Gary Russell
* @since 3.0
* @deprecated since 4.2 in favor of {@link IntegrationContextUtils#getEvaluationContext}
* direct usage from the {@code afterPropertiesSet} implementation.
* Will be removed in the next release.
*/
@Deprecated
@SuppressWarnings("deprecation")
public class IntegrationEvaluationContextAwareBeanPostProcessor
implements BeanPostProcessor, Ordered, BeanFactoryAware, SmartInitializingSingleton {

Expand Down Expand Up @@ -71,5 +76,5 @@ public void afterSingletonsInstantiated() {
public int getOrder() {
return LOWEST_PRECEDENCE;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 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,11 +16,15 @@

package org.springframework.integration.routingslip;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.expression.IntegrationEvaluationContextAware;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.messaging.Message;

/**
Expand Down Expand Up @@ -57,14 +61,16 @@
* @since 4.1
*/
public class ExpressionEvaluatingRoutingSlipRouteStrategy
implements RoutingSlipRouteStrategy, IntegrationEvaluationContextAware {
implements RoutingSlipRouteStrategy, BeanFactoryAware, InitializingBean {

private static final ExpressionParser PARSER = new SpelExpressionParser();

private final Expression expression;

private EvaluationContext evaluationContext;

private BeanFactory beanFactory;

public ExpressionEvaluatingRoutingSlipRouteStrategy(String expression) {
this(PARSER.parseExpression(expression));
}
Expand All @@ -74,8 +80,13 @@ public ExpressionEvaluatingRoutingSlipRouteStrategy(Expression expression) {
}

@Override
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

@Override
public void afterPropertiesSet() throws Exception {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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
Expand All @@ -24,7 +24,6 @@
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.expression.IntegrationEvaluationContextAware;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.transaction.support.TransactionSynchronization;
Expand Down Expand Up @@ -56,7 +55,7 @@
*
*/
public class ExpressionEvaluatingTransactionSynchronizationProcessor extends IntegrationObjectSupport
implements TransactionSynchronizationProcessor, IntegrationEvaluationContextAware {
implements TransactionSynchronizationProcessor {

private volatile EvaluationContext evaluationContext;

Expand All @@ -72,11 +71,6 @@ public class ExpressionEvaluatingTransactionSynchronizationProcessor extends Int

private volatile MessageChannel afterRollbackChannel = new NullChannel();

@Override
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
}

public void setBeforeCommitChannel(MessageChannel beforeCommitChannel) {
Assert.notNull(beforeCommitChannel, "'beforeCommitChannel' must not be null");
this.beforeCommitChannel = beforeCommitChannel;
Expand Down Expand Up @@ -107,6 +101,12 @@ public void setAfterRollbackExpression(Expression afterRollbackExpression) {
this.afterRollbackExpression = afterRollbackExpression;
}

@Override
protected void onInit() throws Exception {
super.onInit();
this.evaluationContext = createEvaluationContext();
}

public void processBeforeCommit(IntegrationResourceHolder holder) {
this.doProcess(holder, this.beforeCommitExpression, this.beforeCommitChannel, "beforeCommit");
}
Expand Down
Loading

0 comments on commit 3392d4e

Please sign in to comment.