1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02: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;
}
}
}