001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.protocol;
032:
033: import java.io.IOException;
034: import java.net.InetAddress;
035: import java.net.Socket;
036: import java.net.UnknownHostException;
037:
038: import org.apache.commons.httpclient.ConnectTimeoutException;
039: import org.apache.commons.httpclient.params.HttpConnectionParams;
040:
041: /**
042: * The default class for creating protocol sockets. This class just uses the
043: * {@link java.net.Socket socket} constructors.
044: *
045: * @author Michael Becke
046: *
047: * @since 2.0
048: */
049: public class DefaultProtocolSocketFactory implements
050: ProtocolSocketFactory {
051:
052: /**
053: * The factory singleton.
054: */
055: private static final DefaultProtocolSocketFactory factory = new DefaultProtocolSocketFactory();
056:
057: /**
058: * Gets an singleton instance of the DefaultProtocolSocketFactory.
059: * @return a DefaultProtocolSocketFactory
060: */
061: static DefaultProtocolSocketFactory getSocketFactory() {
062: return factory;
063: }
064:
065: /**
066: * Constructor for DefaultProtocolSocketFactory.
067: */
068: public DefaultProtocolSocketFactory() {
069: super ();
070: }
071:
072: /**
073: * @see #createSocket(java.lang.String,int,java.net.InetAddress,int)
074: */
075: public Socket createSocket(String host, int port,
076: InetAddress localAddress, int localPort)
077: throws IOException, UnknownHostException {
078: return new Socket(host, port, localAddress, localPort);
079: }
080:
081: /**
082: * Attempts to get a new socket connection to the given host within the given time limit.
083: * <p>
084: * This method employs several techniques to circumvent the limitations of older JREs that
085: * do not support connect timeout. When running in JRE 1.4 or above reflection is used to
086: * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older
087: * JREs a controller thread is executed. The controller thread attempts to create a new socket
088: * within the given limit of time. If socket constructor does not return until the timeout
089: * expires, the controller terminates and throws an {@link ConnectTimeoutException}
090: * </p>
091: *
092: * @param host the host name/IP
093: * @param port the port on the host
094: * @param localAddress the local host name/IP to bind the socket to
095: * @param localPort the port on the local machine
096: * @param params {@link HttpConnectionParams Http connection parameters}
097: *
098: * @return Socket a new socket
099: *
100: * @throws IOException if an I/O error occurs while creating the socket
101: * @throws UnknownHostException if the IP address of the host cannot be
102: * determined
103: * @throws ConnectTimeoutException if socket cannot be connected within the
104: * given time limit
105: *
106: * @since 3.0
107: */
108: public Socket createSocket(final String host, final int port,
109: final InetAddress localAddress, final int localPort,
110: final HttpConnectionParams params) throws IOException,
111: UnknownHostException, ConnectTimeoutException {
112: if (params == null) {
113: throw new IllegalArgumentException(
114: "Parameters may not be null");
115: }
116: int timeout = params.getConnectionTimeout();
117: if (timeout == 0) {
118: return createSocket(host, port, localAddress, localPort);
119: } else {
120: // To be eventually deprecated when migrated to Java 1.4 or above
121: Socket socket = ReflectionSocketFactory.createSocket(
122: "javax.net.SocketFactory", host, port,
123: localAddress, localPort, timeout);
124: if (socket == null) {
125: socket = ControllerThreadSocketFactory.createSocket(
126: this , host, port, localAddress, localPort,
127: timeout);
128: }
129: return socket;
130: }
131: }
132:
133: /**
134: * @see ProtocolSocketFactory#createSocket(java.lang.String,int)
135: */
136: public Socket createSocket(String host, int port)
137: throws IOException, UnknownHostException {
138: return new Socket(host, port);
139: }
140:
141: /**
142: * All instances of DefaultProtocolSocketFactory are the same.
143: */
144: public boolean equals(Object obj) {
145: return ((obj != null) && obj.getClass().equals(getClass()));
146: }
147:
148: /**
149: * All instances of DefaultProtocolSocketFactory have the same hash code.
150: */
151: public int hashCode() {
152: return getClass().hashCode();
153: }
154:
155: }
|