1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

Initial cut of constructor re-factor (SMACK-184).

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6532 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2007-01-04 23:00:58 +00:00 committed by matt
parent 3915eae215
commit 92d1de2dce
12 changed files with 81 additions and 292 deletions

View file

@ -68,10 +68,11 @@ public class ConnectionConfiguration implements Cloneable {
private String password;
private String resource;
private boolean sendPresence;
/**
* Creates a new ConnectionConfiguration for a connection that will connect
* to the desired service name. A DNS SRV lookup will be performed to find out the
* actual host address and port to use for the connection.
* Creates a new ConnectionConfiguration for the specified service name.
* A DNS SRV lookup will be performed to find out the actual host address
* and port to use for the connection.
*
* @param serviceName the name of the service provided by an XMPP server.
*/
@ -82,10 +83,14 @@ public class ConnectionConfiguration implements Cloneable {
}
/**
* Creates a new ConnectionConfiguration for a connection that will connect
* to the desired host, port and service name. It is expected for the XMPP server to
* be running at the specified address and to be using the specified service name.
* Anyway, if the service name is incorrect the correct one will be used instead.
* Creates a new ConnectionConfiguration using the specified host, port and
* service name. This is useful for manually overriding the DNS SRV lookup
* process that's used with the {@link #ConnectionConfiguration(String)}
* constructor. For example, say that an XMPP server is running at localhost
* in an internal network on port 5222 but is configured to think that it's
* "example.com" for testing purposes. This constructor is necessary to connect
* to the server in that case since a DNS SRV lookup for example.com would not
* point to the local testing server.
*
* @param host the host where the XMPP server is running.
* @param port the port where the XMPP is listening.

View file

@ -1,38 +0,0 @@
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright 2003-2004 Jive Software.
*
* All rights reserved. Licensed 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.jivesoftware.smack;
/**
* Convenience class to make it easier to connect to the Google Talk IM service.
* You can also use {@link XMPPConnection} to connect to Google Talk by specifying
* the server name, service name, and port.<p>
*
* After creating the connection, log in in using a Gmail username and password.
* For the Gmail address "jsmith@gmail.com", the username is "jsmith".
*
* @author Matt Tucker
*/
public class GoogleTalkConnection extends XMPPConnection {
public GoogleTalkConnection() throws XMPPException {
super("talk.google.com", 5222, "gmail.com");
}
}

View file

@ -25,10 +25,8 @@ import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.StringUtils;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import java.io.*;
@ -44,8 +42,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
* Creates a connection to a XMPP server. A simple use of this API might
* look like the following:
* <pre>
* // Create a connection to the jivesoftware.com XMPP server.
* XMPPConnection con = new XMPPConnection("jivesoftware.com");
* // Create a connection to the igniterealtime.org XMPP server.
* XMPPConnection con = new XMPPConnection("igniterealtime.org");
* // Connect to the server
* con.connect();
* // Most servers require you to login before performing other tasks.
@ -117,7 +115,7 @@ public class XMPPConnection {
*/
String serviceName;
String connectionID;
String connectionID = null;
private String user = null;
private boolean connected = false;
/**
@ -158,119 +156,59 @@ public class XMPPConnection {
/**
* Creates a new connection to the specified XMPP server. A DNS SRV lookup will be
* performed to try to determine the IP address and port corresponding to the
* serviceName; if that lookup fails, it's assumed that server resides at serviceName
* with the default port of 5222. This is the preferred constructor for connecting
* to an XMPP server.<p>
* performed to determine the IP address and port corresponding to the
* service name; if that lookup fails, it's assumed that server resides at
* <tt>serviceName</tt> with the default port of 5222. Encrypted connections (TLS)
* will be used if available, stream compression is disabled, and standard SASL
* mechanisms will be used for authentication.<p>
*
* Note that XMPPConnection constructors do not establish the connection to the server,
* to make it effective use the connect method. {@link #connect()}.
* This is the simplest constructor for connecting to an XMPP server. Alternatively,
* you can get fine-grained control over connection settings using the
* {@link #XMPPConnection(ConnectionConfiguration)} constructor.<p>
*
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
* Note that XMPPConnection constructors do not establish a connection to the server
* and you must call {@link #connect()}.
*
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>example.com</tt>.
*/
public XMPPConnection(String serviceName) {
// Perform DNS lookup to get host and port to use
DNSUtil.HostAddress address = DNSUtil.resolveXMPPDomain(serviceName);
// Create the configuration for this new connection
ConnectionConfiguration config =
new ConnectionConfiguration(address.getHost(), address.getPort(), serviceName);
ConnectionConfiguration config = new ConnectionConfiguration(serviceName);
config.setTLSEnabled(true);
config.setCompressionEnabled(false);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(DEBUG_ENABLED);
init(config, null);
this.configuration = config;
}
/**
* Creates a new connection to the XMPP server at the specifiec host and port.<p>
* Creates a new XMPP connection using the specified connection configuration.<p>
*
* Note that XMPPConnection constructors do not establish the connection to the server,
* to make it effective use the connect method. {@link #connect()}.
* Manually specifying connection configuration information is suitable for
* advanced users of the API. In many cases, using the
* {@link #XMPPConnection(String)} constructor is a better approach.<p>
*
* @param host the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
* Note that XMPPConnection constructors do not establish a connection to the server
* and you must call {@link #connect()}.
*
* @param config the connection configuration.
*/
public XMPPConnection(String host, int port) {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(host, port);
config.setTLSEnabled(true);
config.setCompressionEnabled(false);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(DEBUG_ENABLED);
init(config, null);
}
/**
* Creates a new connection to the specified XMPP server on the given host and port.<p>
*
* Note that XMPPConnection constructors do not establish the connection to the server,
* to make it effective use the connect method. {@link #connect()}.
*
* @param host the host name, or null for the loopback address.
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
*/
public XMPPConnection(String host, int port, String serviceName) {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(host, port, serviceName);
config.setTLSEnabled(true);
config.setCompressionEnabled(false);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(DEBUG_ENABLED);
init(config, null);
}
/**
* Creates a new connection to the specified XMPP server on the given port using the
* specified SocketFactory.<p>
*
* A custom SocketFactory allows fine-grained control of the actual connection to the
* XMPP server. A typical use for a custom SocketFactory is when connecting through a
* SOCKS proxy.<p>
*
* Note that XMPPConnection constructors do not establish the connection to the server,
* to make it effective use the connect method. {@link #connect()}.
*
* @param host the host name, or null for the loopback address.
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
* @param socketFactory a SocketFactory that will be used to create the socket to the XMPP
* server.
*/
public XMPPConnection(String host, int port, String serviceName, SocketFactory socketFactory) {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(host, port, serviceName);
config.setTLSEnabled(true);
config.setCompressionEnabled(false);
config.setSASLAuthenticationEnabled(true);
config.setDebuggerEnabled(DEBUG_ENABLED);
init(config, socketFactory);
}
public XMPPConnection(ConnectionConfiguration config) {
init(config, null);
}
public XMPPConnection(ConnectionConfiguration config, SocketFactory socketFactory) {
init(config, socketFactory);
}
/**
* Package-private default constructor. This constructor is only intended
* for unit testing. Normal classes extending XMPPConnection should override
* one of the other constructors.
*/
XMPPConnection() {
this.configuration = config;
}
/**
* Returns the connection ID for this connection, which is the value set by the server
* when opening a XMPP stream. If the server does not set a connection ID, this value
* will be null.
* will be null. This value will be <tt>null</tt> if not connected to the server.
*
* @return the ID of this connection returned from the XMPP server.
* @return the ID of this connection returned from the XMPP server or <tt>null</tt> if
* not connected to the server.
*/
public String getConnectionID() {
if (!isConnected()) {
return null;
}
return connectionID;
}
@ -849,25 +787,6 @@ public class XMPPConnection {
initConnection();
}
/**
* Initializes the connection configuration. This method is only executed
* when the XMPPConnection is created.
*
* @param config the connection configuration options.
* @param socketFactory a factory used for creating sockets to the XMPP server.
*/
private void init(ConnectionConfiguration config, SocketFactory socketFactory) {
try {
// Keep a copy to be sure that once the configuration has been passed to the
// constructor it cannot be modified
this.configuration = (ConnectionConfiguration) config.clone();
this.configuration.setSocketFactory(socketFactory);
}
catch (CloneNotSupportedException e) {
// Do nothing
}
}
/**
* Initializes the connection by creating a packet reader and writer and opening a
* XMPP stream to the server.

View file

@ -152,7 +152,9 @@ public class DNSUtil {
port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
host = srvRecordEntries[srvRecordEntries.length-1];
}
catch (Exception e2) { }
catch (Exception e2) {
// Ignore.
}
}
// Host entries in DNS should end with a ".".
if (host.endsWith(".")) {
@ -212,11 +214,7 @@ public class DNSUtil {
if (!host.equals(address.host)) {
return false;
}
if (port != address.port) {
return false;
}
return true;
return port == address.port;
}
}
}

View file

@ -31,7 +31,7 @@ public class LoginTest extends SmackTestCase {
*/
public void testInvalidLogin() {
try {
XMPPConnection connection = new XMPPConnection(getHost(), getPort());
XMPPConnection connection = createConnection();
connection.connect();
try {
// Login with an invalid user
@ -59,8 +59,8 @@ public class LoginTest extends SmackTestCase {
*/
public void testSASLAnonymousLogin() {
try {
XMPPConnection conn1 = new XMPPConnection(getHost(), getPort());
XMPPConnection conn2 = new XMPPConnection(getHost(), getPort());
XMPPConnection conn1 = createConnection();
XMPPConnection conn2 = createConnection();
conn1.connect();
conn2.connect();
try {
@ -133,7 +133,7 @@ public class LoginTest extends SmackTestCase {
*/
public void testLoginWithNoResource() {
try {
XMPPConnection conn = new XMPPConnection(getHost(), getPort());
XMPPConnection conn = createConnection();
conn.connect();
try {
conn.getAccountManager().createAccount("user_1", "user_1");

View file

@ -1,102 +0,0 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smack;
import junit.framework.TestCase;
public class MessengerLoginTest extends TestCase {
private String host;
private int port;
public void setUp() {
// override default settings from system properties
if (System.getProperty("smack.test.host") != null) {
host = System.getProperty("smack.test.host");
}
if (System.getProperty("smack.test.port") != null) {
try {
port = Integer.parseInt(System.getProperty("smack.test.port"));
}
catch (Exception ignored) {}
}
}
public void testAdminLogin() {
String username = System.getProperty("smack.test.admin.username");
String password = System.getProperty("smack.test.admin.password");
String resource = System.getProperty("smack.test.admin.resource");
boolean debug = false;
try {
debug = Boolean.valueOf(System.getProperty("smack.debug")).booleanValue();
}
catch (Exception ignored) {}
XMPPConnection.DEBUG_ENABLED = debug;
try {
XMPPConnection con = new XMPPConnection(host, port);
con.connect();
con.login(username, password, resource);
}
catch (XMPPException e) {
String message = e.getMessage();
if (e.getXMPPError() != null) {
message = "XMPPError code: " + e.getXMPPError().getCode() + ", message: "
+ e.getXMPPError().getMessage();
}
/*fail("Login to server " + host + ":" + port + " failed using user/pass/resource: "
+ username + "/" + password + "/" + resource + ". Error message: " + message);*/
}
}
}

View file

@ -36,7 +36,7 @@ public class PresenceTest extends SmackTestCase {
XMPPConnection conn = null;
try {
// User_1 will log in again using another resource
conn = new XMPPConnection(getHost(), getPort());
conn = createConnection();
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
// Change the presence priorities of User_1
@ -84,7 +84,7 @@ public class PresenceTest extends SmackTestCase {
Presence.Mode.available));
// User_1 will log in again using another resource
conn = new XMPPConnection(getHost(), getPort());
conn = createConnection();
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
conn.sendPacket(new Presence(Presence.Type.available, null, 1,
@ -136,7 +136,7 @@ public class PresenceTest extends SmackTestCase {
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
// User_1 will log in again using another resource (that is going to be available)
XMPPConnection conn = new XMPPConnection(getHost(), getPort());
XMPPConnection conn = createConnection();
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");

View file

@ -153,14 +153,14 @@ public class ReconnectionTest extends SmackTestCase {
assertEquals("Failed the manual connection", true, connection.isAnonymous());
}
private XMPPConnection createConnection() throws Exception {
private XMPPConnection createXMPPConnection() throws Exception {
XMPPConnection connection;
// Create the configuration
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setTLSEnabled(true);
config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
config.setSASLAuthenticationEnabled(true);
connection = new XMPPConnection(config, getSocketFactory());
connection = new XMPPConnection(config);
return connection;
}

View file

@ -366,7 +366,7 @@ public class RosterTest extends SmackTestCase {
// Log in from another resource so we can test the roster
XMPPConnection con2 = new XMPPConnection(getHost(), getPort());
XMPPConnection con2 = createConnection();
con2.connect();
con2.login(getUsername(0), getUsername(0), "MyNewResource");

View file

@ -107,6 +107,24 @@ public abstract class SmackTestCase extends TestCase {
return connections[index];
}
/**
* Creates a new XMPPConnection using the connection preferences. This is useful when
* not using a connection from the connection pool in a test case.
*
* @return a new XMPP connection.
*/
protected XMPPConnection createConnection() {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(host, port);
config.setTLSEnabled(true);
config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
config.setSASLAuthenticationEnabled(true);
if (getSocketFactory() == null) {
config.setSocketFactory(getSocketFactory());
}
return new XMPPConnection(config);
}
/**
* Returns the name of the user (e.g. johndoe) that is using the connection
* located at the requested position.
@ -183,17 +201,7 @@ public abstract class SmackTestCase extends TestCase {
try {
// Connect to the server
for (int i = 0; i < getMaxConnections(); i++) {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(host, port);
config.setTLSEnabled(true);
config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
config.setSASLAuthenticationEnabled(true);
if (getSocketFactory() == null) {
connections[i] = new XMPPConnection(config);
}
else {
connections[i] = new XMPPConnection(config, getSocketFactory());
}
connections[i] = createConnection();
connections[i].connect();
}
// Use the host name that the server reports. This is a good idea in most

View file

@ -69,7 +69,7 @@ public class CompressionTest extends SmackTestCase {
*/
protected void setUp() throws Exception {
super.setUp();
XMPPConnection setupConnection = new XMPPConnection(getHost(), getPort());
XMPPConnection setupConnection = new XMPPConnection(getServiceName());
setupConnection.connect();
if (!setupConnection.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
@ -90,7 +90,7 @@ public class CompressionTest extends SmackTestCase {
*/
protected void tearDown() throws Exception {
super.tearDown();
XMPPConnection setupConnection = new XMPPConnection(getHost(), getPort());
XMPPConnection setupConnection = createConnection();
setupConnection.connect();
setupConnection.login("user0", "user0");
// Delete the created account for the test
@ -98,5 +98,4 @@ public class CompressionTest extends SmackTestCase {
// Close the setupConnection
setupConnection.disconnect();
}
}
}

View file

@ -242,7 +242,7 @@ public class MultiUserChatTest extends SmackTestCase {
public void testAnonymousParticipant() {
try {
// Anonymous user joins the new room
XMPPConnection anonConnection = new XMPPConnection(getHost(), getPort());
XMPPConnection anonConnection = new XMPPConnection(getServiceName());
anonConnection.connect();
anonConnection.loginAnonymously();
MultiUserChat muc2 = new MultiUserChat(anonConnection, room);