INT-1651 added support for default XMPP connection name and auto-discovery of that name by the XMPP adapters
This commit is contained in:
@@ -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., <int-xmpp:xmpp-connection .../>). 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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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., <int-xmpp:xmpp-connection .../>). If 'id' is not provided the default will be 'xmppConnection'.");
|
||||
}
|
||||
|
||||
return builder.getBeanDefinition();
|
||||
}
|
||||
|
||||
protected abstract String getHandlerClassName();
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="id" type="xsd:string" default="xmppConnection"/>
|
||||
<xsd:attribute name="user" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
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
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
@@ -174,7 +174,7 @@
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="xmpp-connection" type="xsd:string" use="required">
|
||||
<xsd:attribute name="xmpp-connection" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
@@ -207,7 +207,7 @@
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="xmpp-connection" type="xsd:string" use="required">
|
||||
<xsd:attribute name="xmpp-connection" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
|
||||
@@ -9,5 +9,8 @@
|
||||
|
||||
<int-xmpp:xmpp-connection id="connection"
|
||||
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
|
||||
|
||||
|
||||
<int-xmpp:xmpp-connection user="happy.user" password="blah" host="localhost" auto-startup="false"/>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
<context:property-placeholder location="classpath:test.properties"/>
|
||||
|
||||
<int-xmpp:xmpp-connection
|
||||
id="testConnection"
|
||||
user="${user.1.login}"
|
||||
password="${user.1.password}"
|
||||
host="${user.1.host}"
|
||||
@@ -20,6 +19,6 @@
|
||||
|
||||
<int:channel id="xmppInput"/>
|
||||
|
||||
<int-xmpp:outbound-channel-adapter channel="xmppInput" xmpp-connection="testConnection"/>
|
||||
<int-xmpp:outbound-channel-adapter channel="xmppInput"/>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user