From 2c7b57448bde6a6f8b86158e0211e7d5d0fe051a Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 23 Aug 2010 18:10:58 +0000 Subject: [PATCH] INT-1338, Added support for property placeholder --- .../config/xml/IntegrationNamespaceUtils.java | 18 ------------- ...ApplicationEventInboundChannelAdapter.java | 5 ++-- .../EventInboundChannelAdapterParser.java | 25 +------------------ ...cationEventInboundChannelAdapterTests.java | 9 ++----- ...boundChannelAdapterParserTests-context.xml | 17 ++++++++++--- ...EventInboundChannelAdapterParserTests.java | 15 +++++++++++ .../event/config/inbound-adapter.properties | 1 + 7 files changed, 36 insertions(+), 54 deletions(-) create mode 100644 spring-integration-event/src/test/java/org/springframework/integration/event/config/inbound-adapter.properties diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/IntegrationNamespaceUtils.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/IntegrationNamespaceUtils.java index 8ab8a40d74..219befc379 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/IntegrationNamespaceUtils.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/IntegrationNamespaceUtils.java @@ -262,22 +262,4 @@ public abstract class IntegrationNamespaceUtils { + " are not allowed together."); return innerComponentDefinition; } - - public static Class convertFqClassNameToClassObject(String fullyQualifiedClassname, ParserContext parserContext){ - Assert.isTrue(StringUtils.hasText(fullyQualifiedClassname), "'fullyQualifiedClassname' must be provided"); - Assert.notNull(parserContext, "'parserContext' must be provided"); - ClassLoader classLoader = parserContext.getReaderContext().getBeanClassLoader(); - if (classLoader == null) { - classLoader = ClassUtils.getDefaultClassLoader(); - } - if (classLoader != null){ - try { - return classLoader.loadClass(fullyQualifiedClassname); - } catch (Exception e) { - // no handling required, method may return null - } - - } - return null; - } } diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java b/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java index bc5d57b60f..bc43f64517 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java @@ -42,11 +42,12 @@ public class ApplicationEventInboundChannelAdapter extends MessageProducerSuppor * this adapter should send to the message channel. By default, all event * types will be sent. */ - public void setEventTypes(Set> eventTypes) { + @SuppressWarnings("unchecked") + public void setEventTypes(Class[] eventTypes) { Assert.notEmpty(eventTypes, "at least one event type is required"); synchronized (this.eventTypes) { this.eventTypes.clear(); - this.eventTypes.addAll(eventTypes); + this.eventTypes.addAll(CollectionUtils.arrayToList(eventTypes)); } } diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java index 11f5be9d87..3b605e5024 100644 --- a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java @@ -15,20 +15,12 @@ */ package org.springframework.integration.event.config; -import java.util.HashSet; -import java.util.Set; -import java.util.StringTokenizer; - import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.context.ApplicationEvent; import org.springframework.integration.config.xml.AbstractChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.event.ApplicationEventInboundChannelAdapter; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; import org.w3c.dom.Element; /** @@ -37,27 +29,12 @@ import org.w3c.dom.Element; */ public class EventInboundChannelAdapterParser extends AbstractChannelAdapterParser{ - @SuppressWarnings("unchecked") @Override protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) { BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.rootBeanDefinition(ApplicationEventInboundChannelAdapter.class); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(adapterBuilder, element, "channel", "outputChannel"); - String stringEventTypes = element.getAttribute("event-types"); - if (StringUtils.hasText(stringEventTypes)){ - //Set eventTypes = StringUtils.commaDelimitedListToSet(stringEventTypes); - StringTokenizer tokenizer = new StringTokenizer(stringEventTypes, " ,"); - Set> applicationEvents = new HashSet>(); - while (tokenizer.hasMoreTokens()) { - String fullyQualifiedClassname = tokenizer.nextToken(); - Class clazz = - (Class) IntegrationNamespaceUtils.convertFqClassNameToClassObject(fullyQualifiedClassname, parserContext); - Assert.notNull(clazz, "Class for the event type '" + fullyQualifiedClassname + "' can not be located"); - applicationEvents.add(clazz); - } - adapterBuilder.addPropertyValue("eventTypes", applicationEvents); - } - //IntegrationNamespaceUtils.setReferenceIfAttributeDefined(adapterBuilder, element, "event", "eventTypes"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, element, "event-types"); return adapterBuilder.getBeanDefinition(); } diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java index 139c8f9641..b3b27c4fb8 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java @@ -16,10 +16,6 @@ package org.springframework.integration.event; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - import org.junit.Test; import org.springframework.context.ApplicationEvent; import org.springframework.context.event.ContextClosedEvent; @@ -57,14 +53,13 @@ public class ApplicationEventInboundChannelAdapterTests { assertEquals("event2", ((ApplicationEvent) message3.getPayload()).getSource()); } + @SuppressWarnings("unchecked") @Test public void onlyConfiguredEventTypesAreSent() { QueueChannel channel = new QueueChannel(); ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); adapter.setOutputChannel(channel); - Set> events = new HashSet>(); - events.add(TestApplicationEvent1.class); - adapter.setEventTypes(events); + adapter.setEventTypes(new Class[]{TestApplicationEvent1.class}); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml index 2cce1fb3ea..dddd5381b2 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml @@ -1,11 +1,13 @@ + http://www.springframework.org/schema/integration/event http://www.springframework.org/schema/integration/event/spring-integration-event-2.0.xsd" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:int="http://www.springframework.org/schema/integration" + xmlns:int-event="http://www.springframework.org/schema/integration/event"> @@ -19,5 +21,14 @@ + + + + + + + + diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java index 81f22664bf..de0504f126 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java @@ -74,6 +74,21 @@ public class EventInboundChannelAdapterParserTests { assertTrue(eventTypes.contains(SampleEvent.class)); assertTrue(eventTypes.contains(AnotherSampleEvent.class)); } + + @SuppressWarnings("unchecked") + @Test + public void validateEventParserWithEventTypesAndPlaceholder() { + Object adapter = context.getBean("eventAdapterFilteredPlaceHolder"); + Assert.assertNotNull(adapter); + Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); + Assert.assertEquals(context.getBean("inputFilteredPlaceHolder"), adapterAccessor.getPropertyValue("outputChannel")); + Set> eventTypes = (Set>) adapterAccessor.getPropertyValue("eventTypes"); + assertNotNull(eventTypes); + assertTrue(eventTypes.size() == 2); + assertTrue(eventTypes.contains(SampleEvent.class)); + assertTrue(eventTypes.contains(AnotherSampleEvent.class)); + } @Test public void validateUsage() { diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/config/inbound-adapter.properties b/spring-integration-event/src/test/java/org/springframework/integration/event/config/inbound-adapter.properties new file mode 100644 index 0000000000..d602625a52 --- /dev/null +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/inbound-adapter.properties @@ -0,0 +1 @@ +event.types=org.springframework.integration.event.config.EventInboundChannelAdapterParserTests$AnotherSampleEvent, org.springframework.integration.event.config.EventInboundChannelAdapterParserTests$SampleEvent \ No newline at end of file