From b62b8f0f8027b08e2c2ba5962c2b98e841820f51 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 17 Jun 2010 20:25:26 +0000 Subject: [PATCH] INT-1187 added bean resolution support for expression based filters --- .../AbstractMessageProcessingSelector.java | 11 +++- .../integration/filter/MessageFilter.java | 4 ++ .../SpelFilterIntegrationTests-context.xml | 22 +++++-- .../filter/SpelFilterIntegrationTests.java | 61 ++++++++++++++----- 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/filter/AbstractMessageProcessingSelector.java b/spring-integration-core/src/main/java/org/springframework/integration/filter/AbstractMessageProcessingSelector.java index 07aba7ce12..b6b75bff17 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/filter/AbstractMessageProcessingSelector.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/filter/AbstractMessageProcessingSelector.java @@ -16,6 +16,9 @@ package org.springframework.integration.filter; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.core.convert.ConversionService; import org.springframework.integration.core.Message; import org.springframework.integration.handler.AbstractMessageProcessor; @@ -29,7 +32,7 @@ import org.springframework.util.Assert; * * @author Mark Fisher */ -abstract class AbstractMessageProcessingSelector implements MessageSelector { +abstract class AbstractMessageProcessingSelector implements MessageSelector, BeanFactoryAware { private final MessageProcessor messageProcessor; @@ -46,6 +49,12 @@ abstract class AbstractMessageProcessingSelector implements MessageSelector { } } + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + if (this.messageProcessor instanceof BeanFactoryAware) { + ((BeanFactoryAware) this.messageProcessor).setBeanFactory(beanFactory); + } + } + public final boolean accept(Message message) { Object result = this.messageProcessor.processMessage(message); Assert.notNull(result, "result must not be null"); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java b/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java index 4a121b82bd..f88e6e6802 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java @@ -16,6 +16,7 @@ package org.springframework.integration.filter; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.integration.core.Message; import org.springframework.integration.core.MessageChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; @@ -91,6 +92,9 @@ public class MessageFilter extends AbstractReplyProducingMessageHandler { if (this.selector instanceof AbstractMessageProcessingSelector) { ((AbstractMessageProcessingSelector) this.selector).setConversionService(this.getConversionService()); } + if (this.selector instanceof BeanFactoryAware) { + ((BeanFactoryAware) this.selector).setBeanFactory(this.getBeanFactory()); + } } @Override diff --git a/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests-context.xml index 22e7cd5f69..dc2522f1e6 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests-context.xml @@ -1,22 +1,32 @@ - - - - + - + + + + + + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests.java index e2368e74c6..5e76967846 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/filter/SpelFilterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -23,7 +23,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.core.MessageChannel; import org.springframework.integration.message.GenericMessage; @@ -37,28 +36,60 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) public class SpelFilterIntegrationTests { - @Autowired @Qualifier("input") - private MessageChannel input; + @Autowired + private MessageChannel simpleInput; - @Autowired @Qualifier("positives") + @Autowired private PollableChannel positives; - @Autowired @Qualifier("discards") - private PollableChannel discards; + @Autowired + private PollableChannel negatives; + + @Autowired + private MessageChannel beanResolvingInput; + + @Autowired + private PollableChannel evens; + + @Autowired + private PollableChannel odds; @Test - public void filter() { - this.input.send(new GenericMessage(1)); - this.input.send(new GenericMessage(0)); - this.input.send(new GenericMessage(99)); - this.input.send(new GenericMessage(-99)); + public void simpleExpressionBasedFilter() { + this.simpleInput.send(new GenericMessage(1)); + this.simpleInput.send(new GenericMessage(0)); + this.simpleInput.send(new GenericMessage(99)); + this.simpleInput.send(new GenericMessage(-99)); assertEquals(new Integer(1), positives.receive(0).getPayload()); assertEquals(new Integer(99), positives.receive(0).getPayload()); - assertEquals(new Integer(0), discards.receive(0).getPayload()); - assertEquals(new Integer(-99), discards.receive(0).getPayload()); + assertEquals(new Integer(0), negatives.receive(0).getPayload()); + assertEquals(new Integer(-99), negatives.receive(0).getPayload()); assertNull(positives.receive(0)); - assertNull(discards.receive(0)); + assertNull(negatives.receive(0)); + } + + @Test + public void beanResolvingExpressionBasedFilter() { + this.beanResolvingInput.send(new GenericMessage(1)); + this.beanResolvingInput.send(new GenericMessage(2)); + this.beanResolvingInput.send(new GenericMessage(9)); + this.beanResolvingInput.send(new GenericMessage(22)); + assertEquals(new Integer(1), odds.receive(0).getPayload()); + assertEquals(new Integer(9), odds.receive(0).getPayload()); + assertEquals(new Integer(2), evens.receive(0).getPayload()); + assertEquals(new Integer(22), evens.receive(0).getPayload()); + assertNull(odds.receive(0)); + assertNull(evens.receive(0)); + } + + + @SuppressWarnings("unused") + private static class TestBean { + + public boolean isEven(int number) { + return number % 2 == 0; + } } }