Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/impl/io/PlainSocketFactory.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/io/PlainSocketFactory.java $
3    * $Revision: 280927 $
4    * $Date: 2005-09-14 21:47:21 +0200 (Wed, 14 Sep 2005) $
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  
30  package org.apache.http.impl.io;
31  
32  import java.io.IOException;
33  import java.net.InetAddress;
34  import java.net.Socket;
35  import java.net.UnknownHostException;
36  
37  import org.apache.http.io.SocketFactory;
38  import org.apache.http.params.HttpConnectionParams;
39  import org.apache.http.params.HttpParams;
40  
41  /**
42   * The default class for creating protocol sockets.  This class just uses the
43   * {@link java.net.Socket socket} constructors.
44   * 
45   * @author Michael Becke
46   * 
47   * @since 2.0
48   */
49  public class PlainSocketFactory implements SocketFactory {
50  
51      /**
52       * The factory singleton.
53       */
54      private static final PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory();
55      
56      /**
57       * Gets an singleton instance of the DefaultProtocolSocketFactory.
58       * @return a DefaultProtocolSocketFactory
59       */
60      public static PlainSocketFactory getSocketFactory() {
61          return DEFAULT_FACTORY;
62      }
63      
64      /**
65       * Constructor for DefaultProtocolSocketFactory.
66       */
67      private PlainSocketFactory() {
68          super();
69      }
70  
71      /**
72       * Attempts to get a new socket connection to using old (pre Java 1.4) IO mode.
73       * This socket factory does not support connect timeout as it requires Java 1.4
74       * functionality.
75       *  
76       * @param host the host name/IP
77       * @param port the port on the host
78       * @param localAddress the local host name/IP to bind the socket to
79       * @param localPort the port on the local machine
80       * @param params {@link HttpConnectionParams Http connection parameters}
81       * 
82       * @return Socket a new socket
83       * 
84       * @throws IOException if an I/O error occurs while creating the socket
85       * @throws UnknownHostException if the IP address of the host cannot be
86       * @throws IllegalStateException if connection timeout is set
87       * determined
88       * 
89       * @since 3.0
90       */
91      public Socket createSocket(
92          final String host,
93          final int port,
94          final InetAddress localAddress,
95          final int localPort,
96          final HttpParams params
97      ) throws IOException, UnknownHostException {
98          if (params == null) {
99              throw new IllegalArgumentException("Parameters may not be null");
100         }
101         int timeout = HttpConnectionParams.getConnectionTimeout(params);
102         if (timeout != 0) {
103             throw new IllegalStateException("Connection timeout is not supported in old IO mode");
104         }
105         if (localAddress != null) {
106             return new Socket(host, port, localAddress, localPort);
107         } else {
108             return new Socket(host, port);
109         }
110     }
111 
112     /**
113      * All instances of DefaultProtocolSocketFactory are the same.
114      */
115     public boolean equals(Object obj) {
116         return ((obj != null) && obj.getClass().equals(PlainSocketFactory.class));
117     }
118 
119     /**
120      * All instances of DefaultProtocolSocketFactory have the same hash code.
121      */
122     public int hashCode() {
123         return PlainSocketFactory.class.hashCode();
124     }
125 
126 }