From 6c402903e6cf1385a3ac531faee4146c36d252e7 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 28 Jun 2010 16:18:21 +0000 Subject: [PATCH] INT-1221, Added namespace support for ApplicationContextEvent Adapters --- spring-integration-event/.project | 6 ++ .../.settings/org.eclipse.jdt.core.prefs | 9 ++- spring-integration-event/pom.xml | 8 ++ .../EventInboundChannelAdapterParser.java | 40 ++++++++++ .../event/config/EventNamespaceHandler.java | 33 +++++++++ .../main/resources/META-INF/spring.handlers | 1 + .../main/resources/META-INF/spring.schemas | 2 + .../main/resources/META-INF/spring.tooling | 3 + .../config/spring-integration-event-2.0.xsd | 44 +++++++++++ .../src/test/java/log4j.properties | 8 ++ ...icationEventInboundChannelAdapterTests.xml | 13 ++-- ...boundChannelAdapterParserTests-context.xml | 16 ++++ ...EventInboundChannelAdapterParserTests.java | 74 +++++++++++++++++++ spring-integration-event/template.mf | 4 +- 14 files changed, 254 insertions(+), 7 deletions(-) create mode 100644 spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java create mode 100644 spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java create mode 100644 spring-integration-event/src/main/resources/META-INF/spring.handlers create mode 100644 spring-integration-event/src/main/resources/META-INF/spring.schemas create mode 100644 spring-integration-event/src/main/resources/META-INF/spring.tooling create mode 100644 spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd create mode 100644 spring-integration-event/src/test/java/log4j.properties create mode 100644 spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml create mode 100644 spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java diff --git a/spring-integration-event/.project b/spring-integration-event/.project index d6cc138a4b..851a478fd9 100644 --- a/spring-integration-event/.project +++ b/spring-integration-event/.project @@ -15,8 +15,14 @@ + + org.springframework.ide.eclipse.core.springbuilder + + + + org.springframework.ide.eclipse.core.springnature org.maven.ide.eclipse.maven2Nature org.eclipse.jdt.core.javanature diff --git a/spring-integration-event/.settings/org.eclipse.jdt.core.prefs b/spring-integration-event/.settings/org.eclipse.jdt.core.prefs index b6692aeb8f..3c6062bcd8 100644 --- a/spring-integration-event/.settings/org.eclipse.jdt.core.prefs +++ b/spring-integration-event/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,14 @@ -#Wed May 26 00:58:03 CEST 2010 +#Mon Jun 28 10:49:24 EDT 2010 eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning org.eclipse.jdt.core.compiler.problem.deadCode=warning @@ -11,6 +17,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore diff --git a/spring-integration-event/pom.xml b/spring-integration-event/pom.xml index 5952b7f9e3..073a51a677 100644 --- a/spring-integration-event/pom.xml +++ b/spring-integration-event/pom.xml @@ -26,6 +26,14 @@ junit junit + + org.springframework + spring-test + + + org.mockito + mockito-all + 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 new file mode 100644 index 0000000000..c8cad719ef --- /dev/null +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventInboundChannelAdapterParser.java @@ -0,0 +1,40 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on 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.event.config; + +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.AbstractChannelAdapterParser; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.integration.event.ApplicationEventInboundChannelAdapter; +import org.w3c.dom.Element; + +/** + * @author Oleg Zhurakousky + * + */ +public class EventInboundChannelAdapterParser extends AbstractChannelAdapterParser{ + + @Override + protected AbstractBeanDefinition doParse(Element element, + ParserContext parserContext, String channelName) { + BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.rootBeanDefinition(ApplicationEventInboundChannelAdapter.class); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(adapterBuilder, element, "channel", "outputChannel"); + return adapterBuilder.getBeanDefinition(); + } + +} diff --git a/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java new file mode 100644 index 0000000000..deaf84e8ec --- /dev/null +++ b/spring-integration-event/src/main/java/org/springframework/integration/event/config/EventNamespaceHandler.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2009 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on 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.event.config; + +import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler; + +/** + * Namespace handler for Spring Integration's 'event' namespace. + * + * @author Oleg Zhurakousky + * @since 2.0 + */ +public class EventNamespaceHandler extends AbstractIntegrationNamespaceHandler { + + public void init() { + registerBeanDefinitionParser("inbound-channel-adapter", new EventInboundChannelAdapterParser()); + } + +} diff --git a/spring-integration-event/src/main/resources/META-INF/spring.handlers b/spring-integration-event/src/main/resources/META-INF/spring.handlers new file mode 100644 index 0000000000..545ed5d629 --- /dev/null +++ b/spring-integration-event/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/integration/event=org.springframework.integration.event.config.EventNamespaceHandler \ No newline at end of file diff --git a/spring-integration-event/src/main/resources/META-INF/spring.schemas b/spring-integration-event/src/main/resources/META-INF/spring.schemas new file mode 100644 index 0000000000..0598e0cccb --- /dev/null +++ b/spring-integration-event/src/main/resources/META-INF/spring.schemas @@ -0,0 +1,2 @@ +http\://www.springframework.org/schema/integration/event/spring-integration-event-2.0.xsd=org/springframework/integration/event/config/spring-integration-event-2.0.xsd +http\://www.springframework.org/schema/integration/event/spring-integration-event.xsd=org/springframework/integration/event/config/spring-integration-event-2.0.xsd \ No newline at end of file diff --git a/spring-integration-event/src/main/resources/META-INF/spring.tooling b/spring-integration-event/src/main/resources/META-INF/spring.tooling new file mode 100644 index 0000000000..4b11beb7e3 --- /dev/null +++ b/spring-integration-event/src/main/resources/META-INF/spring.tooling @@ -0,0 +1,3 @@ +# Tooling related information for the integration event namespace +http\://www.springframework.org/schema/integration/event@name=integration event Namespace +http\://www.springframework.org/schema/integration/event@prefix=int-event diff --git a/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd b/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd new file mode 100644 index 0000000000..88cdf0a6c8 --- /dev/null +++ b/spring-integration-event/src/main/resources/org/springframework/integration/event/config/spring-integration-event-2.0.xsd @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + Configures an inbound Channel Adapter which listens for an Application Context events, converts them to + Messages and sends them to a 'channel' + + + + + + + + + + + + + Identifies inbound 'channel' which accepts Messages generated from Application Context events. + + + + + + diff --git a/spring-integration-event/src/test/java/log4j.properties b/spring-integration-event/src/test/java/log4j.properties new file mode 100644 index 0000000000..941cbe4822 --- /dev/null +++ b/spring-integration-event/src/test/java/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootCategory=WARN, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%c{1}: %m%n + +log4j.category.org.springframework.integration=WARN +log4j.category.org.springframework.integration.file=WARN diff --git a/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml b/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml index 5e339a8b32..c99b9a81d7 100644 --- a/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml @@ -1,10 +1,13 @@ - - - + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" + xmlns:int="http://www.springframework.org/schema/integration"> + + + + 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 new file mode 100644 index 0000000000..259da42760 --- /dev/null +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests-context.xml @@ -0,0 +1,16 @@ + + + + + + + + + + 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 new file mode 100644 index 0000000000..9f2d2cbf6a --- /dev/null +++ b/spring-integration-event/src/test/java/org/springframework/integration/event/config/EventInboundChannelAdapterParserTests.java @@ -0,0 +1,74 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on 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.event.config; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; +import org.springframework.integration.channel.SubscribableChannel; +import org.springframework.integration.core.Message; +import org.springframework.integration.event.ApplicationEventInboundChannelAdapter; +import org.springframework.integration.message.MessageHandler; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Oleg Zhurakousky + * @since 2.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class EventInboundChannelAdapterParserTests { + @Autowired + private ApplicationContext context; + + @Test + public void validateEventParser(){ + Object adapter = context.getBean("eventAdapter"); + Assert.assertNotNull(adapter); + Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter); + DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); + Assert.assertEquals(context.getBean("input"), adapterAccessor.getPropertyValue("outputChannel")); + } + @Test + public void validateUsage(){ + SubscribableChannel channel = context.getBean("input", SubscribableChannel.class); + MessageHandler handler = Mockito.mock(MessageHandler.class); + channel.subscribe(handler); + final ApplicationEvent event = new SampleEvent("hello"); + context.publishEvent(new SampleEvent("hello")); + + Mockito.verify(handler, Mockito.times(1)).handleMessage(Mockito.any(Message.class)); + } + + public static class SampleEvent extends ApplicationEvent { + + public SampleEvent(Object source) { + super(source); + } + + } +} diff --git a/spring-integration-event/template.mf b/spring-integration-event/template.mf index 3190459350..0d7242696f 100644 --- a/spring-integration-event/template.mf +++ b/spring-integration-event/template.mf @@ -6,4 +6,6 @@ Import-Template: org.springframework.integration.*;version="[2.0.0, 2.0.1)", org.springframework.context;version="[3.0.0, 4.0.0)", org.springframework.util;version="[3.0.0, 4.0.0)", - org.apache.commons.logging;version="[1.1.1, 2.0.0)" + org.springframework.beans.*;version="[3.0.0, 4.0.0)", + org.apache.commons.logging;version="[1.1.1, 2.0.0)", + org.w3c.dom.*;version="0"