RosterListeningEndpoint now only sends Presence changes

This commit is contained in:
Mark Fisher
2010-11-15 10:45:11 -05:00
parent 55d73216f8
commit b6f6541447
2 changed files with 14 additions and 56 deletions

View File

@@ -29,10 +29,7 @@ import org.jivesoftware.smack.packet.Presence;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.MessagingException;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint;
import org.springframework.util.Assert;
@@ -49,8 +46,6 @@ public class RosterListeningEndpoint extends AbstractXmppConnectionAwareEndpoint
private static final Log logger = LogFactory.getLog(RosterListeningEndpoint.class);
private volatile MessageChannel requestChannel;
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private final EventForwardingRosterListener rosterListener = new EventForwardingRosterListener();
@@ -69,7 +64,7 @@ public class RosterListeningEndpoint extends AbstractXmppConnectionAwareEndpoint
* @param requestChannel the channel on which the inbound message should be sent
*/
public void setRequestChannel(final MessageChannel requestChannel) {
this.requestChannel = requestChannel;
this.messagingTemplate.setDefaultChannel(requestChannel);
}
@Override
@@ -86,51 +81,32 @@ public class RosterListeningEndpoint extends AbstractXmppConnectionAwareEndpoint
@Override
protected void onInit() throws Exception {
super.onInit();
this.messagingTemplate.setDefaultChannel(requestChannel);
this.messagingTemplate.afterPropertiesSet();
}
/**
* Called whenever an event happens related to the {@link Roster}.
*/
private void forwardRosterEvent(Object event) {
Message<?> message = null;
try {
message = MessageBuilder.withPayload(event).build();
this.messagingTemplate.send(this.requestChannel, message);
}
catch (MessagingException e) {
throw e;
}
catch (Exception e) {
throw new MessageHandlingException(message, "Failed to send roster event message", e);
}
}
/**
* RosterListener that subscribes to a given {@link Roster}'s events
* and forwards them to a message channel.
* RosterListener that subscribes to a given {@link Roster}'s events.
* Presence changes will be forwarded to a message channel.
* All others are only logged at debug level.
*/
private class EventForwardingRosterListener implements RosterListener {
public void entriesAdded(Collection<String> entries) {
logger.debug("entries added: " + StringUtils.join(entries.iterator(), ","));
forwardRosterEvent(entries);
}
public void entriesUpdated(Collection<String> entries) {
logger.debug("entries updated: " + StringUtils.join(entries.iterator(), ","));
forwardRosterEvent(entries);
}
public void entriesDeleted(Collection<String> entries) {
logger.debug("entries deleted: " + StringUtils.join(entries.iterator(), ","));
forwardRosterEvent(entries);
}
public void presenceChanged(Presence presence) {
logger.debug("presence changed: " + ToStringBuilder.reflectionToString(presence));
forwardRosterEvent(presence);
messagingTemplate.convertAndSend(presence);
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xmpp.inbound;
import static junit.framework.Assert.assertEquals;
@@ -21,9 +22,7 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jivesoftware.smack.Roster;
@@ -36,21 +35,20 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.core.XmppContextUtils;
import org.springframework.integration.xmpp.inbound.RosterListeningEndpoint;
/**
* @author Oleg Zhurakousky
*
*/
public class XmppRosterListeningEndpointTests {
@Test
public void testEndpointLifecycle(){
public void testEndpointLifecycle() {
final Set<RosterListener> rosterSet = new HashSet<RosterListener>();
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
@@ -79,13 +77,13 @@ public class XmppRosterListeningEndpointTests {
}
@Test(expected=IllegalArgumentException.class)
public void testNonInitializedFailure(){
public void testNonInitializedFailure() {
RosterListeningEndpoint rosterEndpoint = new RosterListeningEndpoint(mock(XMPPConnection.class));
rosterEndpoint.start();
}
@Test
public void testRosterPresenceChangeEvent(){
public void testRosterPresenceChangeEvent() {
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
when(connection.getRoster()).thenReturn(roster);
@@ -100,26 +98,9 @@ public class XmppRosterListeningEndpointTests {
Message<?> message = channel.receive(10);
assertEquals(presence, message.getPayload());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRosterEntriesEvents(){
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
when(connection.getRoster()).thenReturn(roster);
RosterListeningEndpoint rosterEndpoint = new RosterListeningEndpoint(connection);
QueueChannel channel = new QueueChannel();
rosterEndpoint.setRequestChannel(channel);
rosterEndpoint.afterPropertiesSet();
rosterEndpoint.start();
RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener");
List entries = Arrays.asList(new String[]{"many", "moe", "jack"});
rosterListener.entriesUpdated(entries);
Message<?> message = channel.receive(10);
assertEquals(entries, message.getPayload());
}
@Test
public void testWithImplicitXmppConnection(){
public void testWithImplicitXmppConnection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
RosterListeningEndpoint endpoint = new RosterListeningEndpoint();
@@ -129,8 +110,9 @@ public class XmppRosterListeningEndpointTests {
}
@Test(expected=IllegalArgumentException.class)
public void testNoXmppConnection(){
public void testNoXmppConnection() {
RosterListeningEndpoint handler = new RosterListeningEndpoint();
handler.afterPropertiesSet();
}
}