diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppInboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppInboundChannelAdapterParser.java new file mode 100644 index 0000000000..492de54e29 --- /dev/null +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppInboundChannelAdapterParser.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 org.w3c.dom.Element; + +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.util.StringUtils; + +/** + * Base class for XMPP inbound parsers + * + * @author Oleg Zhurakousky + * @since 2.0 + */ +public abstract class AbstractXmppInboundChannelAdapterParser extends AbstractSingleBeanDefinitionParser { + + @Override + protected boolean shouldGenerateId() { + return false; + } + + @Override + protected boolean shouldGenerateIdAsFallback() { + return true; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + String connectionName = element.getAttribute("xmpp-connection"); + + if (StringUtils.hasText(connectionName)){ + builder.addConstructorArgReference(connectionName); + } + else if (parserContext.getRegistry().containsBeanDefinition(XmppNamespaceHandler.XMPP_CONNECTION_BEAN_NAME)) { + builder.addConstructorArgReference(XmppNamespaceHandler.XMPP_CONNECTION_BEAN_NAME); + } + else { + throw new BeanCreationException("You must either explicitly define which XMPP connection to use via " + + "'xmpp-connection' attribute or have default XMPP connection bean registered under the name 'xmppConnection'" + + "(e.g., ). If 'id' is not provided the default will be 'xmppConnection'."); + } + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup"); + this.postProcess(element, parserContext, builder); + } + + protected void postProcess(Element element, ParserContext parserContext, BeanDefinitionBuilder builder){ + // no op + } + +} diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppOutboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppOutboundChannelAdapterParser.java new file mode 100644 index 0000000000..ae29d54695 --- /dev/null +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/AbstractXmppOutboundChannelAdapterParser.java @@ -0,0 +1,56 @@ +/* + * 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.BeanCreationException; +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.util.StringUtils; + +import org.w3c.dom.Element; + +/** + * Parser for 'xmpp:presence-outbound-channel-adapter' element + * + * @author Oleg Zhurakousky + * @since 2.0 + */ +public abstract class AbstractXmppOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { + + @Override + protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(this.getHandlerClassName()); + String connectionName = element.getAttribute("xmpp-connection"); + if (StringUtils.hasText(connectionName)){ + builder.addConstructorArgReference(connectionName); + } + else if (parserContext.getRegistry().containsBeanDefinition(XmppNamespaceHandler.XMPP_CONNECTION_BEAN_NAME)) { + builder.addConstructorArgReference(XmppNamespaceHandler.XMPP_CONNECTION_BEAN_NAME); + } + else { + throw new BeanCreationException("You must either explicitly define which XMPP connection to use via " + + "'xmpp-connection' attribute or have default XMPP connection bean registered under the name 'xmppConnection'" + + "(e.g., ). If 'id' is not provided the default will be 'xmppConnection'."); + } + + return builder.getBeanDefinition(); + } + + protected abstract String getHandlerClassName(); +} diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParser.java index 20049adf96..bd59b37b15 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParser.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParser.java @@ -19,7 +19,6 @@ package org.springframework.integration.xmpp.config; import org.w3c.dom.Element; import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; @@ -30,7 +29,7 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils; * @author Oleg Zhurakousky * @since 2.0 */ -public class ChatMessageInboundChannelAdapterParser extends AbstractSingleBeanDefinitionParser { +public class ChatMessageInboundChannelAdapterParser extends AbstractXmppInboundChannelAdapterParser { @Override protected String getBeanClassName(Element element) { @@ -38,22 +37,8 @@ public class ChatMessageInboundChannelAdapterParser extends AbstractSingleBeanDe } @Override - protected boolean shouldGenerateId() { - return false; - } - - @Override - protected boolean shouldGenerateIdAsFallback() { - return true; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - String connectionName = element.getAttribute("xmpp-connection"); - builder.addConstructorArgReference(connectionName); - IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel"); + protected void postProcess(Element element, ParserContext parserContext, BeanDefinitionBuilder builder){ 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/ChatMessageOutboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageOutboundChannelAdapterParser.java index 40a31d040c..c69a8c1fbe 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageOutboundChannelAdapterParser.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/ChatMessageOutboundChannelAdapterParser.java @@ -16,11 +16,6 @@ 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.w3c.dom.Element; /** * Parser for the XMPP 'outbound-channel-adapter' element @@ -28,15 +23,11 @@ import org.w3c.dom.Element; * @author Oleg Zhurakousky * @since 2.0 */ -public class ChatMessageOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { +public class ChatMessageOutboundChannelAdapterParser extends AbstractXmppOutboundChannelAdapterParser { @Override - protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( - "org.springframework.integration.xmpp.outbound.ChatMessageSendingMessageHandler"); - String connectionName = element.getAttribute("xmpp-connection"); - builder.addConstructorArgReference(connectionName); - return builder.getBeanDefinition(); + protected String getHandlerClassName() { + return "org.springframework.integration.xmpp.outbound.ChatMessageSendingMessageHandler"; } } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceInboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceInboundChannelAdapterParser.java index 8e115a0a05..9b21702677 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceInboundChannelAdapterParser.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceInboundChannelAdapterParser.java @@ -16,10 +16,6 @@ package org.springframework.integration.xmpp.config; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.w3c.dom.Element; /** @@ -29,29 +25,10 @@ import org.w3c.dom.Element; * @author Oleg Zhurakousky * @since 2.0 */ -public class PresenceInboundChannelAdapterParser extends AbstractSingleBeanDefinitionParser { +public class PresenceInboundChannelAdapterParser extends AbstractXmppInboundChannelAdapterParser { @Override protected String getBeanClassName(Element element) { return "org.springframework.integration.xmpp.inbound.PresenceListeningEndpoint"; } - - @Override - protected boolean shouldGenerateId() { - return false; - } - - @Override - protected boolean shouldGenerateIdAsFallback() { - return true; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - String connectionName = element.getAttribute("xmpp-connection"); - builder.addConstructorArgReference(connectionName); - IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel"); - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup"); - } - } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceOutboundChannelAdapterParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceOutboundChannelAdapterParser.java index d8a36f2a2c..aff8727702 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceOutboundChannelAdapterParser.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/PresenceOutboundChannelAdapterParser.java @@ -16,11 +16,6 @@ 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.w3c.dom.Element; /** * Parser for 'xmpp:presence-outbound-channel-adapter' element @@ -28,15 +23,11 @@ import org.w3c.dom.Element; * @author Oleg Zhurakousky * @since 2.0 */ -public class PresenceOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { +public class PresenceOutboundChannelAdapterParser extends AbstractXmppOutboundChannelAdapterParser { @Override - protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( - "org.springframework.integration.xmpp.outbound.PresenceSendingMessageHandler"); - String connectionName = element.getAttribute("xmpp-connection"); - builder.addConstructorArgReference(connectionName); - return builder.getBeanDefinition(); + protected String getHandlerClassName() { + return "org.springframework.integration.xmpp.outbound.PresenceSendingMessageHandler"; } } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppConnectionParser.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppConnectionParser.java index 60494b1ce2..65b21497d0 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppConnectionParser.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppConnectionParser.java @@ -16,13 +16,14 @@ package org.springframework.integration.xmpp.config; +import org.w3c.dom.Element; + import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import org.w3c.dom.Element; /** * Parser for 'xmpp:xmpp-connection' element @@ -42,7 +43,7 @@ public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser { @Override protected boolean shouldGenerateIdAsFallback() { - return true; + return false; } @Override 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 cf940ad7e3..8b3eaab265 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 @@ -27,6 +27,8 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport; * @since 2.0 */ public class XmppNamespaceHandler extends NamespaceHandlerSupport { + + public final static String XMPP_CONNECTION_BEAN_NAME = "xmppConnection"; public void init() { // connection 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 faf098f56f..4ef1a17b71 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 @@ -20,11 +20,11 @@ - + - The user name (e.g., somuser@gmail.com) that will be used by this connection object + The user name (e.g., someuser@gmail.com) that will be used by this connection object @@ -174,7 +174,7 @@ - + @@ -207,7 +207,7 @@ - + diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests-simple.xml b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests-simple.xml index 0a25528a65..b600d35916 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests-simple.xml +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests-simple.xml @@ -9,5 +9,8 @@ + + + diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests.java index f923aea46a..dfe537ba1c 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/XmppConnectionParserTests.java @@ -19,6 +19,7 @@ package org.springframework.integration.xmpp.config; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; import java.util.List; @@ -62,6 +63,12 @@ public class XmppConnectionParserTests { assertEquals("localhost", configuration.getHost()); assertEquals(5222, configuration.getPort()); } + + @Test + public void testDefaultConnectionName() { + ApplicationContext ac = new ClassPathXmlApplicationContext("XmppConnectionParserTests-simple.xml", this.getClass()); + assertTrue(ac.containsBean("xmppConnection")); + } @Test public void testCompleteConfiguration() { diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTest-context.xml b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTest-context.xml index 50d29ee023..198f509bb2 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTest-context.xml +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTest-context.xml @@ -12,7 +12,6 @@ - +