INT-3314: Fix BeanFactory Population for SAH

JIRA: https://jira.springsource.org/browse/INT-3314

Add to `ServiceActivatingHandler` `beanFactory` population code for the `MessageProcessor` delegate

INT-3314: Populate `BF` to `HValueMProcessor`s

Make `XPathExpressionEvaluatingHeaderValueMessageProcessor` as `public`

INT-3314: Populate `BF` to `HE.messageProcessor`

INT-3314: Add `BF` `NPE` protection

INT-3314 XPath Header Enricher Test

Add a test that verifies the `BeanFactoryTypeConverter` is used
(convert to a TimeZone object).
This commit is contained in:
Artem Bilan
2014-03-07 16:03:57 +02:00
committed by Gary Russell
parent 267fc5b3db
commit 7adc9537b6
12 changed files with 84 additions and 35 deletions

View File

@@ -111,7 +111,7 @@ public class MessageFilter extends AbstractReplyProducingPostProcessingMessageHa
if (this.selector instanceof AbstractMessageProcessingSelector) {
((AbstractMessageProcessingSelector) this.selector).setConversionService(this.getConversionService());
}
if (this.selector instanceof BeanFactoryAware) {
if (this.selector instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.selector).setBeanFactory(this.getBeanFactory());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -18,9 +18,10 @@ package org.springframework.integration.handler;
import java.lang.reflect.Method;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.integration.annotation.ServiceActivator;
/**
* @author Mark Fisher
@@ -59,6 +60,9 @@ public class ServiceActivatingHandler extends AbstractReplyProducingMessageHandl
if (processor instanceof AbstractMessageProcessor) {
((AbstractMessageProcessor<?>) this.processor).setConversionService(this.getConversionService());
}
if (this.processor instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.processor).setBeanFactory(this.getBeanFactory());
}
}
@Override

View File

@@ -49,7 +49,7 @@ class AbstractMessageProcessingRouter extends AbstractMappingMessageRouter {
if (this.messageProcessor instanceof AbstractMessageProcessor) {
((AbstractMessageProcessor<?>) this.messageProcessor).setConversionService(this.getConversionService());
}
if (this.messageProcessor instanceof BeanFactoryAware) {
if (this.messageProcessor instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.messageProcessor).setBeanFactory(this.getBeanFactory());
}
}

View File

@@ -48,7 +48,7 @@ abstract class AbstractMessageProcessingSplitter extends AbstractMessageSplitter
if (conversionService != null && this.messageProcessor instanceof AbstractMessageProcessor) {
((AbstractMessageProcessor<?>) this.messageProcessor).setConversionService(conversionService);
}
if (this.messageProcessor instanceof BeanFactoryAware) {
if (this.messageProcessor instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.messageProcessor).setBeanFactory(this.getBeanFactory());
}
}

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.Lifecycle;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
@@ -264,6 +265,14 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
targetContext.setBeanResolver(null);
this.targetEvaluationContext = targetContext;
if (this.getBeanFactory() != null) {
for (HeaderValueMessageProcessor<?> headerValueMessageProcessor : headerExpressions.values()) {
if (headerValueMessageProcessor instanceof BeanFactoryAware) {
((BeanFactoryAware) headerValueMessageProcessor).setBeanFactory(this.getBeanFactory());
}
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.context.IntegrationObjectSupport;
@@ -153,11 +154,17 @@ public class HeaderEnricher extends IntegrationObjectSupport implements Transfor
public void onInit() throws Exception {
boolean shouldOverwrite = this.defaultOverwrite;
for (HeaderValueMessageProcessor<?> processor : this.headersToAdd.values()) {
if (processor instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) processor).setBeanFactory(this.getBeanFactory());
}
Boolean processerOverwrite = processor.isOverwrite();
if (processerOverwrite != null) {
shouldOverwrite |= processerOverwrite;
}
}
if (this.messageProcessor != null && this.messageProcessor instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.messageProcessor).setBeanFactory(this.getBeanFactory());
}
if (!shouldOverwrite && !this.shouldSkipNulls) {
logger.warn(this.getComponentName()
+ " is configured to not overwrite existing headers. 'shouldSkipNulls = false' will have no effect");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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
@@ -113,9 +113,7 @@ public abstract class AbstractExpressionEvaluator implements BeanFactoryAware, I
else {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
}
if (this.typeConverter != null) {
this.evaluationContext.setTypeConverter(this.typeConverter);
}
this.evaluationContext.setTypeConverter(this.typeConverter);
}
return this.evaluationContext;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2014 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.
@@ -20,21 +20,27 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelResolver;
import org.springframework.integration.handler.ReplyRequiredException;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
/**
* @author Mark Fisher
* @author Marius Bogoevici
* @author Artem Bilan
*/
public class ServiceActivatorEndpointTests {
@@ -200,6 +206,17 @@ public class ServiceActivatorEndpointTests {
assertEquals("ABC-123", correlationId);
}
@Test
public void testBeanFactoryPopulation() {
ServiceActivatingHandler endpoint = this.createEndpoint();
BeanFactory mock = Mockito.mock(BeanFactory.class);
endpoint.setBeanFactory(mock);
endpoint.afterPropertiesSet();
Object beanFactory = TestUtils.getPropertyValue(endpoint, "processor.beanFactory");
assertNotNull(beanFactory);
assertSame(mock, beanFactory);
}
private ServiceActivatingHandler createEndpoint() {
return new ServiceActivatingHandler(new TestBean(), "handle");