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");

View File

@@ -16,14 +16,6 @@
package org.springframework.integration.groovy;
import groovy.lang.Binding;
import groovy.lang.GString;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -41,6 +33,14 @@ import org.springframework.scripting.groovy.GroovyObjectCustomizer;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import groovy.lang.Binding;
import groovy.lang.GString;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
/**
* The {@link org.springframework.integration.handler.MessageProcessor} implementation
* to evaluate Groovy scripts.
@@ -97,14 +97,14 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
super.setBeanFactory(beanFactory);
if (beanFactory instanceof ConfigurableListableBeanFactory) {
if (beanFactory != null && beanFactory instanceof ConfigurableListableBeanFactory) {
((ConfigurableListableBeanFactory) beanFactory).ignoreDependencyType(MetaClass.class);
}
}
/**
* Sets a {@link GroovyObjectCustomizer} for this processor.
*
*
* @param customizer The customizer.
*/
public void setCustomizer(GroovyObjectCustomizer customizer) {

View File

@@ -23,7 +23,6 @@ import org.w3c.dom.NodeList;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.IntegrationConfigUtils;
import org.springframework.integration.config.xml.AbstractTransformerParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.xml.transformer.XPathHeaderEnricher;
@@ -61,8 +60,8 @@ public class XPathHeaderEnricherParser extends AbstractTransformerParser {
Element headerElement = (Element) node;
String elementName = node.getLocalName();
if ("header".equals(elementName)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationConfigUtils.BASE_PACKAGE
+ ".xml.transformer.XPathHeaderEnricher$XPathExpressionEvaluatingHeaderValueMessageProcessor");
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(XPathHeaderEnricher.XPathExpressionEvaluatingHeaderValueMessageProcessor.class);
String expressionString = headerElement.getAttribute("xpath-expression");
String expressionRef = headerElement.getAttribute("xpath-expression-ref");
boolean isExpressionString = StringUtils.hasText(expressionString);

View File

@@ -25,11 +25,10 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.transformer.HeaderEnricher;
import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
import org.springframework.integration.util.BeanFactoryTypeConverter;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.integration.xml.xpath.XPathEvaluationType;
@@ -61,15 +60,15 @@ public class XPathHeaderEnricher extends HeaderEnricher {
}
static class XPathExpressionEvaluatingHeaderValueMessageProcessor implements HeaderValueMessageProcessor<Object>,
public static class XPathExpressionEvaluatingHeaderValueMessageProcessor implements HeaderValueMessageProcessor<Object>,
BeanFactoryAware {
private TypeConverter typeConverter = new StandardTypeConverter();
private static final XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
private final BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
private final XPathExpression expression;
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
private volatile XPathEvaluationType evaluationType = XPathEvaluationType.STRING_RESULT;
private volatile TypeDescriptor headerTypeDescriptor;
@@ -109,7 +108,7 @@ public class XPathHeaderEnricher extends HeaderEnricher {
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
ConversionService conversionService = IntegrationContextUtils.getConversionService(beanFactory);
if (conversionService != null) {
this.typeConverter = new StandardTypeConverter(conversionService);
this.typeConverter.setConversionService(conversionService);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 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.
@@ -22,19 +22,20 @@ import static org.junit.Assert.assertNull;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xml.transformer.XPathHeaderEnricher;
import org.springframework.integration.xml.transformer.XPathHeaderEnricher.XPathExpressionEvaluatingHeaderValueMessageProcessor;
import org.springframework.integration.xml.xpath.XPathEvaluationType;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
/**
* @author Jonas Partner
* @author David Turanski
* @author Gary Russell
* @since 2.0
*/
public class XPathHeaderEnricherTests {
@@ -53,6 +54,21 @@ public class XPathHeaderEnricherTests {
assertEquals("Wrong value for element two expression", "2", headers.get("two"));
}
@Test
public void convertedEvaluation() {
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap =
new HashMap<String, XPathExpressionEvaluatingHeaderValueMessageProcessor>();
XPathExpressionEvaluatingHeaderValueMessageProcessor processor = new XPathExpressionEvaluatingHeaderValueMessageProcessor(
"/root/elementOne");
processor.setHeaderType(TimeZone.class);
expressionMap.put("one", processor);
String docAsString = "<root><elementOne>America/New_York</elementOne></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("Wrong value for element one expression", TimeZone.getTimeZone("America/New_York"), headers.get("one"));
}
@Test
public void nullValuesSkippedByDefault() {
Map<String, XPathExpressionEvaluatingHeaderValueMessageProcessor> expressionMap