incubator-nuvem-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r1104607 - in /incubator/nuvem/trunk: nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/ nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/ nuvem-google/ nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/ nuvem-standa...
Date Tue, 17 May 2011 22:30:37 GMT
Author: johnp
Date: Tue May 17 22:30:37 2011
New Revision: 1104607

URL: http://svn.apache.org/viewvc?rev=1104607&view=rev
Log:
Added necessary APIs to all XMPP clients to receive presence updates from the XMPP server

Added:
    incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceFilter.java
    incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceSubscriptionListener.java
    incubator/nuvem/trunk/nuvem-standalone/src/test/java/org/apache/nuvem/cloud/xmpp/client/XMPPPresenceIntegrationTest.java
Removed:
    incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/AbstractPresenceManager.java
Modified:
    incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/PresenceManager.java
    incubator/nuvem/trunk/nuvem-google/pom.xml
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceManager.java
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceServlet.java
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/PresenceAdapter.java
    incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultPresenceManager.java
    incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultXMPPEndPoint.java
    incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceAdapter.java

Modified: incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/PresenceManager.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/PresenceManager.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/PresenceManager.java
(original)
+++ incubator/nuvem/trunk/nuvem-api/src/main/java/org/apache/nuvem/cloud/xmpp/presence/PresenceManager.java
Tue May 17 22:30:37 2011
@@ -29,8 +29,26 @@ import java.util.List;
  */
 public interface PresenceManager {
 
+	/**
+	 * Registers a presence listener to recieve notification of any presence
+	 * message recieved from the XMPP server.
+	 * 
+	 * @param listener
+	 */
 	void registerListener(PresenceListener listener);
 
+	/**
+	 * Returns a list of all {@link PresenceListener}s waiting to receive
+	 * presence stanzas.
+	 * 
+	 * @return
+	 */
 	List<PresenceListener> listeners();
+	
+	/**
+	 * Sends the presence stanza to the XMPP server.
+	 * @param presence
+	 */
+	void sendPresence(Presence presence);
 
 }

Modified: incubator/nuvem/trunk/nuvem-google/pom.xml
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/pom.xml?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/pom.xml (original)
+++ incubator/nuvem/trunk/nuvem-google/pom.xml Tue May 17 22:30:37 2011
@@ -96,6 +96,12 @@
 		</dependency>
 
         <dependency>
+            <groupId>commons-collections</groupId>
+            <artifactId>commons-collections</artifactId>
+            <version>3.2.1</version>
+        </dependency>
+
+        <dependency>
     		<groupId>javax.servlet</groupId>
     		<artifactId>servlet-api</artifactId>
     		<version>2.5</version>

Modified: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceManager.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceManager.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceManager.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceManager.java
Tue May 17 22:30:37 2011
@@ -20,21 +20,29 @@
 
 package org.apache.nuvem.cloud.xmpp.impl;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.logging.Logger;
 
 import org.apache.commons.lang.Validate;
-import org.apache.nuvem.cloud.xmpp.AbstractPresenceManager;
 import org.apache.nuvem.cloud.xmpp.XMPPConnector;
+import org.apache.nuvem.cloud.xmpp.presence.Presence;
 import org.apache.nuvem.cloud.xmpp.presence.PresenceListener;
 import org.apache.nuvem.cloud.xmpp.presence.PresenceManager;
 import org.oasisopen.sca.annotation.Init;
 import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
 import org.oasisopen.sca.annotation.Service;
 
+import com.google.appengine.api.xmpp.JID;
+import com.google.appengine.api.xmpp.PresenceShow;
+import com.google.appengine.api.xmpp.PresenceType;
 import com.google.appengine.api.xmpp.XMPPService;
 
 @Service(PresenceManager.class)
-public class GooglePresenceManager extends AbstractPresenceManager {
+@Scope("COMPOSITE")
+public class GooglePresenceManager implements PresenceManager {
 
 	/**
 	 * Logger.
@@ -42,13 +50,19 @@ public class GooglePresenceManager exten
 	private static final Logger log = Logger.getLogger(PresenceManager.class
 			.getName());
 
+	/**
+	 * List of all presence listeners.
+	 */
+	private List<PresenceListener> listeners = new ArrayList<PresenceListener>();
+	
 	@Reference(required = false)
 	protected XMPPConnector<XMPPService> connector;
 
 	@Init
 	public void init() {
 		if (connector == null) {
-			log.info("Google Presence manager is getting initialized with a default connector...");
+			log
+					.info("Google Presence manager is getting initialized with a default connector...");
 			connector = new GoogleXMPPConnector();
 		}
 	}
@@ -59,4 +73,24 @@ public class GooglePresenceManager exten
 		listeners.add(listener);
 	}
 
+	public void clearListeners() {
+		listeners.clear();
+	}
+
+	public List<PresenceListener> listeners() {
+		return Collections.unmodifiableList(listeners);
+	}
+
+	@Override
+	public void sendPresence(Presence presence) {
+		JID to = new JID(presence.to().asString());
+		PresenceType type = PresenceAdapter.toGooglePresenceType(presence
+				.type());
+		PresenceShow show = PresenceAdapter.toGooglePresenceShow(presence
+				.show());
+		String status = presence.status();
+		connector.getConnection().sendPresence(to, type, show, status);
+
+	}
+
 }

Modified: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceServlet.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceServlet.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceServlet.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GooglePresenceServlet.java
Tue May 17 22:30:37 2011
@@ -39,7 +39,7 @@ import com.google.appengine.api.xmpp.XMP
  * A servlet to recieve XMPP presence change requests from google cloud
  * platform.
  * <p>
- * This servlet will recieve the presence change requests posted in the HTTP
+ * This servlet will receive the presence change requests posted in the HTTP
  * POST request, parse the message using the APIs provided by GAE convert the
  * request into nuvem specific presence object so that the
  * {@link PresenceListener#listen(org.apache.nuvem.cloud.xmpp.presence.Presence)}

Modified: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/PresenceAdapter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/PresenceAdapter.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/PresenceAdapter.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/PresenceAdapter.java
Tue May 17 22:30:37 2011
@@ -22,6 +22,8 @@ package org.apache.nuvem.cloud.xmpp.impl
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.collections.BidiMap;
+import org.apache.commons.collections.bidimap.DualHashBidiMap;
 import org.apache.commons.lang.StringUtils;
 import org.apache.nuvem.cloud.xmpp.JID;
 import org.apache.nuvem.cloud.xmpp.presence.Presence;
@@ -37,8 +39,17 @@ import com.google.appengine.api.xmpp.Pre
  */
 public final class PresenceAdapter {
 
-	private static final Map<PresenceShow, Presence.Show> showConverstionMap = new HashMap<PresenceShow,
Show>();
-	private static final Map<PresenceType, Presence.Type> typeConversionMap = new HashMap<PresenceType,
Type>();
+	/**
+	 * A bidirectional converstion map with key as google presence show and
+	 * value as nuvem's show value.
+	 */
+	private static final BidiMap showConverstionMap = new DualHashBidiMap();
+
+	/**
+	 * A bidirectional converstion map with key as google's presence type value
+	 * and value as nuvem's presence type value.F
+	 */
+	private static final BidiMap typeConverstionMap = new DualHashBidiMap();
 
 	static {
 		showConverstionMap.put(PresenceShow.AWAY, Show.AWAY);
@@ -47,9 +58,12 @@ public final class PresenceAdapter {
 		showConverstionMap.put(PresenceShow.XA, Show.XA);
 		showConverstionMap.put(PresenceShow.NONE, Show.NONE);
 
-		typeConversionMap.put(PresenceType.AVAILABLE, Type.AVAILABLE);
-		typeConversionMap.put(PresenceType.PROBE, Type.PROBE);
-		typeConversionMap.put(PresenceType.UNAVAILABLE, Type.UNAVAILABLE);
+		typeConverstionMap.put(PresenceType.AVAILABLE,
+				Type.AVAILABLE);
+		typeConverstionMap.put(PresenceType.PROBE, Type.PROBE);
+		typeConverstionMap.put(PresenceType.UNAVAILABLE,
+				Type.UNAVAILABLE);
+
 	}
 
 	/**
@@ -59,8 +73,10 @@ public final class PresenceAdapter {
 	 */
 	public static Presence toNuvemPresence(
 			com.google.appengine.api.xmpp.Presence presence) {
-		Show show = showConverstionMap.get(presence.getPresenceShow());
-		Type type = typeConversionMap.get(presence.getPresenceType());
+		Show show = (Show) showConverstionMap.get(presence
+				.getPresenceShow());
+		Type type = (Type) typeConverstionMap.get(presence
+				.getPresenceType());
 		String status = StringUtils.defaultString(presence.getStatus());
 		String stanza = StringUtils.defaultString(presence.getStanza());
 
@@ -72,4 +88,22 @@ public final class PresenceAdapter {
 		}
 		return builder.build();
 	}
+
+	public static Presence.Type toNuvemPresenceType(
+			com.google.appengine.api.xmpp.PresenceType type) {
+		return (Presence.Type) typeConverstionMap.get(type);
+	}
+
+	public static PresenceType toGooglePresenceType(Presence.Type type) {
+		return (PresenceType) typeConverstionMap.getKey(type);
+	}
+
+	public static Presence.Show toNuvemPresenceShow(
+			com.google.appengine.api.xmpp.PresenceShow show) {
+		return (Presence.Show) showConverstionMap.get(show);
+	}
+
+	public static PresenceShow toGooglePresenceShow(Presence.Show show) {
+		return (PresenceShow) showConverstionMap.getKey(show);
+	}
 }

Modified: incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultPresenceManager.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultPresenceManager.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultPresenceManager.java
(original)
+++ incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultPresenceManager.java
Tue May 17 22:30:37 2011
@@ -20,13 +20,20 @@
 
 package org.apache.nuvem.cloud.xmpp.client;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+
 import org.apache.commons.lang.Validate;
 import org.apache.nuvem.cloud.xmpp.XMPPConnector;
+import org.apache.nuvem.cloud.xmpp.presence.Presence;
 import org.apache.nuvem.cloud.xmpp.presence.PresenceListener;
 import org.apache.nuvem.cloud.xmpp.presence.PresenceManager;
-import org.apache.nuvem.cloud.xmpp.AbstractPresenceManager;
 import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.Roster.SubscriptionMode;
+import org.oasisopen.sca.annotation.Init;
 import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
 import org.oasisopen.sca.annotation.Service;
 
 /**
@@ -34,7 +41,19 @@ import org.oasisopen.sca.annotation.Serv
  * 
  */
 @Service(PresenceManager.class)
-public class DefaultPresenceManager extends AbstractPresenceManager {
+@Scope("COMPOSITE")
+public class DefaultPresenceManager implements PresenceManager {
+
+	/**
+	 * A list of all listeners listening for any presence messages.
+	 */
+	private List<PresenceListener> listeners = new ArrayList<PresenceListener>();
+
+	/**
+	 * Logger.
+	 */
+	private static final Logger log = Logger.getLogger(PresenceManager.class
+			.getName());
 
 	/**
 	 * Connecter to fetch XMPP Connection.
@@ -42,6 +61,14 @@ public class DefaultPresenceManager exte
 	@Reference
 	protected XMPPConnector<XMPPConnection> connector;
 
+	@Init
+	public void init() {
+		log.info("Initializing PresenceManager using connector "
+				+ connector.getDescription());
+		connector.getConnection().getRoster().setSubscriptionMode(
+				SubscriptionMode.manual);
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */
@@ -50,8 +77,24 @@ public class DefaultPresenceManager exte
 		Validate.notNull(listener);
 		connector.getConnection().getRoster().addRosterListener(
 				new RosterListenerAdapter(listener));
-		//Keeps a reference to the listener for later use.
+		// Keeps a reference to the listener for later use.
 		listeners.add(listener);
 	}
 
+	public void clearListeners() {
+		listeners.clear();
+	}
+
+	public List<PresenceListener> listeners() {
+		List<PresenceListener> listeners = new ArrayList<PresenceListener>();
+		listeners.addAll(this.listeners);
+		return listeners;
+	}
+
+	@Override
+	public void sendPresence(Presence presence) {
+		connector.getConnection().sendPacket(
+				PresenceAdapter.toSmackPresence(presence));
+	}
+
 }

Modified: incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultXMPPEndPoint.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultXMPPEndPoint.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultXMPPEndPoint.java
(original)
+++ incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/DefaultXMPPEndPoint.java
Tue May 17 22:30:37 2011
@@ -61,9 +61,8 @@ public class DefaultXMPPEndPoint extends
 	protected XMPPConnector<XMPPConnection> connector;
 
 	@Reference
-	protected PresenceManager presenceManager; 
-	
-	
+	protected PresenceManager presenceManager;
+
 	/**
 	 * The packet filter to decide what packets to recieve from the XMPP
 	 * Server..
@@ -94,6 +93,9 @@ public class DefaultXMPPEndPoint extends
 		filter = new NuvemPacketFilter(this);
 		listener = new NuvemPacketListener(this);
 		connector.getConnection().addPacketListener(this.listener, filter);
+		connector.getConnection().addPacketListener(
+				new PresenceSubscriptionListener(presenceManager),
+				new PresenceFilter(Type.subscribe));
 	}
 
 	/**

Modified: incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceAdapter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceAdapter.java?rev=1104607&r1=1104606&r2=1104607&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceAdapter.java
(original)
+++ incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceAdapter.java
Tue May 17 22:30:37 2011
@@ -19,9 +19,8 @@
 
 package org.apache.nuvem.cloud.xmpp.client;
 
-import java.util.HashMap;
-import java.util.Map;
-
+import org.apache.commons.collections.BidiMap;
+import org.apache.commons.collections.bidimap.DualHashBidiMap;
 import org.apache.commons.lang.StringUtils;
 import org.apache.nuvem.cloud.xmpp.JID;
 import org.apache.nuvem.cloud.xmpp.presence.Presence;
@@ -37,8 +36,17 @@ import org.jivesoftware.smack.packet.Pre
  */
 public final class PresenceAdapter {
 
-	private static final Map<Mode, Presence.Show> showConverstionMap = new HashMap<Mode,
Show>();
-	private static final Map<org.jivesoftware.smack.packet.Presence.Type, Presence.Type>
typeConversionMap = new HashMap<org.jivesoftware.smack.packet.Presence.Type, Type>();
+	/**
+	 * A bidirectional map with key being smack's mode and value being nuvem's
+	 * show values respectively.
+	 */
+	private static final BidiMap showConverstionMap = new DualHashBidiMap();
+
+	/**
+	 * A bidirectional map with key being smack's type and value being nuvems
+	 * presence types respectively.
+	 */
+	private static final BidiMap typeConversionMap = new DualHashBidiMap();
 
 	static {
 		showConverstionMap.put(Mode.available, Show.AVAILABLE);
@@ -78,8 +86,8 @@ public final class PresenceAdapter {
 		String from = presence.getFrom();
 		String to = presence.getTo();
 		String status = presence.getStatus();
-		Show show = showConverstionMap.get(presence.getMode());
-		Type type = typeConversionMap.get(presence.getType());
+		Show show = (Show) showConverstionMap.get(presence.getMode());
+		Type type = (Type) typeConversionMap.get(presence.getType());
 		PresenceBuilder builder = new PresenceBuilder().withShow(show)
 				.withType(type).withStatus(status);
 		builder.from(new JID(from));
@@ -88,4 +96,28 @@ public final class PresenceAdapter {
 		}
 		return builder.build();
 	}
+
+	/**
+	 * Converts Nuvem's {@link org.apache.nuvem.cloud.xmpp.presence.Presence} to
+	 * smack's {@link org.jivesoftware.smack.packet.Presence} object.
+	 * 
+	 * @param presence
+	 * @return
+	 */
+	public static org.jivesoftware.smack.packet.Presence toSmackPresence(
+			Presence presence) {
+		Mode mode = (Mode) showConverstionMap.getKey(presence.show());
+		org.jivesoftware.smack.packet.Presence.Type type = (org.jivesoftware.smack.packet.Presence.Type)
typeConversionMap
+				.getKey(presence.type());
+		String status = StringUtils.defaultString(presence.status());
+		org.jivesoftware.smack.packet.Presence smackPresence = new org.jivesoftware.smack.packet.Presence(
+				type, status, 0, mode);
+		if (presence.from() != null) {
+			smackPresence.setFrom(presence.from().asString());
+		}
+		if (presence.to() != null) {
+			smackPresence.setTo(presence.to().asString());
+		}
+		return smackPresence;
+	}
 }

Added: incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceFilter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceFilter.java?rev=1104607&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceFilter.java
(added)
+++ incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceFilter.java
Tue May 17 22:30:37 2011
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.nuvem.cloud.xmpp.client;
+
+import org.apache.commons.lang.Validate;
+import org.jivesoftware.smack.filter.PacketFilter;
+import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.packet.Presence.Type;
+
+/**
+ * Accepts only {@link Presence} stanza of {@link Type} which is injected as a
+ * constructor argument
+ * 
+ */
+public class PresenceFilter implements PacketFilter {
+
+	private Type type;
+
+	public PresenceFilter(Type type) {
+		Validate.notNull(type);
+		this.type = type;
+	}
+
+	/**
+	 * Accepts only Presence stanza of type "subscribe".
+	 */
+	public boolean accept(Packet packet) {
+		if (packet != null) {
+			return (packet instanceof Presence)
+					&& ((Presence) packet).getType() == type;
+		}
+		return false;
+	}
+}
\ No newline at end of file

Added: incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceSubscriptionListener.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceSubscriptionListener.java?rev=1104607&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceSubscriptionListener.java
(added)
+++ incubator/nuvem/trunk/nuvem-standalone/src/main/java/org/apache/nuvem/cloud/xmpp/client/PresenceSubscriptionListener.java
Tue May 17 22:30:37 2011
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.nuvem.cloud.xmpp.client;
+
+import org.apache.nuvem.cloud.xmpp.presence.PresenceListener;
+import org.apache.nuvem.cloud.xmpp.presence.PresenceManager;
+import org.jivesoftware.smack.PacketListener;
+import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.packet.Presence;
+
+/**
+ * Listens to all only Presence stanza of type subscribe.
+ * 
+ * @see Presence.Type;
+ * 
+ */
+public class PresenceSubscriptionListener implements PacketListener {
+
+	/**
+	 * The presence manager.
+	 */
+	private PresenceManager presenceManager;
+
+	/**
+	 * Constructor which takes a reference to the {@link PresenceManager}
+	 * through which this listener can notify all listeners registered to
+	 * recieve Presence messages.
+	 * 
+	 * @param endPoint
+	 */
+	public PresenceSubscriptionListener(PresenceManager presenceManager) {
+		this.presenceManager = presenceManager;
+	}
+
+	@Override
+	public void processPacket(Packet packet) {
+		if (packet instanceof Presence) {
+			for (PresenceListener listener : presenceManager.listeners()) {
+				listener.listen(PresenceAdapter
+						.toNuvemPresence((Presence) packet));
+			}
+		}
+	}
+}
\ No newline at end of file

Added: incubator/nuvem/trunk/nuvem-standalone/src/test/java/org/apache/nuvem/cloud/xmpp/client/XMPPPresenceIntegrationTest.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone/src/test/java/org/apache/nuvem/cloud/xmpp/client/XMPPPresenceIntegrationTest.java?rev=1104607&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone/src/test/java/org/apache/nuvem/cloud/xmpp/client/XMPPPresenceIntegrationTest.java
(added)
+++ incubator/nuvem/trunk/nuvem-standalone/src/test/java/org/apache/nuvem/cloud/xmpp/client/XMPPPresenceIntegrationTest.java
Tue May 17 22:30:37 2011
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.nuvem.cloud.xmpp.client;
+
+import junit.framework.Assert;
+
+import org.apache.nuvem.cloud.xmpp.JID;
+import org.apache.nuvem.cloud.xmpp.XMPPEndPoint;
+import org.apache.nuvem.cloud.xmpp.XMPPServer;
+import org.apache.nuvem.cloud.xmpp.presence.Presence;
+import org.apache.nuvem.cloud.xmpp.presence.PresenceBuilder;
+import org.apache.nuvem.cloud.xmpp.presence.PresenceListener;
+import org.apache.nuvem.cloud.xmpp.presence.PresenceManager;
+import org.apache.nuvem.cloud.xmpp.server.DefaultXMPPServer;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class XMPPPresenceIntegrationTest {
+	private static XMPPServer server;
+	private static Node xmppNode;
+	private static PresenceManager presenceManagerA;
+	private static PresenceManager presenceManagerB;
+
+	@BeforeClass
+	public static void setUp() throws Exception {
+		server = new DefaultXMPPServer();
+		server.start();
+		xmppNode = NodeFactory.getInstance().createNode(
+				"testnuvemxmpp.composite");
+		xmppNode.start();
+		presenceManagerA = xmppNode.getService(XMPPEndPoint.class,
+				"TestXMPPComponentSender/XMPPEndPoint").presenceManager();
+		presenceManagerB = xmppNode.getService(XMPPEndPoint.class,
+				"TestXMPPComponentReciever/XMPPEndPoint").presenceManager();
+	}
+
+	@Test
+	public void ShouldSendAndRecievePresence() throws InterruptedException {
+		PresenceListenerImpl listener = new PresenceListenerImpl(new JID(
+				"nuvem@localhost"), Presence.Type.SUBSCRIBE);
+		presenceManagerB.registerListener(listener);
+		Presence presence = new PresenceBuilder().from(
+				new JID("nuvem@localhost")).to(new JID("reciever@localhost"))
+				.withType(Presence.Type.SUBSCRIBE).build();
+		presenceManagerA.sendPresence(presence);
+		Thread.sleep(2000);
+		listener.assertValidPresenceRecieved();
+
+	}
+
+	private static class PresenceListenerImpl implements PresenceListener {
+
+		private JID expectedFromJID;
+		private Presence.Type expectedType;
+
+		private JID actualFromJID;
+		private Presence.Type actualType;
+
+		public PresenceListenerImpl(JID expectedfrom, Presence.Type expectedType) {
+			this.expectedFromJID = expectedfrom;
+			this.expectedType = expectedType;
+		}
+
+		@Override
+		public void listen(Presence presence) {
+			actualFromJID = presence.from();
+			actualType = presence.type();
+		}
+
+		public void assertValidPresenceRecieved() {
+			Assert.assertEquals(expectedFromJID, actualFromJID);
+			Assert.assertEquals(expectedType, actualType);
+		}
+
+	}
+}



Mime
View raw message