You are viewing a plain text version of this content. The canonical link for it is here.
Posted to nuvem-commits@incubator.apache.org by jo...@apache.org on 2011/03/28 20:56:04 UTC

svn commit: r1086399 - in /incubator/nuvem/trunk/nuvem-standalone-xmpp: ./ src/main/java/org/apache/nuvem/ src/main/java/org/apache/nuvem/sca/ src/main/java/org/apache/nuvem/xmpp/ src/main/java/org/apache/nuvem/xmpp/client/ src/main/java/org/apache/nuv...

Author: johnp
Date: Mon Mar 28 20:56:03 2011
New Revision: 1086399

URL: http://svn.apache.org/viewvc?rev=1086399&view=rev
Log:
Implemented XMPPConnector implementation and restructured the folders into client/server to provide standalone XMPP server implementation

Added:
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/
      - copied from r1070036, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/
      - copied from r1070036, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageAdapter.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageListenerAdapter.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackStatusAdapter.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/
      - copied from r1070036, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/host/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/DefaultXMPPServer.java
      - copied, changed from r1070036, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/host/xmpp/DefaultXMPPServer.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPointTestCase.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/XMPPEndPointIntegrationTest.java
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/server/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/resources/
Removed:
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/sca/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/host/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/xmpp/
    incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/
Modified:
    incubator/nuvem/trunk/nuvem-standalone-xmpp/pom.xml

Modified: incubator/nuvem/trunk/nuvem-standalone-xmpp/pom.xml
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/pom.xml?rev=1086399&r1=1086398&r2=1086399&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/pom.xml (original)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/pom.xml Mon Mar 28 20:56:03 2011
@@ -30,7 +30,7 @@
 
 	<artifactId>nuvem-cloud-standalone-xmpp</artifactId>
 	<name>Apache Nuvem Standalone XMPP Host</name>
-
+	
 	<dependencies>
 		<dependency>
 			<groupId>org.apache.tuscany.sca</groupId>
@@ -82,5 +82,41 @@
 			<version>4.8.1</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.apache.nuvem</groupId>
+			<artifactId>nuvem-cloud-api</artifactId>
+			<version>1.0-incubating-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>jivesoftware</groupId>
+			<artifactId>smackx</artifactId>
+			<version>3.1.0</version>
+		</dependency>
+
+		<dependency>
+			<groupId>org.easymock</groupId>
+			<artifactId>easymock</artifactId>
+			<version>3.0</version>
+			<scope>test</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.tuscany.sca</groupId>
+			<artifactId>tuscany-sca-api</artifactId>
+			<version>${tuscany.version}</version>
+		</dependency>
+		
+		<dependency>
+			<groupId>org.apache.tuscany.sca</groupId>
+			<artifactId>tuscany-implementation-java-runtime</artifactId>
+			<version>${tuscany.version}</version>
+			<scope>runtime</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.tuscany.sca</groupId>
+			<artifactId>tuscany-domain-node</artifactId>
+			<version>${tuscany.version}</version>
+		</dependency>
 	</dependencies>
 </project>

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,170 @@
+/*
+ * 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.xmpp.client;
+
+import java.util.logging.Logger;
+
+import org.apache.commons.lang.Validate;
+import org.apache.nuvem.cloud.xmpp.api.JID;
+import org.apache.nuvem.cloud.xmpp.api.Message;
+import org.apache.nuvem.cloud.xmpp.api.Status;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+import org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint;
+import org.apache.nuvem.cloud.xmpp.common.AbstractXMPPEndPoint;
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.Roster;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.packet.Presence.Type;
+import org.oasisopen.sca.annotation.Init;
+import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * A Default Implemention of XMPP Client for cloud platforms without explicit
+ * XMPP support.
+ * 
+ */
+@Service(XMPPEndPoint.class)
+@Scope("COMPOSITE")
+public class DefaultXMPPEndPoint extends AbstractXMPPEndPoint implements
+		XMPPEndPoint {
+
+	/**
+	 * Logger.
+	 */
+	private static final Logger log = Logger.getLogger(XMPPEndPoint.class
+			.getName());
+
+	@Reference 
+	protected XMPPConnector<XMPPConnection> connector;
+
+	/**
+	 * Default constructor.
+	 */
+	public DefaultXMPPEndPoint() {
+
+	}
+
+	public DefaultXMPPEndPoint(XMPPConnector<XMPPConnection> connector) {
+		Validate.notNull(connector);
+		this.connector = connector;
+	}
+
+
+	@Init
+	public void init() {
+		log.info("Initializing DefaultXMPPEndPoint using connector " + connector.getDescription());
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public Status sendMessage(Message message) {
+		XMPPConnection connection = connector.connect();
+		if (!connection.isConnected()) {
+			throw new IllegalStateException(
+					"XMPP Endpoint Not connected to the XMPP Server, please check the configurations");
+		}
+		if (message == null || message.recipient() == null) {
+			throw new IllegalArgumentException("invalid message");
+		}
+		Status deliveryStatus = new Status();
+
+		org.apache.nuvem.cloud.xmpp.api.MessageListener nuvemListener = getListenerFor(message
+				.recipient());
+
+		Chat chat = connection.getChatManager().createChat(
+				message.recipient().asString(),
+				new SmackMessageListenerAdapter(nuvemListener));
+		try {
+			chat.sendMessage(message.payLoad().content());
+		} catch (XMPPException e) {
+			log.severe("error while sending xmpp message to "
+					+ message.recipient().asString());
+			deliveryStatus = SmackStatusAdapter.toStatus(e.getXMPPError());
+		}
+		return deliveryStatus;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public Status invite(JID jid) {
+		throw new UnsupportedOperationException("not supported yet");
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public Status invite(String jid) {
+		throw new UnsupportedOperationException("not supported yet");
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean isPresent(JID jid) {
+		XMPPConnection connection = connector.connect();
+		if (!connection.isConnected()) {
+			throw new IllegalStateException(
+					"XMPP Endpoint Not connected to the XMPP Server, please check the configurations");
+		}
+		if (jid == null)
+			throw new IllegalArgumentException(
+					"Illegal JID passed for finding presence");
+
+		Roster roster = connection.getRoster();
+		Presence presence = roster.getPresence(jid.asString());
+		if (presence != null)
+			return presence.getType() == Type.available;
+		return false;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean isPresent(String id) {
+		XMPPConnection connection = connector.connect();
+		if (!connection.isConnected()) {
+			throw new IllegalStateException(
+					"XMPP Endpoint Not connected to the XMPP Server, please check the configurations");
+		}
+		if (id == null)
+			throw new IllegalArgumentException(
+					"Illegal JID passed for finding presence");
+
+		Roster roster = connection.getRoster();
+		Presence presence = roster.getPresence(id);
+		if (presence != null)
+			return presence.getType() == Type.available;
+		return false;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean isConnected() {
+		return connector.connect().isConnected();
+	}
+}

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,147 @@
+/*
+ * 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.xmpp.client;
+
+import java.util.Map;
+import java.util.logging.Logger;
+
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLSocketFactory;
+
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnectException;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.SASLAuthentication;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.oasisopen.sca.annotation.Property;
+import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * Helps establish a connection to the XMPP Server and returns the same
+ * connection instance once connected. This specific implementation of
+ * {@link XMPPConnector} will just maintain on connection instance to the
+ * XMPPServer and return the same each time the {@link #connect()} method is
+ * called.
+ * 
+ */
+@Service(XMPPConnector.class)
+@Scope("COMPOSITE")
+public class SingleConnectionConnector implements XMPPConnector<XMPPConnection> {
+
+	/**
+	 * Logger.
+	 */
+	private static final Logger log = Logger.getLogger(XMPPConnector.class
+			.getName());
+
+	@Property(required = false)
+	protected String host = "localhost";
+	@Property(required = false)
+	protected int port = 5222;
+	@Property(required = false)
+	protected String serviceName;
+	@Property
+	protected String clientJID = "nuvem@localhost";
+	@Property
+	protected String clientPassword = "password";
+	@Property(required = false)
+	protected boolean sslMode = false;
+	@Property(required = false)
+	protected String trustStorePath;
+	@Property(required = false)
+	protected String trustStorePassword;
+
+	/** Authentication mechanism. */
+	private String authenticationMechanism = "PLAIN";
+
+	@Reference(required = false)
+	protected SocketFactory socketFactory = SSLSocketFactory.getDefault();
+
+	// TODO: connection and connected classes like the chat are assumed to be
+	// thread safe.. if required, a connection pool needs to be implemented.
+	/**
+	 * The XMPP Connection.
+	 */
+	private XMPPConnection connection;
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public XMPPConnection connect() {
+		if (connection != null)
+			return connection;
+		log
+				.info(String
+						.format(
+								"initializing XMPP Connection for server %s and with client jid %s",
+								host, clientJID));
+		if (host == null || port == 0 || clientJID == null
+				|| clientPassword == null) {
+			throw new IllegalStateException(
+					"the configuration parameters are not initlaized properly!");
+		}
+		connection = new XMPPConnection(buildConnectionConfiguration());
+		SASLAuthentication.supportSASLMechanism(authenticationMechanism, 0);
+		log.info("Connecting to XMPP Server....");
+		try {
+			connection.connect();
+			connection.login(clientJID, clientPassword);
+			log.info("EndPoint connected to XMPP Server successfuly!");
+		} catch (XMPPException e) {
+			log.severe("Error while connecting to the XMPP Server..."
+					+ e.getMessage());
+			throw new XMPPConnectException(
+					"error while connecting to the xmpp server", e);
+		}
+		return connection;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public XMPPConnection connect(Map<String, String> connectionProperties) {
+		throw new UnsupportedOperationException("still not supported");
+	}
+
+	private ConnectionConfiguration buildConnectionConfiguration() {
+		ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(
+				host, port, serviceName);
+		if (sslMode && !(trustStorePassword == null || trustStorePath == null)) {
+			log.info("XMPP is configured to use SSL mode.");
+			connectionConfiguration
+					.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
+			connectionConfiguration.setSocketFactory(socketFactory);
+			connectionConfiguration.setTruststorePath(trustStorePath);
+			connectionConfiguration.setTruststorePassword(trustStorePassword);
+		}
+		return connectionConfiguration;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public String getDescription() {
+		return "Single Connection Default connector";
+	}
+
+}

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageAdapter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageAdapter.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageAdapter.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageAdapter.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,45 @@
+/*
+ * 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.xmpp.client;
+
+import org.jivesoftware.smack.packet.Message;
+
+/**
+ * Adapter to transform nuvem message into smack message.
+ * 
+ * 
+ */
+public class SmackMessageAdapter {
+
+	/**
+	 * Converts nuvem specific message object to the one smack API accepts.
+	 * 
+	 */
+	public static Message toSmackMessage(
+			org.apache.nuvem.cloud.xmpp.api.Message nuvemMessage, String sender) {
+		Message smackMessage = new Message();
+		smackMessage.setFrom(sender);
+		smackMessage.setBody(nuvemMessage.payLoad().content());
+		smackMessage.setTo(nuvemMessage.recipient().asString());
+		return smackMessage;
+	}
+
+}

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageListenerAdapter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageListenerAdapter.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageListenerAdapter.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackMessageListenerAdapter.java Mon Mar 28 20:56:03 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.xmpp.client;
+
+import org.apache.nuvem.cloud.xmpp.api.MessageBuilder;
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.MessageListener;
+import org.jivesoftware.smack.packet.Message;
+
+/**
+ * Message Listener to listen to smack api and adapt it to nuvem specific
+ * messages.
+ * 
+ */
+public class SmackMessageListenerAdapter implements MessageListener {
+
+	/**
+	 * The Nuvem Listener.
+	 */
+	private org.apache.nuvem.cloud.xmpp.api.MessageListener nuvemListener;
+
+	public SmackMessageListenerAdapter(
+			org.apache.nuvem.cloud.xmpp.api.MessageListener nuvemMessageListener) {
+		if (nuvemMessageListener == null)
+			throw new IllegalArgumentException("listener should not be null");
+		this.nuvemListener = nuvemMessageListener;
+	}
+
+	@Override
+	public void processMessage(Chat chat, Message message) {
+		nuvemListener.listen(new MessageBuilder().from(message.getFrom())
+				.containing(message.getBody()).build());
+	}
+
+}

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackStatusAdapter.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackStatusAdapter.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackStatusAdapter.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SmackStatusAdapter.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,61 @@
+/*
+ * 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.xmpp.client;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.nuvem.cloud.xmpp.api.Error;
+import org.apache.nuvem.cloud.xmpp.api.ErrorCode;
+import org.apache.nuvem.cloud.xmpp.api.Status;
+import org.jivesoftware.smack.packet.XMPPError;
+
+/**
+ * Acts as an adapter to transform the status recieved by the the send API to
+ * the form compatible with nuvem.
+ */
+public class SmackStatusAdapter {
+
+	/**
+	 * converstion map.
+	 */
+	private static Map<Integer, ErrorCode> converstionMap;
+
+	static {
+		converstionMap = new HashMap<Integer, ErrorCode>();
+		converstionMap.put(Integer.valueOf(400), ErrorCode.BAD_FORMAT);
+		converstionMap.put(Integer.valueOf(401), ErrorCode.NOT_AUTHORIZED);
+		converstionMap.put(Integer.valueOf(408), ErrorCode.CONNECTION_TIMEOUT);
+	}
+
+	public static Status toStatus(XMPPError xmppError) {
+		Status status = new Status();
+		if (xmppError != null
+				&& converstionMap.containsKey(xmppError.getCode())) {
+			status.addError(new Error(converstionMap.get(xmppError.getCode()),
+					xmppError.getMessage()));
+			return status;
+		}
+		status.addError(new Error(ErrorCode.UNKNOWN_ERROR, xmppError
+				.getMessage()));
+		return status;
+
+	}
+}

Copied: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/DefaultXMPPServer.java (from r1070036, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/host/xmpp/DefaultXMPPServer.java)
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/DefaultXMPPServer.java?p2=incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/DefaultXMPPServer.java&p1=incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/host/xmpp/DefaultXMPPServer.java&r1=1070036&r2=1086399&rev=1086399&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/tuscany/sca/host/xmpp/DefaultXMPPServer.java (original)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/server/DefaultXMPPServer.java Mon Mar 28 20:56:03 2011
@@ -17,11 +17,12 @@
  * under the License.    
  */
 
-package org.apache.tuscany.sca.host.xmpp;
+package org.apache.nuvem.xmpp.server;
 
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.nuvem.cloud.xmpp.api.XMPPServer;
 import org.apache.vysper.mina.TCPEndpoint;
 import org.apache.vysper.stanzasession.StanzaSessionFactory;
 import org.apache.vysper.storage.StorageProviderRegistry;
@@ -32,83 +33,90 @@ import org.apache.vysper.xmpp.modules.ex
 import org.apache.vysper.xmpp.modules.extension.xep0092_software_version.SoftwareVersionModule;
 import org.apache.vysper.xmpp.modules.extension.xep0119_xmppping.XmppPingModule;
 import org.apache.vysper.xmpp.modules.extension.xep0202_entity_time.EntityTimeModule;
+import org.oasisopen.sca.annotation.Service;
 
 /**
  * Default XMPP Server
  */
+@Service(XMPPServer.class)
 public class DefaultXMPPServer implements XMPPServer {
-    private static String DEFAULT_HOST = "localhost";
-    
-    org.apache.vysper.xmpp.server.XMPPServer server;
-
-    /**
-     * Default constructor
-     */
-    public DefaultXMPPServer() {
-     
-    }
-    
-    /**
-     * Start a Embedded XMPP Server
-     */
-    public void start() {
-        try {
-            StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry();
-
-            final AccountManagement accountManagement = (AccountManagement)providerRegistry.retrieve(AccountManagement.class);
-
-            accountManagement.addUser("test@localhost", "password");
-            accountManagement.addUser("test2@localhost", "password");
-
-            server = new org.apache.vysper.xmpp.server.XMPPServer(DEFAULT_HOST);
-            server.addEndpoint(new TCPEndpoint());
-            server.addEndpoint(new StanzaSessionFactory());
-            server.setStorageProviderRegistry(providerRegistry);
-
-            server.setTLSCertificateInfo(new File("src/main/resources/config/bogus_mina_tls.cert"), "boguspw");
-
-            try {
-                server.start();
-                System.out.println("vysper server is running...");
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
-            server.addModule(new SoftwareVersionModule());
-            server.addModule(new EntityTimeModule());
-            server.addModule(new VcardTempModule());
-            server.addModule(new XmppPingModule());
-            server.addModule(new PrivateDataModule());
-            
-            //server.addModule(new MUCModule());
-        } catch (Exception e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-    }
-
-    /**
-     * Stop the XMPP Server and perform any cleanup necessary
-     */
-    public void stop() {
-        server.stop();
-
-    }
-    
-    /**
-     * For debug/test purposes only
-     * @param args
-     * @throws IOException
-     */
-    public static void main(String args[]) throws IOException {
-        DefaultXMPPServer server = new DefaultXMPPServer();
-        server.start();
-        System.out.println("XMPP server started !!!");
-        System.in.read();
-        System.out.println("Stopping ...");
-        server.stop();
-        System.out.println();
-    }
 
+	private static String DEFAULT_HOST = "localhost";
+
+	private org.apache.vysper.xmpp.server.XMPPServer server;
+
+	/**
+	 * Default constructor
+	 */
+	public DefaultXMPPServer() {
+
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void start() {
+		try {
+			StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry();
+
+			final AccountManagement accountManagement = (AccountManagement) providerRegistry
+					.retrieve(AccountManagement.class);
+
+			accountManagement.addUser("nuvem@localhost", "password");
+			accountManagement.addUser("test2@localhost", "password");
+
+			server = new org.apache.vysper.xmpp.server.XMPPServer(DEFAULT_HOST);
+			server.addEndpoint(new TCPEndpoint());
+			server.addEndpoint(new StanzaSessionFactory());
+			server.setStorageProviderRegistry(providerRegistry);
+
+			server
+					.setTLSCertificateInfo(new File(
+							"src/main/resources/config/bogus_mina_tls.cert"),
+							"boguspw");
+
+			try {
+				server.start();
+				System.out.println("vysper server is running...");
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+
+			server.addModule(new SoftwareVersionModule());
+			server.addModule(new EntityTimeModule());
+			server.addModule(new VcardTempModule());
+			server.addModule(new XmppPingModule());
+			server.addModule(new PrivateDataModule());
+
+			// server.addModule(new MUCModule());
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
+
+	/**
+	 * Stop the XMPP Server and perform any cleanup necessary
+	 */
+	public void stop() {
+		server.stop();
+
+	}
+
+	/**
+	 * For debug/test purposes only
+	 * 
+	 * @param args
+	 * @throws IOException
+	 */
+	public static void main(String args[]) throws IOException {
+		DefaultXMPPServer server = new DefaultXMPPServer();
+		server.start();
+		System.out.println("XMPP server started !!!");
+		System.in.read();
+		System.out.println("Stopping ...");
+		server.stop();
+		System.out.println();
+	}
 }

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPointTestCase.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPointTestCase.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPointTestCase.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPointTestCase.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,112 @@
+/*
+ * 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.xmpp.client;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.isA;
+import static org.easymock.EasyMock.replay;
+
+import org.apache.nuvem.cloud.xmpp.api.Status;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+import org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint;
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.ChatManager;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.XMPPError;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DefaultXMPPEndPointTestCase {
+
+	private static final String RECIPIENT = "recipient@host.com";
+
+	private static final String MESSAGE = "hi";
+
+	private XMPPConnector<XMPPConnection> connector;
+	private XMPPConnection connection;
+	private ChatManager chatManager;
+	private Chat chat;
+
+	private XMPPEndPoint endPoint;
+
+	@Before
+	public void init() {
+		connection = createMock(XMPPConnection.class);
+		chatManager = createMock(ChatManager.class);
+		connector = createMock(XMPPConnector.class);
+		expect(connector.connect()).andReturn(connection);
+		chat = createMock(Chat.class);
+		endPoint = new DefaultXMPPEndPoint(connector);
+	}
+
+	@Test(expected = IllegalStateException.class)
+	public void shouldHandleConnectivityProblem() {
+		expect(connection.isConnected()).andReturn(false);
+		replay(connection);
+		replay(connector);
+		endPoint.sendTextMessage(MESSAGE, RECIPIENT);
+	}
+
+	@Test
+	public void shouldBeAbleToSendMessage() throws XMPPException,
+			IllegalArgumentException, IllegalAccessException {
+		expect(connection.isConnected()).andReturn(true);
+		expect(connection.getChatManager()).andReturn(chatManager);
+		expect(
+				chatManager.createChat(isA(String.class),
+						isA(SmackMessageListenerAdapter.class)))
+				.andReturn(chat);
+		chat.sendMessage(MESSAGE);
+		expectLastCall();
+		replay(connection);
+		replay(chatManager);
+		replay(chat);
+		replay(connector);
+		Status status = endPoint.sendTextMessage(MESSAGE, RECIPIENT);
+		Assert.assertNotNull(status);
+		Assert.assertTrue(!status.hasErrors());
+	}
+
+	@Test
+	public void shouldBeAbleToIdentifyErrors() throws IllegalArgumentException,
+			IllegalAccessException, XMPPException {
+		expect(connection.isConnected()).andReturn(true);
+		expect(connection.getChatManager()).andReturn(chatManager);
+		expect(
+				chatManager.createChat(isA(String.class),
+						isA(SmackMessageListenerAdapter.class)))
+				.andReturn(chat);
+		chat.sendMessage(MESSAGE);
+		expectLastCall().andThrow(
+				new XMPPException("error", new XMPPError(302, "error")));
+		replay(connection);
+		replay(chatManager);
+		replay(chat);
+		replay(connector);
+		Status status = endPoint.sendTextMessage(MESSAGE, RECIPIENT);
+		Assert.assertNotNull(status);
+		Assert.assertTrue(status.hasErrors());
+	}
+
+}

Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/XMPPEndPointIntegrationTest.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/XMPPEndPointIntegrationTest.java?rev=1086399&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/XMPPEndPointIntegrationTest.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/test/java/org/apache/nuvem/xmpp/client/XMPPEndPointIntegrationTest.java Mon Mar 28 20:56:03 2011
@@ -0,0 +1,85 @@
+/*
+ * 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.xmpp.client;
+
+import junit.framework.Assert;
+
+import org.apache.nuvem.cloud.xmpp.api.JID;
+import org.apache.nuvem.cloud.xmpp.api.Message;
+import org.apache.nuvem.cloud.xmpp.api.MessageListener;
+import org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint;
+import org.apache.nuvem.cloud.xmpp.api.XMPPServer;
+import org.apache.nuvem.xmpp.server.DefaultXMPPServer;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class XMPPEndPointIntegrationTest {
+
+	private static XMPPServer server;
+	private static Node xmppNode;
+	private static XMPPEndPoint xmppEndPoint;
+
+	@BeforeClass
+	public static void setUp() throws Exception {
+		server = new DefaultXMPPServer();
+		server.start();
+		xmppNode = NodeFactory.getInstance().createNode(
+				"nuvemxmpp.composite");
+		xmppNode.start();
+		xmppEndPoint = xmppNode.getService(XMPPEndPoint.class,
+				"XMPPComponent/XMPPEndPoint");
+
+	}
+
+	@AfterClass
+	public static void tearDown() throws Exception {
+		if (xmppNode != null) {
+			xmppNode.stop();
+		}
+		if (server != null) {
+			server.stop();
+		}
+	}
+
+	@Test
+	public void ShouldBeConnectedAndReadyToUse() {
+		Assert.assertTrue(xmppEndPoint.isConnected());
+	}
+
+	@Test
+	public void SendRoundTripMessage() {
+		final String message = "hello xmpp";
+		xmppEndPoint.registerListner(new JID("nuvem@localhost"),
+				new MessageListener() {
+					@Override
+					public void listen(Message message) {
+						Assert.assertNotNull(message);
+						Assert.assertTrue(message.equals(message.payLoad()
+								.content()));
+					}
+				});
+
+		xmppEndPoint.sendTextMessage(message, "nuvem@localhost");
+	}
+
+}