From 55db3b11f0f6ac8cd44c94f2e43239da6ed8c284 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 17 Sep 2008 19:21:21 +0000 Subject: [PATCH] Renamed ApplicationEventSource and ApplicationEventTarget. The new names are ApplicationEventInboundChannelAdapter and ApplicationEventOutboundChannelAdapter respectively. --- ...pplicationEventInboundChannelAdapter.java} | 37 +++++++------------ ...plicationEventOutboundChannelAdapter.java} | 19 +++++----- ...ationEventInboundChannelAdapterTests.java} | 20 +++++----- ...tionEventOutboundChannelAdapterTests.java} | 6 +-- ...cationEventInboundChannelAdapterTests.xml} | 4 +- .../template.mf | 2 - 6 files changed, 40 insertions(+), 48 deletions(-) rename org.springframework.integration.event/src/main/java/org/springframework/integration/event/{ApplicationEventSource.java => ApplicationEventInboundChannelAdapter.java} (62%) rename org.springframework.integration.event/src/main/java/org/springframework/integration/event/{ApplicationEventTarget.java => ApplicationEventOutboundChannelAdapter.java} (60%) rename org.springframework.integration.event/src/test/java/org/springframework/integration/event/{ApplicationEventSourceTests.java => ApplicationEventInboundChannelAdapterTests.java} (86%) rename org.springframework.integration.event/src/test/java/org/springframework/integration/event/{ApplicationEventTargetTests.java => ApplicationEventOutboundChannelAdapterTests.java} (90%) rename org.springframework.integration.event/src/test/java/org/springframework/integration/event/{applicationEventSourceTests.xml => applicationEventInboundChannelAdapterTests.xml} (75%) diff --git a/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventSource.java b/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java similarity index 62% rename from org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventSource.java rename to org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java index d482a527c5..afeadefa4e 100644 --- a/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventSource.java +++ b/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapter.java @@ -16,36 +16,25 @@ package org.springframework.integration.event; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; -import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.message.GenericMessage; -import org.springframework.integration.message.MessageChannelTemplate; +import org.springframework.integration.endpoint.AbstractProducerEndpoint; +import org.springframework.integration.message.MessageBuilder; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** - * A message source for passing Spring + * An inbound Channel Adapter that passes Spring * {@link ApplicationEvent ApplicationEvents} within messages. * * @author Mark Fisher */ -public class ApplicationEventSource implements ApplicationListener { +public class ApplicationEventInboundChannelAdapter extends AbstractProducerEndpoint implements ApplicationListener { - private final MessageChannel channel; - - private List> eventTypes = new ArrayList>(); - - private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate(); - - - public ApplicationEventSource(MessageChannel channel) { - Assert.notNull(channel, "channel must not be null"); - this.channel = channel; - } + private final List> eventTypes = new CopyOnWriteArrayList>(); /** @@ -55,25 +44,27 @@ public class ApplicationEventSource implements ApplicationListener { */ public void setEventTypes(List> eventTypes) { Assert.notEmpty(eventTypes, "at least one event type is required"); - this.eventTypes = eventTypes; + synchronized (this.eventTypes) { + this.eventTypes.clear(); + this.eventTypes.addAll(eventTypes); + } } public void onApplicationEvent(ApplicationEvent event) { if (CollectionUtils.isEmpty(this.eventTypes)) { - this.sendMessage(event); + this.sendEventAsMessage(event); return; } for (Class eventType : this.eventTypes) { if (eventType.isAssignableFrom(event.getClass())) { - this.sendMessage(event); + this.sendEventAsMessage(event); return; } } } - private boolean sendMessage(ApplicationEvent event) { - return this.channelTemplate.send( - new GenericMessage(event), this.channel); + private boolean sendEventAsMessage(ApplicationEvent event) { + return this.sendMessage(MessageBuilder.fromPayload(event).build()); } } diff --git a/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventTarget.java b/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapter.java similarity index 60% rename from org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventTarget.java rename to org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapter.java index 9c6895532a..caefad06a0 100644 --- a/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventTarget.java +++ b/org.springframework.integration.event/src/main/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapter.java @@ -19,17 +19,18 @@ package org.springframework.integration.event; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.integration.endpoint.AbstractMessageConsumingEndpoint; import org.springframework.integration.message.Message; -import org.springframework.integration.message.MessageTarget; +import org.springframework.util.Assert; /** - * A message target for publishing {@link MessagingEvent MessagingEvents}. The - * {@link MessagingEvent} is a subclass of Spring's {@link ApplicationEvent} - * used by this adapter to wrap any {@link Message} sent to this target. + * An outbound Channel Adapter that publishes each {@link Message} it receives + * as a {@link MessagingEvent}. The {@link MessagingEvent} is a subclass of Spring's + * {@link ApplicationEvent} used by this adapter to simply wrap the {@link Message}. * * @author Mark Fisher */ -public class ApplicationEventTarget implements MessageTarget, ApplicationEventPublisherAware { +public class ApplicationEventOutboundChannelAdapter extends AbstractMessageConsumingEndpoint implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; @@ -38,10 +39,10 @@ public class ApplicationEventTarget implements MessageTarget, ApplicationEven this.applicationEventPublisher = applicationEventPublisher; } - public boolean send(Message message) { - this.applicationEventPublisher.publishEvent( - new MessagingEvent((Message) message)); - return true; + @Override + protected void processMessage(Message message) { + Assert.notNull(this.applicationEventPublisher, "applicationEventPublisher is required"); + this.applicationEventPublisher.publishEvent(new MessagingEvent((Message) message)); } } diff --git a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventSourceTests.java b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java similarity index 86% rename from org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventSourceTests.java rename to org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java index 55d7ce039f..74cb443656 100644 --- a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventSourceTests.java +++ b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventInboundChannelAdapterTests.java @@ -20,8 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import java.util.ArrayList; -import java.util.List; +import java.util.Collections; import org.junit.Test; @@ -38,12 +37,13 @@ import org.springframework.integration.message.Message; /** * @author Mark Fisher */ -public class ApplicationEventSourceTests { +public class ApplicationEventInboundChannelAdapterTests { @Test public void testAnyApplicationEventSentByDefault() { QueueChannel channel = new QueueChannel(); - ApplicationEventSource adapter = new ApplicationEventSource(channel); + ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); + adapter.setOutputChannel(channel); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); @@ -59,10 +59,9 @@ public class ApplicationEventSourceTests { @Test public void testOnlyConfiguredEventTypesAreSent() { QueueChannel channel = new QueueChannel(); - ApplicationEventSource adapter = new ApplicationEventSource(channel); - List> eventTypes = new ArrayList>(); - eventTypes.add(TestApplicationEvent1.class); - adapter.setEventTypes(eventTypes); + ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter(); + adapter.setOutputChannel(channel); + adapter.setEventTypes(Collections.>singletonList(TestApplicationEvent1.class)); Message message1 = channel.receive(0); assertNull(message1); adapter.onApplicationEvent(new TestApplicationEvent1()); @@ -76,7 +75,8 @@ public class ApplicationEventSourceTests { @Test public void testApplicationContextEvents() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationEventSourceTests.xml", this.getClass()); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "applicationEventInboundChannelAdapterTests.xml", this.getClass()); PollableChannel channel = (PollableChannel) context.getBean("channel"); Message refreshedEventMessage = channel.receive(0); assertNotNull(refreshedEventMessage); @@ -96,6 +96,7 @@ public class ApplicationEventSourceTests { } + @SuppressWarnings("serial") private static class TestApplicationEvent1 extends ApplicationEvent { public TestApplicationEvent1() { @@ -104,6 +105,7 @@ public class ApplicationEventSourceTests { } + @SuppressWarnings("serial") private static class TestApplicationEvent2 extends ApplicationEvent { public TestApplicationEvent2() { diff --git a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventTargetTests.java b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapterTests.java similarity index 90% rename from org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventTargetTests.java rename to org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapterTests.java index 3481a1dcad..fbfd0000c5 100644 --- a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventTargetTests.java +++ b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/ApplicationEventOutboundChannelAdapterTests.java @@ -29,17 +29,17 @@ import org.springframework.integration.message.StringMessage; /** * @author Mark Fisher */ -public class ApplicationEventTargetTests { +public class ApplicationEventOutboundChannelAdapterTests { @Test @SuppressWarnings("unchecked") public void testSendingEvent() throws InterruptedException { TestApplicationEventPublisher publisher = new TestApplicationEventPublisher(); - ApplicationEventTarget adapter = new ApplicationEventTarget(); + ApplicationEventOutboundChannelAdapter adapter = new ApplicationEventOutboundChannelAdapter(); adapter.setApplicationEventPublisher(publisher); assertNull(publisher.getLastEvent()); Message message = new StringMessage("testing"); - adapter.send(message); + adapter.onMessage(message); ApplicationEvent event = publisher.getLastEvent(); assertEquals(MessagingEvent.class, event.getClass()); assertEquals(message, ((MessagingEvent) event).getMessage()); diff --git a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventSourceTests.xml b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml similarity index 75% rename from org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventSourceTests.xml rename to org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml index 7729a92f1b..38e093edfb 100644 --- a/org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventSourceTests.xml +++ b/org.springframework.integration.event/src/test/java/org/springframework/integration/event/applicationEventInboundChannelAdapterTests.xml @@ -8,8 +8,8 @@ - - + + diff --git a/org.springframework.integration.event/template.mf b/org.springframework.integration.event/template.mf index 343e765dea..724ae87feb 100644 --- a/org.springframework.integration.event/template.mf +++ b/org.springframework.integration.event/template.mf @@ -7,6 +7,4 @@ Import-Template: org.springframework.context;version="[2.5.5.A, 3.0.0)", org.springframework.util;version="[2.5.5.A, 3.0.0)", org.apache.commons.logging;version="[1.1.1, 2.0.0)" -Unversioned-Imports: - org.w3c.dom