INT-1554 polishing XmppMessageDrivenEndpoint, uts parser and tests

This commit is contained in:
Oleg Zhurakousky
2010-11-04 13:51:36 -04:00
parent acf9d2bc13
commit f07dc4a68f
3 changed files with 19 additions and 40 deletions

View File

@@ -19,10 +19,11 @@ 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.w3c.dom.Element;
/**
* Parser for 'message-inbound-channel-adapter' element
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0
@@ -42,8 +43,7 @@ public class XmppMessageInboundEndpointParser extends AbstractSingleBeanDefiniti
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String connectionName = element.getAttribute("xmpp-connection");
Assert.hasText(connectionName, "'xmpp-connection' must be defined");
builder.addPropertyReference("xmppConnection", connectionName);
builder.addConstructorArgReference(connectionName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");

View File

@@ -16,8 +16,6 @@
package org.springframework.integration.xmpp.messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.PacketListener;
@@ -33,48 +31,35 @@ import org.springframework.util.Assert;
/**
* This component logs in as a user and forwards any messages <em>to</em> that
* user on to downstream components. The component is an endpoint that has its
* own lifecycle and does not need any poller
* to work. It takes any message from a given XMPP session (as established by
* user on to downstream components.
* It takes any message from a given XMPP session (as established by
* the current {@link XMPPConnection}) and forwards the
* {@link org.jivesoftware.smack.packet.Message} as the payload of the Spring
* Integration {@link org.springframework.integration.Message}. The
* {@link org.jivesoftware.smack.Chat} instance that's used is passed along as a
* header (under {@link org.springframework.integration.xmpp.XmppHeaders#CHAT}).
* Additionally, the {@link org.jivesoftware.smack.packet.Message.Type} is
* passed along under the header
* {@link org.springframework.integration.xmpp.XmppHeaders#TYPE}. Both of these
* pieces of metadata can be obtained directly from the payload, if required.
* They are here as a convenience.
* <p/>
* Integration {@link org.springframework.integration.Message}.
* <strong>Note</strong>: the {@link org.jivesoftware.smack.ChatManager}
* maintains a Map&lt;String, Chat&gt; for threads and users, where the threadID
* ({@link String}) is the key or the userID {@link String} is the key. This
* {@link java.util.Map} is a Smack-specific implementation called
* {@link org.jivesoftware.smack.util.collections.ReferenceMap} that removes
* key/values as references are dereferenced. Take care to enable this garbage
* collection, taking what you need from the payload and the headers and
* discarding as soon as possible.
* key/values as references are dereferenced.
*
* @author Josh Long
* @author Mark Fisher
* @author Oleg Zhurakousky
*
* @see ChatManager the ChatManager class that
* keeps watch over all Chats between the client and any other
* participants.
* @see MessagingTemplate
* handles all interesing operations on any Spring Integration channels.
* @see XMPPConnection the XMPPConnection (as
* created by {@link XmppConnectionFactory}
*/
public class XmppMessageDrivenEndpoint extends AbstractEndpoint {
private static final Log logger = LogFactory.getLog(XmppMessageDrivenEndpoint.class);
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private volatile MessageChannel requestChannel;
private volatile XMPPConnection xmppConnection;
private final XMPPConnection xmppConnection;
private volatile boolean extractPayload = true;
@@ -82,24 +67,17 @@ public class XmppMessageDrivenEndpoint extends AbstractEndpoint {
private volatile boolean initialized;
/**
* This will be injected or configured via a <em>xmpp-connection-factory</em> element.
*
* @param xmppConnection the connection
*/
public void setXmppConnection(final XMPPConnection xmppConnection) {
public XmppMessageDrivenEndpoint(XMPPConnection xmppConnection){
this.xmppConnection = xmppConnection;
}
/**
* @param requestChannel the channel on which the inbound message should be sent
*/
public void setRequestChannel(final MessageChannel requestChannel) {
this.messagingTemplate.setDefaultChannel(requestChannel);
public void setRequestChannel(MessageChannel requestChannel) {
this.requestChannel = requestChannel;
}
/**
* Specify whether the text message body should be extracted when mapping to a
* Spring Integration Message payload. Otherwise, the full XMPP Message will be
@@ -122,7 +100,8 @@ public class XmppMessageDrivenEndpoint extends AbstractEndpoint {
@Override
protected void onInit() throws Exception {
messagingTemplate.afterPropertiesSet();
this.messagingTemplate.setDefaultChannel(requestChannel);
this.messagingTemplate.afterPropertiesSet();
this.packetListener = new PacketListener() {
public void processPacket(final Packet packet) {
org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message) packet;

View File

@@ -43,9 +43,10 @@ public class XmppMessageDrivenEndpointTests {
*/
public void testLifecycle(){
final Set<PacketListener> packetListSet = new HashSet<PacketListener>();
XmppMessageDrivenEndpoint endpoint = new XmppMessageDrivenEndpoint();
XMPPConnection connection = mock(XMPPConnection.class);
XmppMessageDrivenEndpoint endpoint = new XmppMessageDrivenEndpoint(connection);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@@ -62,7 +63,6 @@ public class XmppMessageDrivenEndpointTests {
}
}).when(connection).removePacketListener(Mockito.any(PacketListener.class));
endpoint.setXmppConnection(connection);
assertEquals(0, packetListSet.size());
endpoint.afterPropertiesSet();
endpoint.start();
@@ -73,7 +73,7 @@ public class XmppMessageDrivenEndpointTests {
@Test(expected=IllegalArgumentException.class)
public void testNonInitializationFailure(){
XmppMessageDrivenEndpoint endpoint = new XmppMessageDrivenEndpoint();
XmppMessageDrivenEndpoint endpoint = new XmppMessageDrivenEndpoint(mock(XMPPConnection.class));
endpoint.start();
}
}