INT-1554 refactored XmppRosterEventOutboundEndpointParser, cleaned up XmppRosterEventMessageSendingHandler, added tests

This commit is contained in:
Oleg Zhurakousky
2010-11-04 12:31:45 -04:00
parent e20a929d1d
commit 8d1e5e3f1d
8 changed files with 215 additions and 60 deletions

View File

@@ -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");
}
}

View File

@@ -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();
}
}

View File

@@ -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<Presence> 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);
}
}

View File

@@ -55,7 +55,19 @@
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="message-mapper" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.mapping.OutboundMessageMapper"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="channel" use="required" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp-2.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>
</bean>
<int:channel id="pollingChannel">
<int:queue/>
</int:channel>
<int-xmpp:roster-event-outbound-channel-adapter id="pollingOutboundRosterAdapter"
xmpp-connection="testConnection"
channel="pollingChannel">
<int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-xmpp:roster-event-outbound-channel-adapter>
<int:channel id="eventChannel"/>
<int-xmpp:roster-event-outbound-channel-adapter id="eventOutboundRosterAdapter"
xmpp-connection="testConnection"
channel="eventChannel"
message-mapper="mockMappper"/>
<bean id="mockMappper" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.mapping.OutboundMessageMapper"/>
</bean>
</beans>

View File

@@ -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);
}
}

View File

@@ -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}"
/>

View File

@@ -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 {
}