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/04/04 22:08:37 UTC
svn commit: r1088802 - in
/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main:
java/org/apache/nuvem/xmpp/client/ resources/
Author: johnp
Date: Mon Apr 4 22:08:37 2011
New Revision: 1088802
URL: http://svn.apache.org/viewvc?rev=1088802&view=rev
Log:
Structured the code to support Different Connectors
1. Simple Connector - unsecured connection to the XMPP Server
2. SSL Connector - secure way to connect to the XMPP Server
Added:
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/AbstractConnector.java
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SSLConnector.java
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SimpleConnector.java
- copied, changed from r1086399, incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java
Removed:
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java
Modified:
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java
incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/resources/nuvemxmpp.composite
Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/AbstractConnector.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/AbstractConnector.java?rev=1088802&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/AbstractConnector.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/AbstractConnector.java Mon Apr 4 22:08:37 2011
@@ -0,0 +1,99 @@
+/*
+ * 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 org.apache.nuvem.cloud.xmpp.api.XMPPConnectException;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+import org.jivesoftware.smack.SASLAuthentication;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.oasisopen.sca.annotation.Property;
+
+/**
+ * Common Template to establish conneciton to the XMPPServer, which is extended
+ * by a more specific mechanisms.
+ *
+ *
+ */
+public abstract class AbstractConnector implements
+ XMPPConnector<XMPPConnection> {
+ /**
+ * Logger.
+ */
+ protected 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 String authenticationMechanism = "PLAIN";
+
+ /**
+ * The XMPP Connection.
+ */
+ private XMPPConnection connection;
+
+ /**
+ * Connects to the XMPP Server and returns the XMPPConnection.
+ */
+ public XMPPConnection connect() {
+ if (connection != null)
+ return connection;
+ log.info("Initializing a new connection...");
+ checkConfigurations();
+ connection = prepareConnection();
+ 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");
+ }
+
+ protected abstract void checkConfigurations();
+
+ protected abstract XMPPConnection prepareConnection();
+
+}
Modified: 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=1088802&r1=1088801&r2=1088802&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java (original)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/DefaultXMPPEndPoint.java Mon Apr 4 22:08:37 2011
@@ -82,10 +82,6 @@ public class DefaultXMPPEndPoint extends
*/
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");
}
@@ -126,10 +122,6 @@ public class DefaultXMPPEndPoint extends
*/
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");
@@ -146,10 +138,7 @@ public class DefaultXMPPEndPoint extends
*/
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");
Added: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SSLConnector.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SSLConnector.java?rev=1088802&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SSLConnector.java (added)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SSLConnector.java Mon Apr 4 22:08:37 2011
@@ -0,0 +1,84 @@
+/*
+ * 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 javax.net.SocketFactory;
+import javax.net.ssl.SSLSocketFactory;
+
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.XMPPConnection;
+import org.oasisopen.sca.annotation.Property;
+import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * Establishes a secure Stream to the XMPPServer.
+ *
+ */
+@Service(XMPPConnector.class)
+public final class SSLConnector extends AbstractConnector {
+
+ @Property
+ protected String trustStorePath;
+ @Property
+ protected String trustStorePassword;
+ @Reference(required = false)
+ protected SocketFactory socketFactory = SSLSocketFactory.getDefault();
+
+ /**
+ * {@inheritDoc}
+ */
+ public XMPPConnection connect(Map<String, String> connectionProperties) {
+ throw new UnsupportedOperationException("still not supported");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getDescription() {
+ return "Single Connection Default connector";
+ }
+
+ @Override
+ protected void checkConfigurations() {
+ if (host == null || port == 0 || clientJID == null
+ || clientPassword == null || trustStorePassword == null
+ || trustStorePath == null) {
+ throw new IllegalStateException(
+ "the configuration parameters are not initlaized properly!");
+ }
+ }
+
+ @Override
+ protected XMPPConnection prepareConnection() {
+ ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(
+ host, port, serviceName);
+ connectionConfiguration
+ .setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
+ connectionConfiguration.setSocketFactory(socketFactory);
+ connectionConfiguration.setTruststorePath(trustStorePath);
+ connectionConfiguration.setTruststorePassword(trustStorePassword);
+ return new XMPPConnection(connectionConfiguration);
+ }
+
+}
Copied: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SimpleConnector.java (from r1086399, 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/SimpleConnector.java?p2=incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SimpleConnector.java&p1=incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java&r1=1086399&r2=1088802&rev=1088802&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SingleConnectionConnector.java (original)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/java/org/apache/nuvem/xmpp/client/SimpleConnector.java Mon Apr 4 22:08:37 2011
@@ -19,129 +19,43 @@
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.
+ * {@link XMPPConnector} will try to connect to the XMPPServer in an unsecured
+ * way called. For more secure way of connecting to the XMPP server, please use
+ * {@link SSLConnector}
*
*/
@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;
+public class SimpleConnector extends AbstractConnector {
/**
* {@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));
+ public String getDescription() {
+ return "Single Connection Default connector";
+ }
+
+ @Override
+ protected void checkConfigurations() {
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() {
+ @Override
+ protected XMPPConnection prepareConnection() {
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";
+ return new XMPPConnection(connectionConfiguration);
}
}
Modified: incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/resources/nuvemxmpp.composite
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/resources/nuvemxmpp.composite?rev=1088802&r1=1088801&r2=1088802&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/resources/nuvemxmpp.composite (original)
+++ incubator/nuvem/trunk/nuvem-standalone-xmpp/src/main/resources/nuvemxmpp.composite Mon Apr 4 22:08:37 2011
@@ -28,14 +28,14 @@
<service name="XMPPEndPoint">
<interface.java interface="org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint" />
</service>
- <reference name="connector" target="SingleConnectionConnector/XMPPConnector">
+ <reference name="connector" target="SimpleConnector/XMPPConnector">
<interface.java interface="org.apache.nuvem.cloud.xmpp.api.XMPPConnector" />
</reference>
</component>
- <component name="SingleConnectionConnector">
+ <component name="SimpleConnector">
<implementation.java
- class="org.apache.nuvem.xmpp.client.SingleConnectionConnector" />
+ class="org.apache.nuvem.xmpp.client.SimpleConnector" />
<service name="XMPPConnector">
<interface.java interface="org.apache.nuvem.cloud.xmpp.api.XMPPConnector" />
</service>
@@ -43,4 +43,16 @@
<property name="clientPassword">password</property>
</component>
+ <component name="SSLConnector">
+ <implementation.java
+ class="org.apache.nuvem.xmpp.client.SSLConnector" />
+ <service name="XMPPConnector">
+ <interface.java interface="org.apache.nuvem.cloud.xmpp.api.XMPPConnector" />
+ </service>
+ <property name="clientJID">nuvem@localhost</property>
+ <property name="clientPassword">password</property>
+ <property name="trustStorePath">truststorepath</property>
+ <property name="trustStorePassword">changeit</property>
+ </component>
+
</composite>