1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java,v 1.10 2004/05/13 04:01:22 mbecke Exp $
  3. * $Revision: 1.10 $
  4. * $Date: 2004/05/13 04:01:22 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.protocol;
  30. import java.io.IOException;
  31. import java.net.InetAddress;
  32. import java.net.Socket;
  33. import java.net.UnknownHostException;
  34. import org.apache.commons.httpclient.ConnectTimeoutException;
  35. import org.apache.commons.httpclient.params.HttpConnectionParams;
  36. /**
  37. * The default class for creating protocol sockets. This class just uses the
  38. * {@link java.net.Socket socket} constructors.
  39. *
  40. * @author Michael Becke
  41. *
  42. * @since 2.0
  43. */
  44. public class DefaultProtocolSocketFactory implements ProtocolSocketFactory {
  45. /**
  46. * The factory singleton.
  47. */
  48. private static final DefaultProtocolSocketFactory factory = new DefaultProtocolSocketFactory();
  49. /**
  50. * Gets an singleton instance of the DefaultProtocolSocketFactory.
  51. * @return a DefaultProtocolSocketFactory
  52. */
  53. static DefaultProtocolSocketFactory getSocketFactory() {
  54. return factory;
  55. }
  56. /**
  57. * Constructor for DefaultProtocolSocketFactory.
  58. */
  59. public DefaultProtocolSocketFactory() {
  60. super();
  61. }
  62. /**
  63. * @see #createSocket(java.lang.String,int,java.net.InetAddress,int)
  64. */
  65. public Socket createSocket(
  66. String host,
  67. int port,
  68. InetAddress localAddress,
  69. int localPort
  70. ) throws IOException, UnknownHostException {
  71. return new Socket(host, port, localAddress, localPort);
  72. }
  73. /**
  74. * Attempts to get a new socket connection to the given host within the given time limit.
  75. * <p>
  76. * This method employs several techniques to circumvent the limitations of older JREs that
  77. * do not support connect timeout. When running in JRE 1.4 or above reflection is used to
  78. * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older
  79. * JREs a controller thread is executed. The controller thread attempts to create a new socket
  80. * within the given limit of time. If socket constructor does not return until the timeout
  81. * expires, the controller terminates and throws an {@link ConnectTimeoutException}
  82. * </p>
  83. *
  84. * @param host the host name/IP
  85. * @param port the port on the host
  86. * @param localAddress the local host name/IP to bind the socket to
  87. * @param localPort the port on the local machine
  88. * @param params {@link HttpConnectionParams Http connection parameters}
  89. *
  90. * @return Socket a new socket
  91. *
  92. * @throws IOException if an I/O error occurs while creating the socket
  93. * @throws UnknownHostException if the IP address of the host cannot be
  94. * determined
  95. * @throws ConnectTimeoutException if socket cannot be connected within the
  96. * given time limit
  97. *
  98. * @since 3.0
  99. */
  100. public Socket createSocket(
  101. final String host,
  102. final int port,
  103. final InetAddress localAddress,
  104. final int localPort,
  105. final HttpConnectionParams params
  106. ) throws IOException, UnknownHostException, ConnectTimeoutException {
  107. if (params == null) {
  108. throw new IllegalArgumentException("Parameters may not be null");
  109. }
  110. int timeout = params.getConnectionTimeout();
  111. if (timeout == 0) {
  112. return createSocket(host, port, localAddress, localPort);
  113. } else {
  114. // To be eventually deprecated when migrated to Java 1.4 or above
  115. Socket socket = ReflectionSocketFactory.createSocket(
  116. "javax.net.SocketFactory", host, port, localAddress, localPort, timeout);
  117. if (socket == null) {
  118. socket = ControllerThreadSocketFactory.createSocket(
  119. this, host, port, localAddress, localPort, timeout);
  120. }
  121. return socket;
  122. }
  123. }
  124. /**
  125. * @see ProtocolSocketFactory#createSocket(java.lang.String,int)
  126. */
  127. public Socket createSocket(String host, int port)
  128. throws IOException, UnknownHostException {
  129. return new Socket(host, port);
  130. }
  131. /**
  132. * All instances of DefaultProtocolSocketFactory are the same.
  133. */
  134. public boolean equals(Object obj) {
  135. return ((obj != null) && obj.getClass().equals(DefaultProtocolSocketFactory.class));
  136. }
  137. /**
  138. * All instances of DefaultProtocolSocketFactory have the same hash code.
  139. */
  140. public int hashCode() {
  141. return DefaultProtocolSocketFactory.class.hashCode();
  142. }
  143. }