Index: src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java =================================================================== retrieving revision 1.5 diff -u -r1.5 DefaultProtocolSocketFactory.java --- src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java 31 Jan 2003 00:33:36 -0000 1.5 +++ src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java 24 Jan 2004 15:44:38 -0000 @@ -70,7 +70,7 @@ /** * The default class for creating protocol sockets. This class just uses the - * Protocol constructors. + * {@link java.net.Socket socket} constructors. * * @author Michael Becke * @@ -79,6 +79,19 @@ public class DefaultProtocolSocketFactory implements ProtocolSocketFactory { /** + * The factory singleton. + */ + private static final DefaultProtocolSocketFactory factory = new DefaultProtocolSocketFactory(); + + /** + * Gets an singleton instance of the DefaultProtocolSocketFactory. + * @return a DefaultProtocolSocketFactory + */ + static DefaultProtocolSocketFactory getSocketFactory() { + return factory; + } + + /** * Constructor for DefaultProtocolSocketFactory. */ public DefaultProtocolSocketFactory() { @@ -103,6 +116,20 @@ public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return new Socket(host, port); + } + + /** + * All instances of DefaultProtocolSocketFactory are the same. + */ + public boolean equals(Object obj) { + return ((obj != null) && obj.getClass().equals(DefaultProtocolSocketFactory.class)); + } + + /** + * All instances of DefaultProtocolSocketFactory have the same hash code. + */ + public int hashCode() { + return DefaultProtocolSocketFactory.class.hashCode(); } } Index: src/java/org/apache/commons/httpclient/protocol/Protocol.java =================================================================== retrieving revision 1.7 diff -u -r1.7 Protocol.java --- src/java/org/apache/commons/httpclient/protocol/Protocol.java 10 Nov 2003 14:33:13 -0000 1.7 +++ src/java/org/apache/commons/httpclient/protocol/Protocol.java 24 Jan 2004 15:44:39 -0000 @@ -165,14 +165,14 @@ if ("http".equals(id)) { final Protocol http - = new Protocol("http", new DefaultProtocolSocketFactory(), 80); + = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(), 80); Protocol.registerProtocol("http", http); return http; } if ("https".equals(id)) { final Protocol https - = new Protocol("https", new SSLProtocolSocketFactory(), 443); + = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443); Protocol.registerProtocol("https", https); return https; } Index: src/java/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.java =================================================================== retrieving revision 1.5 diff -u -r1.5 ProtocolSocketFactory.java --- src/java/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.java 31 Jan 2003 00:33:36 -0000 1.5 +++ src/java/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.java 24 Jan 2004 15:44:39 -0000 @@ -71,6 +71,12 @@ /** * A factory for creating Sockets. * + *

Both {@link java.lang.Object#equals(java.lang.Object) Object.equals()} and + * {@link java.lang.Object#hashCode() Object.hashCode()} should be overridden appropriately. + * Protocol socket factories are used to uniquely identify Protocols and + * HostConfigurations, and equals() and hashCode() are + * required for the correct operation of some connection managers.

+ * * @see Protocol * * @author Michael Becke Index: src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java =================================================================== retrieving revision 1.5 diff -u -r1.5 SSLProtocolSocketFactory.java --- src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java 31 Jan 2003 00:33:36 -0000 1.5 +++ src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java 24 Jan 2004 15:44:39 -0000 @@ -81,6 +81,19 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory { /** + * The factory singleton. + */ + private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory(); + + /** + * Gets an singleton instance of the SSLProtocolSocketFactory. + * @return a SSLProtocolSocketFactory + */ + static SSLProtocolSocketFactory getSocketFactory() { + return factory; + } + + /** * Constructor for SSLProtocolSocketFactory. */ public SSLProtocolSocketFactory() { @@ -132,4 +145,18 @@ ); } + /** + * All instances of SSLProtocolSocketFactory are the same. + */ + public boolean equals(Object obj) { + return ((obj != null) && obj.getClass().equals(SSLProtocolSocketFactory.class)); + } + + /** + * All instances of SSLProtocolSocketFactory have the same hash code. + */ + public int hashCode() { + return SSLProtocolSocketFactory.class.hashCode(); + } + } Index: src/test/org/apache/commons/httpclient/TestEquals.java =================================================================== RCS file: src/test/org/apache/commons/httpclient/TestEquals.java diff -N src/test/org/apache/commons/httpclient/TestEquals.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/test/org/apache/commons/httpclient/TestEquals.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,65 @@ +package org.apache.commons.httpclient; + +import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; +import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory; + +import junit.framework.TestCase; + +/** + */ +public class TestEquals extends TestCase { + + /** + * + */ + public TestEquals() { + super(); + } + + /** + * @param arg0 + */ + public TestEquals(String arg0) { + super(arg0); + } + + public void testProtocol() { + + Protocol p1 = new Protocol("test", new DefaultProtocolSocketFactory(), 123); + Protocol p2 = new Protocol("test", new DefaultProtocolSocketFactory(), 123); + + assertTrue(p1.equals(p2)); + assertTrue(p2.equals(p1)); + } + + public void testProtocolSocketFactory() { + + ProtocolSocketFactory p1 = new DefaultProtocolSocketFactory(); + ProtocolSocketFactory p2 = new DefaultProtocolSocketFactory(); + + assertTrue(p1.equals(p2)); + assertTrue(p2.equals(p1)); + + p1 = new SSLProtocolSocketFactory(); + p2 = new SSLProtocolSocketFactory(); + + assertTrue(p1.equals(p2)); + assertTrue(p2.equals(p1)); + + } + + public void testHostConfiguration() { + + HostConfiguration hc1 = new HostConfiguration(); + hc1.setHost("http", 80, "http"); + + HostConfiguration hc2 = new HostConfiguration(); + hc2.setHost("http", 80, "http"); + + assertTrue(hc1.equals(hc2)); + assertTrue(hc2.equals(hc1)); + } + +}