PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/ApacheHttpClient/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java

http://google-enterprise-connector-sharepoint.googlecode.com/
Java | 164 lines | 73 code | 11 blank | 80 comment | 4 complexity | 023495c30107df909b296e4ab3e91bc0 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, CPL-1.0, Apache-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. /*
  2. * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java,v 1.2 2004/04/18 23:51:38 jsdever Exp $
  3. * $Revision: 480424 $
  4. * $Date: 2006-11-29 11:26:49 +0530 (Wed, 29 Nov 2006) $
  5. *
  6. * ====================================================================
  7. *
  8. * Licensed to the Apache Software Foundation (ASF) under one or more
  9. * contributor license agreements. See the NOTICE file distributed with
  10. * this work for additional information regarding copyright ownership.
  11. * The ASF licenses this file to You under the Apache License, Version 2.0
  12. * (the "License"); you may not use this file except in compliance with
  13. * the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ====================================================================
  23. *
  24. * This software consists of voluntary contributions made by many
  25. * individuals on behalf of the Apache Software Foundation. For more
  26. * information on the Apache Software Foundation, please see
  27. * <http://www.apache.org/>.
  28. *
  29. */
  30. package org.apache.commons.httpclient.protocol;
  31. import java.io.IOException;
  32. import java.net.InetAddress;
  33. import java.net.Socket;
  34. import java.net.UnknownHostException;
  35. import org.apache.commons.httpclient.ConnectTimeoutException;
  36. import org.apache.commons.httpclient.util.TimeoutController;
  37. /**
  38. * This helper class is intedned to help work around the limitation of older Java versions
  39. * (older than 1.4) that prevents from specifying a connection timeout when creating a
  40. * socket. This factory executes a controller thread overssing the process of socket
  41. * initialisation. If the socket constructor cannot be created within the specified time
  42. * limit, the controller terminates and throws an {@link ConnectTimeoutException}
  43. *
  44. * @author Ortwin Glueck
  45. * @author Oleg Kalnichevski
  46. *
  47. * @since 3.0
  48. */
  49. public final class ControllerThreadSocketFactory {
  50. private ControllerThreadSocketFactory() {
  51. super();
  52. }
  53. /**
  54. * This method spawns a controller thread overseeing the process of socket
  55. * initialisation. If the socket constructor cannot be created within the specified time
  56. * limit, the controller terminates and throws an {@link ConnectTimeoutException}
  57. *
  58. * @param host the host name/IP
  59. * @param port the port on the host
  60. * @param localAddress the local host name/IP to bind the socket to
  61. * @param localPort the port on the local machine
  62. * @param timeout the timeout value to be used in milliseconds. If the socket cannot be
  63. * completed within the given time limit, it will be abandoned
  64. *
  65. * @return a connected Socket
  66. *
  67. * @throws IOException if an I/O error occurs while creating the socket
  68. * @throws UnknownHostException if the IP address of the host cannot be
  69. * determined
  70. * @throws ConnectTimeoutException if socket cannot be connected within the
  71. * given time limit
  72. *
  73. */
  74. public static Socket createSocket(
  75. final ProtocolSocketFactory socketfactory,
  76. final String host,
  77. final int port,
  78. final InetAddress localAddress,
  79. final int localPort,
  80. int timeout)
  81. throws IOException, UnknownHostException, ConnectTimeoutException
  82. {
  83. SocketTask task = new SocketTask() {
  84. public void doit() throws IOException {
  85. setSocket(socketfactory.createSocket(host, port, localAddress, localPort));
  86. }
  87. };
  88. try {
  89. TimeoutController.execute(task, timeout);
  90. } catch (TimeoutController.TimeoutException e) {
  91. throw new ConnectTimeoutException(
  92. "The host did not accept the connection within timeout of "
  93. + timeout + " ms");
  94. }
  95. Socket socket = task.getSocket();
  96. if (task.exception != null) {
  97. throw task.exception;
  98. }
  99. return socket;
  100. }
  101. public static Socket createSocket(final SocketTask task, int timeout)
  102. throws IOException, UnknownHostException, ConnectTimeoutException
  103. {
  104. try {
  105. TimeoutController.execute(task, timeout);
  106. } catch (TimeoutController.TimeoutException e) {
  107. throw new ConnectTimeoutException(
  108. "The host did not accept the connection within timeout of "
  109. + timeout + " ms");
  110. }
  111. Socket socket = task.getSocket();
  112. if (task.exception != null) {
  113. throw task.exception;
  114. }
  115. return socket;
  116. }
  117. /**
  118. * Helper class for wrapping socket based tasks.
  119. */
  120. public static abstract class SocketTask implements Runnable {
  121. /** The socket */
  122. private Socket socket;
  123. /** The exception */
  124. private IOException exception;
  125. /**
  126. * Set the socket.
  127. * @param newSocket The new socket.
  128. */
  129. protected void setSocket(final Socket newSocket) {
  130. socket = newSocket;
  131. }
  132. /**
  133. * Return the socket.
  134. * @return Socket The socket.
  135. */
  136. protected Socket getSocket() {
  137. return socket;
  138. }
  139. /**
  140. * Perform the logic.
  141. * @throws IOException If an IO problem occurs
  142. */
  143. public abstract void doit() throws IOException;
  144. /** Execute the logic in this object and keep track of any exceptions. */
  145. public void run() {
  146. try {
  147. doit();
  148. } catch (IOException e) {
  149. exception = e;
  150. }
  151. }
  152. }
  153. }