2
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/protocol/ReflectionSocketFactory.java,v 1.4 2004/12/21 23:15:21 olegk Exp $
4
* $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
6
* ====================================================================
8
* Copyright 2002-2004 The Apache Software Foundation
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
* ====================================================================
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/>.
30
package org.apache.commons.httpclient.protocol;
32
import java.io.IOException;
33
import java.lang.reflect.Constructor;
34
import java.lang.reflect.InvocationTargetException;
35
import java.lang.reflect.Method;
36
import java.net.InetAddress;
37
import java.net.Socket;
38
import java.net.UnknownHostException;
40
import org.apache.commons.httpclient.ConnectTimeoutException;
43
* This helper class uses refelction in order to execute Socket methods
44
* available in Java 1.4 and above
46
* @author Oleg Kalnichevski
50
public final class ReflectionSocketFactory {
52
private static boolean REFLECTION_FAILED = false;
54
private static Constructor INETSOCKETADDRESS_CONSTRUCTOR = null;
55
private static Method SOCKETCONNECT_METHOD = null;
56
private static Method SOCKETBIND_METHOD = null;
57
private static Class SOCKETTIMEOUTEXCEPTION_CLASS = null;
59
private ReflectionSocketFactory() {
64
* This method attempts to execute Socket method available since Java 1.4
65
* using reflection. If the methods are not available or could not be executed
66
* <tt>null</tt> is returned
68
* @param socketfactoryName name of the socket factory class
69
* @param host the host name/IP
70
* @param port the port on the host
71
* @param localAddress the local host name/IP to bind the socket to
72
* @param localPort the port on the local machine
73
* @param timeout the timeout value to be used in milliseconds. If the socket cannot be
74
* completed within the given time limit, it will be abandoned
76
* @return a connected Socket
78
* @throws IOException if an I/O error occurs while creating the socket
79
* @throws UnknownHostException if the IP address of the host cannot be
81
* @throws ConnectTimeoutException if socket cannot be connected within the
85
public static Socket createSocket(
86
final String socketfactoryName,
89
final InetAddress localAddress,
92
throws IOException, UnknownHostException, ConnectTimeoutException
94
if (REFLECTION_FAILED) {
95
//This is known to have failed before. Do not try it again
98
// This code uses reflection to essentially do the following:
100
// SocketFactory socketFactory = Class.forName(socketfactoryName).getDefault();
101
// Socket socket = socketFactory.createSocket();
102
// SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
103
// SocketAddress remoteaddr = new InetSocketAddress(host, port);
104
// socket.bind(localaddr);
105
// socket.connect(remoteaddr, timeout);
108
Class socketfactoryClass = Class.forName(socketfactoryName);
109
Method method = socketfactoryClass.getMethod("getDefault",
111
Object socketfactory = method.invoke(null,
113
method = socketfactoryClass.getMethod("createSocket",
115
Socket socket = (Socket) method.invoke(socketfactory, new Object[] {});
117
if (INETSOCKETADDRESS_CONSTRUCTOR == null) {
118
Class addressClass = Class.forName("java.net.InetSocketAddress");
119
INETSOCKETADDRESS_CONSTRUCTOR = addressClass.getConstructor(
120
new Class[] { InetAddress.class, Integer.TYPE });
123
Object remoteaddr = INETSOCKETADDRESS_CONSTRUCTOR.newInstance(
124
new Object[] { InetAddress.getByName(host), new Integer(port)});
126
Object localaddr = INETSOCKETADDRESS_CONSTRUCTOR.newInstance(
127
new Object[] { localAddress, new Integer(localPort)});
129
if (SOCKETCONNECT_METHOD == null) {
130
SOCKETCONNECT_METHOD = Socket.class.getMethod("connect",
131
new Class[] {Class.forName("java.net.SocketAddress"), Integer.TYPE});
134
if (SOCKETBIND_METHOD == null) {
135
SOCKETBIND_METHOD = Socket.class.getMethod("bind",
136
new Class[] {Class.forName("java.net.SocketAddress")});
138
SOCKETBIND_METHOD.invoke(socket, new Object[] { localaddr});
139
SOCKETCONNECT_METHOD.invoke(socket, new Object[] { remoteaddr, new Integer(timeout)});
142
catch (InvocationTargetException e) {
143
Throwable cause = e.getTargetException();
144
if (SOCKETTIMEOUTEXCEPTION_CLASS == null) {
146
SOCKETTIMEOUTEXCEPTION_CLASS = Class.forName("java.net.SocketTimeoutException");
147
} catch (ClassNotFoundException ex) {
148
// At this point this should never happen. Really.
149
REFLECTION_FAILED = true;
153
if (SOCKETTIMEOUTEXCEPTION_CLASS.isInstance(cause)) {
154
throw new ConnectTimeoutException(
155
"The host did not accept the connection within timeout of "
156
+ timeout + " ms", cause);
158
if (cause instanceof IOException) {
159
throw (IOException)cause;
163
catch (Exception e) {
164
REFLECTION_FAILED = true;