From 6a7cfee022ed2f53ef3428e1f657953c9b3fa694 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 15 Nov 2010 15:45:49 -0500 Subject: [PATCH] polishing --- .../AbstractXmppConnectionAwareEndpoint.java | 12 ++--- .../inbound/ChatMessageListeningEndpoint.java | 47 +++++++++---------- .../inbound/PresenceListeningEndpoint.java | 37 ++++++++++----- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/core/AbstractXmppConnectionAwareEndpoint.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/core/AbstractXmppConnectionAwareEndpoint.java index cf4b1c9ddf..6a87e260b1 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/core/AbstractXmppConnectionAwareEndpoint.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/core/AbstractXmppConnectionAwareEndpoint.java @@ -42,13 +42,13 @@ public abstract class AbstractXmppConnectionAwareEndpoint extends AbstractEndpoi protected void onInit() throws Exception { - BeanFactory bf = this.getBeanFactory(); - if (xmppConnection == null && bf != null) { - xmppConnection = bf.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class); + BeanFactory beanFactory = this.getBeanFactory(); + if (this.xmppConnection == null && beanFactory != null) { + this.xmppConnection = beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class); } - Assert.notNull(xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set expicitly " + - "via 'xmpp-connection' attribute or implicitly by registering a bean with the name 'xmppConnection' and of type " + - "'org.jivesoftware.smack.XMPPConnection' in the Application Context"); + Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set expicitly " + + "via the 'xmpp-connection' attribute or implicitly by registering a bean with the name 'xmppConnection' and of type " + + "'org.jivesoftware.smack.XMPPConnection' in the Application Context."); this.initialized = true; } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java index c44f17aa33..d2ef371f7e 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java @@ -42,14 +42,12 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd private final MessagingTemplate messagingTemplate = new MessagingTemplate(); - private volatile MessageChannel requestChannel; - private volatile boolean extractPayload = true; - - private volatile PacketListener packetListener; + + private final PacketListener packetListener = new ChatMessagePublishingPacketListener(); - public ChatMessageListeningEndpoint(){ + public ChatMessageListeningEndpoint() { super(); } @@ -62,7 +60,7 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd * @param requestChannel the channel on which the inbound message should be sent */ public void setRequestChannel(MessageChannel requestChannel) { - this.requestChannel = requestChannel; + this.messagingTemplate.setDefaultChannel(requestChannel); } /** @@ -77,35 +75,36 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd @Override protected void onInit() throws Exception { super.onInit(); - this.messagingTemplate.setDefaultChannel(requestChannel); this.messagingTemplate.afterPropertiesSet(); - this.packetListener = new PacketListener() { - public void processPacket(final Packet packet) { - if (packet instanceof org.jivesoftware.smack.packet.Message) { - org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet; - forwardXmppMessage(xmppConnection.getChatManager().getThreadChat(xmppMessage.getThread()), xmppMessage); - } - } - }; } @Override protected void doStart() { - Assert.isTrue(this.initialized, this.getComponentName() + " must be initialized"); - xmppConnection.addPacketListener(this.packetListener, null); + Assert.isTrue(this.initialized, this.getComponentName() + " [" + this.getComponentType() + "] must be initialized"); + this.xmppConnection.addPacketListener(this.packetListener, null); } @Override protected void doStop() { - xmppConnection.removePacketListener(this.packetListener); + if (this.xmppConnection != null) { + this.xmppConnection.removePacketListener(this.packetListener); + } } - private void forwardXmppMessage(Chat chat, Message xmppMessage) { - Object payload = (this.extractPayload ? xmppMessage.getBody() : xmppMessage); - MessageBuilder messageBuilder = MessageBuilder.withPayload(payload) - .setHeader(XmppHeaders.TYPE, xmppMessage.getType()) - .setHeader(XmppHeaders.CHAT, chat); - this.messagingTemplate.send(requestChannel, messageBuilder.build()); + + private class ChatMessagePublishingPacketListener implements PacketListener { + + public void processPacket(final Packet packet) { + if (packet instanceof org.jivesoftware.smack.packet.Message) { + org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet; + Chat chat = xmppConnection.getChatManager().getThreadChat(xmppMessage.getThread()); + Object payload = (extractPayload ? xmppMessage.getBody() : xmppMessage); + MessageBuilder messageBuilder = MessageBuilder.withPayload(payload) + .setHeader(XmppHeaders.TYPE, xmppMessage.getType()) + .setHeader(XmppHeaders.CHAT, chat); + messagingTemplate.send(messageBuilder.build()); + } + } } } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpoint.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpoint.java index f4b65b7e94..e7919d576d 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpoint.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpoint.java @@ -18,10 +18,9 @@ package org.springframework.integration.xmpp.inbound; import java.util.Collection; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.RosterListener; import org.jivesoftware.smack.XMPPConnection; @@ -32,10 +31,11 @@ import org.springframework.integration.MessageChannel; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** - * Describes an inbound endpoint that is able to login and then emit {@link Message}s when a - * particular Presence event happens to the logged in user's {@link Roster}. + * An inbound endpoint that is able to login and then emit {@link Message}s when a + * particular Presence event occurs within the logged-in user's {@link Roster}. * (e.g., logged in/out, changed status etc.) * * @author Josh Long @@ -46,6 +46,7 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi private static final Log logger = LogFactory.getLog(PresenceListeningEndpoint.class); + private final MessagingTemplate messagingTemplate = new MessagingTemplate(); private final EventForwardingRosterListener rosterListener = new EventForwardingRosterListener(); @@ -69,14 +70,16 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi @Override protected void doStart() { - Assert.isTrue(this.initialized, this.getComponentName() + "#" + this.getComponentType() + " must be initialized"); + Assert.isTrue(this.initialized, this.getComponentName() + " [" + this.getComponentType() + "] must be initialized"); Roster roster = this.xmppConnection.getRoster(); - roster.addRosterListener(rosterListener); + roster.addRosterListener(this.rosterListener); } @Override protected void doStop() { - this.xmppConnection.getRoster().removeRosterListener(rosterListener); + if (this.xmppConnection != null) { + this.xmppConnection.getRoster().removeRosterListener(this.rosterListener); + } } @Override @@ -94,20 +97,30 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi private class EventForwardingRosterListener implements RosterListener { public void entriesAdded(Collection entries) { - logger.debug("entries added: " + StringUtils.join(entries.iterator(), ",")); + if (logger.isDebugEnabled()) { + logger.debug("entries added: " + StringUtils.collectionToCommaDelimitedString(entries)); + } } public void entriesUpdated(Collection entries) { - logger.debug("entries updated: " + StringUtils.join(entries.iterator(), ",")); + if (logger.isDebugEnabled()) { + logger.debug("entries updated: " + StringUtils.collectionToCommaDelimitedString(entries)); + } } public void entriesDeleted(Collection entries) { - logger.debug("entries deleted: " + StringUtils.join(entries.iterator(), ",")); + if (logger.isDebugEnabled()) { + logger.debug("entries deleted: " + StringUtils.collectionToCommaDelimitedString(entries)); + } } public void presenceChanged(Presence presence) { - logger.debug("presence changed: " + presence.getFrom() + " - " + presence); - messagingTemplate.convertAndSend(presence); + if (presence != null) { + if (logger.isDebugEnabled()) { + logger.debug("presence changed: " + presence.getFrom() + " - " + presence); + } + messagingTemplate.convertAndSend(presence); + } } }