From 8d1e5e3f1dad5a00b0013501aec9003501d465b4 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 4 Nov 2010 12:31:45 -0400 Subject: [PATCH] INT-1554 refactored XmppRosterEventOutboundEndpointParser, cleaned up XmppRosterEventMessageSendingHandler, added tests --- .../xmpp/config/XmppNamespaceHandler.java | 14 ---- ...XmppRosterEventOutboundEndpointParser.java | 40 ++++++++++ .../XmppRosterEventMessageSendingHandler.java | 76 ++++++++----------- .../config/spring-integration-xmpp-2.0.xsd | 12 +++ ...boundChannelAdapterParserTests-context.xml | 37 +++++++++ ...ventOutboundChannelAdapterParserTests.java | 70 +++++++++++++++++ ...dXmppRosterEventsEndpointTests-context.xml | 2 - ...RosterEventMessageSendingHandlerTests.java | 24 ++++++ 8 files changed, 215 insertions(+), 60 deletions(-) create mode 100644 spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundEndpointParser.java create mode 100644 spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests-context.xml create mode 100644 spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests.java create mode 100644 spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/messages/XmppRosterEventMessageSendingHandlerTests.java diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppNamespaceHandler.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppNamespaceHandler.java index 9f862253bd..21007a4fe1 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppNamespaceHandler.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppNamespaceHandler.java @@ -92,19 +92,6 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport { } } - private static class XmppRosterEventOutboundEndpointParser extends AbstractOutboundChannelAdapterParser { - - @Override - protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( - PACKAGE_NAME + ".presence.XmppRosterEventMessageSendingHandler"); - String connectionName = element.getAttribute("xmpp-connection"); - Assert.hasText(connectionName, "'xmpp-connection' must be defined"); - builder.addPropertyReference("xmppConnection", connectionName); - return builder.getBeanDefinition(); - } - } - private static class XmppRosterEventInboundEndpointParser extends AbstractSingleBeanDefinitionParser { @Override @@ -123,7 +110,6 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport { Assert.hasText(connectionName, "'xmpp-connection' must be defined"); builder.addPropertyReference("xmppConnection", connectionName); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel"); - //IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup"); } } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundEndpointParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundEndpointParser.java new file mode 100644 index 0000000000..80d9f007f2 --- /dev/null +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundEndpointParser.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.xmpp.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.AbstractOutboundChannelAdapterParser; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.w3c.dom.Element; + +/** + * @author Oleg Zhurakousky + * @since 2.0 + */ +public class XmppRosterEventOutboundEndpointParser extends AbstractOutboundChannelAdapterParser { + + @Override + protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.xmpp.presence.XmppRosterEventMessageSendingHandler"); + String connectionName = element.getAttribute("xmpp-connection"); + builder.addConstructorArgReference(connectionName); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-mapper"); + return builder.getBeanDefinition(); + } +} diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/presence/XmppRosterEventMessageSendingHandler.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/presence/XmppRosterEventMessageSendingHandler.java index 2cb6f7aeec..9e356ec6ad 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/presence/XmppRosterEventMessageSendingHandler.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/presence/XmppRosterEventMessageSendingHandler.java @@ -1,13 +1,26 @@ +/* + * 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.xmpp.presence; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.Presence; -import org.springframework.context.Lifecycle; import org.springframework.integration.Message; import org.springframework.integration.handler.AbstractMessageHandler; import org.springframework.integration.mapping.OutboundMessageMapper; +import org.springframework.util.Assert; /** * This class will facilitate publishing updated presence values for a given connection. This change happens on the @@ -21,43 +34,17 @@ import org.springframework.integration.mapping.OutboundMessageMapper; * {@link org.jivesoftware.smack.packet.Presence.Type#available} ) * @since 2.0 */ -public class XmppRosterEventMessageSendingHandler extends AbstractMessageHandler implements Lifecycle { - private static final Log logger = LogFactory.getLog(XmppRosterEventMessageDrivenEndpoint.class); - - private volatile boolean running; - +public class XmppRosterEventMessageSendingHandler extends AbstractMessageHandler { + private OutboundMessageMapper messageMapper; - private volatile XMPPConnection xmppConnection; - - public void setXmppConnection(final XMPPConnection xmppConnection) { + private final XMPPConnection xmppConnection; + + public XmppRosterEventMessageSendingHandler(XMPPConnection xmppConnection){ + Assert.notNull(xmppConnection, "'xmppConnection' must not be null"); this.xmppConnection = xmppConnection; } - - public boolean isRunning() { - return this.running; - } - - public void start() { - if (null == this.messageMapper) { - this.messageMapper = new XmppPresenceMessageMapper(); - } - - this.running = true; - } - - public void stop() { - this.running = false; - - if (xmppConnection.isConnected()) { - if (logger.isInfoEnabled()) { - logger.info("shutting down XMPP connection"); - } - - xmppConnection.disconnect(); - } - } - + /** * the MessageMapper is responsible for converting outbound Messages into status updates of type * {@link org.jivesoftware.smack.packet.Presence} @@ -68,14 +55,15 @@ public class XmppRosterEventMessageSendingHandler extends AbstractMessageHandler this.messageMapper = messageMapper; } - @Override - protected void handleMessageInternal(Message message) throws Exception { - try { - Presence presence = this.messageMapper.fromMessage(message); - this.xmppConnection.sendPacket(presence); - } - catch (Exception e) { - logger.error("Failed to map packet to message ", e); + protected void onInit() throws Exception { + if (this.messageMapper == null) { + this.messageMapper = new XmppPresenceMessageMapper(); } } + + @Override + protected void handleMessageInternal(Message message) throws Exception { + Presence presence = this.messageMapper.fromMessage(message); + this.xmppConnection.sendPacket(presence); + } } diff --git a/spring-integration-xmpp/src/main/resources/org/springframework/integration/xmpp/config/spring-integration-xmpp-2.0.xsd b/spring-integration-xmpp/src/main/resources/org/springframework/integration/xmpp/config/spring-integration-xmpp-2.0.xsd index 0da2252357..54c1d01c0a 100644 --- a/spring-integration-xmpp/src/main/resources/org/springframework/integration/xmpp/config/spring-integration-xmpp-2.0.xsd +++ b/spring-integration-xmpp/src/main/resources/org/springframework/integration/xmpp/config/spring-integration-xmpp-2.0.xsd @@ -55,7 +55,19 @@ + + + + + + + + + + + + diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests-context.xml b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests-context.xml new file mode 100644 index 0000000000..02a0d3f985 --- /dev/null +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests-context.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests.java new file mode 100644 index 0000000000..576a1c2407 --- /dev/null +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppRosterEventOutboundChannelAdapterParserTests.java @@ -0,0 +1,70 @@ +/* + * 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.xmpp.config; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.endpoint.EventDrivenConsumer; +import org.springframework.integration.endpoint.PollingConsumer; +import org.springframework.integration.mapping.OutboundMessageMapper; +import org.springframework.integration.test.util.TestUtils; +import org.springframework.integration.xmpp.presence.XmppPresenceMessageMapper; +import org.springframework.integration.xmpp.presence.XmppRosterEventMessageSendingHandler; + +/** + * @author Oleg Zhurakousky + * + */ +public class XmppRosterEventOutboundChannelAdapterParserTests { + + @Test + public void testRosterEventOutboundChannelAdapterParserAsPollingConsumer(){ + ApplicationContext ac = + new ClassPathXmlApplicationContext("XmppRosterEventOutboundChannelAdapterParserTests-context.xml", this.getClass()); + Object pollingConsumer = ac.getBean("pollingOutboundRosterAdapter"); + assertTrue(pollingConsumer instanceof PollingConsumer); + } + @Test + @SuppressWarnings("rawtypes") + public void testRosterEventOutboundChannelAdapterParserDefaultMapper(){ + ApplicationContext ac = + new ClassPathXmlApplicationContext("XmppRosterEventOutboundChannelAdapterParserTests-context.xml", this.getClass()); + Object pollingConsumer = ac.getBean("pollingOutboundRosterAdapter"); + XmppRosterEventMessageSendingHandler handler = + TestUtils.getPropertyValue(pollingConsumer, "handler", XmppRosterEventMessageSendingHandler.class); + OutboundMessageMapper mapper = TestUtils.getPropertyValue(handler, "messageMapper", OutboundMessageMapper.class); + assertNotNull(mapper); + assertTrue(mapper instanceof XmppPresenceMessageMapper); + } + @SuppressWarnings("rawtypes") + @Test + public void testRosterEventOutboundChannelAdapterParserCustomMapperEventDriven(){ + ApplicationContext ac = + new ClassPathXmlApplicationContext("XmppRosterEventOutboundChannelAdapterParserTests-context.xml", this.getClass()); + Object eventConsumer = ac.getBean("eventOutboundRosterAdapter"); + assertTrue(eventConsumer instanceof EventDrivenConsumer); + XmppRosterEventMessageSendingHandler handler = + TestUtils.getPropertyValue(eventConsumer, "handler", XmppRosterEventMessageSendingHandler.class); + OutboundMessageMapper mapper = TestUtils.getPropertyValue(handler, "messageMapper", OutboundMessageMapper.class); + assertNotNull(mapper); + assertFalse(mapper instanceof XmppPresenceMessageMapper); + } +} diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundXmppRosterEventsEndpointTests-context.xml b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundXmppRosterEventsEndpointTests-context.xml index ea2c506146..a26994b502 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundXmppRosterEventsEndpointTests-context.xml +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundXmppRosterEventsEndpointTests-context.xml @@ -42,8 +42,6 @@ host="${user.1.host}" port="${user.1.port}" resource="${user.1.resource}" - sasl-mechanism-supported="${user.1.sasl.mechanism}" - sasl-mechanism-supported-index="${user.1.sasl.index}" service-name="${user.1.service}" /> diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/messages/XmppRosterEventMessageSendingHandlerTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/messages/XmppRosterEventMessageSendingHandlerTests.java new file mode 100644 index 0000000000..288c8fb165 --- /dev/null +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/messages/XmppRosterEventMessageSendingHandlerTests.java @@ -0,0 +1,24 @@ +/* + * 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.xmpp.messages; + +/** + * @author Oleg Zhurakousky + * + */ +public class XmppRosterEventMessageSendingHandlerTests { + +}