001: /**
002: *
003: * Copyright (C) 2000-2006 Enterprise Distributed Technologies Ltd
004: *
005: * www.enterprisedt.com
006: *
007: * Change Log:
008: *
009: * $Log: SocketUtils.java,v $
010: * Revision 1.5 2007-12-18 07:53:07 bruceb
011: * extra debug
012: *
013: * Revision 1.4 2007-05-29 06:07:21 bruceb
014: * use casts to remove compile warnings
015: *
016: * Revision 1.3 2007-05-29 04:16:44 bruceb
017: * added isConnected()
018: *
019: * Revision 1.2 2006/11/02 17:37:31 bruceb
020: * remove unneeded sendUrgentData()
021: *
022: * Revision 1.1 2006/10/27 15:38:01 bruceb
023: * used for connect with timeout
024: *
025: *
026: */package com.enterprisedt.net.ftp;
027:
028: import java.io.IOException;
029: import java.lang.reflect.Constructor;
030: import java.lang.reflect.InvocationTargetException;
031: import java.lang.reflect.Method;
032: import java.net.InetAddress;
033: import java.net.Socket;
034:
035: import com.enterprisedt.util.debug.Logger;
036:
037: /**
038: * Utility class that allows 1.4 socket methods to be called while
039: * still being able to be compiled in 1.1.x
040: *
041: * @author Bruce Blackshaw
042: * @version $Revision: 1.5 $
043: */
044: public class SocketUtils {
045:
046: /**
047: * Logging object
048: */
049: private static Logger log = Logger.getLogger("SocketUtils");
050:
051: /**
052: * Create a connected socket, using a timeout if it is available.
053: * Availability is tested by trying to create instances of the
054: * required classes and methods (JRE 1.4+)
055: *
056: * @param host remote host to connect to
057: * @param port port on remote host
058: * @param timeout timeout in milliseconds on
059: * @exception IOException
060: */
061: public static Socket createSocket(InetAddress host, int port,
062: int timeout) throws IOException {
063:
064: // don't bother going thru the below if a timeout isn't asked for ...
065: if (timeout == 0) {
066: return new Socket(host, port);
067: } else {
068: // attempt to set up 1.4 and later's Socket.connect method which
069: // provides a timeout
070: try {
071: // get the correct connect method
072: Class socketAddress = Class
073: .forName("java.net.SocketAddress");
074: Method connectMethod = Socket.class.getMethod(
075: "connect", new Class[] { socketAddress,
076: int.class });
077:
078: // create an unconnected socket instance
079: Socket sock = (Socket) Socket.class.newInstance();
080:
081: // need an InetSocketAddress instance for connect()
082: Class inetSocketAddress = Class
083: .forName("java.net.InetSocketAddress");
084: Constructor inetSocketAddressCtr = inetSocketAddress
085: .getConstructor(new Class[] {
086: InetAddress.class, int.class });
087: Object address = inetSocketAddressCtr
088: .newInstance(new Object[] { host,
089: new Integer(port) });
090:
091: // now invoke the connect method with the timeout
092: log.debug("Invoking connect with timeout=" + timeout);
093: connectMethod.invoke(sock, new Object[] { address,
094: new Integer(timeout) });
095: log.debug("Connected successfully");
096: return sock;
097: } catch (InvocationTargetException ex) {
098: Throwable target = ex.getTargetException();
099: if (target instanceof IOException)
100: throw (IOException) target;
101: log.debug("Could not use timeout connecting to host ("
102: + ex.toString() + ")");
103: return new Socket(host, port);
104: } catch (Exception ex) {
105: log.debug("Could not use timeout connecting to host ("
106: + ex.toString() + ")");
107: return new Socket(host, port);
108: }
109: }
110: }
111:
112: /**
113: * Test if a socket is connected by using the isConnected method, only
114: * available from JRE 1.4+. So invoke using reflection. If can't check
115: * it assumes the socket is connected.
116: *
117: * @param sock socket to test
118: * @exception IOException
119: */
120: public static boolean isConnected(Socket sock) throws IOException {
121: try {
122: // get the isConnected method
123: Method connectedMethod = Socket.class.getMethod(
124: "isConnected", (Class[]) null);
125:
126: Boolean result = (Boolean) connectedMethod.invoke(sock,
127: (Object[]) null);
128: return result.booleanValue();
129: } catch (InvocationTargetException ex) {
130: Throwable target = ex.getTargetException();
131: if (target instanceof IOException)
132: throw (IOException) target;
133: log.debug("Could not use Socket.isConnected ("
134: + ex.toString() + ")");
135: return true;
136: } catch (Exception ex) {
137: log.debug("Could not use Socket.isConnected ("
138: + ex.toString() + ")");
139: return true;
140: }
141: }
142:
143: }
|