1 22 package org.jnp.interfaces; 23 24 import java.io.IOException ; 25 import java.net.ConnectException ; 26 import java.net.InetAddress ; 27 import java.net.Socket ; 28 import java.net.UnknownHostException ; 29 import java.util.Hashtable ; 30 import javax.net.SocketFactory; 31 32 39 public class TimedSocketFactory extends SocketFactory 40 { 41 public static final String JNP_TIMEOUT = "jnp.timeout"; 42 public static final String JNP_SO_TIMEOUT = "jnp.sotimeout"; 43 44 45 protected int timeout = 0; 46 47 protected int soTimeout = 0; 48 49 50 public TimedSocketFactory() 51 { 52 } 53 public TimedSocketFactory(Hashtable env) 54 { 55 String value = (String ) env.get(JNP_TIMEOUT); 56 if( value != null ) 57 timeout = Integer.parseInt(value); 58 value = (String ) env.get(JNP_SO_TIMEOUT); 59 if( value != null ) 60 soTimeout = Integer.parseInt(value); 61 } 62 63 public Socket createSocket(String host, int port) throws IOException , UnknownHostException 64 { 65 InetAddress hostAddr = InetAddress.getByName(host); 66 return this.createSocket(hostAddr, port, null, 0); 67 } 68 public Socket createSocket(InetAddress hostAddr, int port) throws IOException 69 { 70 return this.createSocket(hostAddr, port, null, 0); 71 } 72 73 public Socket createSocket(String host, int port, InetAddress localAddr, int localPort) 74 throws IOException , UnknownHostException 75 { 76 InetAddress hostAddr = InetAddress.getByName(host); 77 return this.createSocket(hostAddr, port, localAddr, localPort); 78 } 79 public Socket createSocket(InetAddress hostAddr, int port, InetAddress localAddr, int localPort) 80 throws IOException 81 { 82 Socket socket = null; 83 if( timeout <= 0 ) 84 socket = new Socket (hostAddr, port, localAddr, localPort); 85 else 86 socket = createSocket(hostAddr, port, localAddr, localPort, timeout); 87 88 socket.setSoTimeout(soTimeout); 89 return socket; 90 } 91 92 protected Socket createSocket(InetAddress hostAddr, int port, 93 InetAddress localAddr, int localPort, int connectTimeout) 94 throws IOException 95 { 96 ConnectThread t = new ConnectThread(); 97 Socket socket = t.createSocket(hostAddr, port, localAddr, localPort, connectTimeout); 98 return socket; 99 } 100 101 104 class ConnectThread extends Thread 105 { 106 IOException ex; 107 InetAddress hostAddr; 108 InetAddress localAddr; 109 int port; 110 int localPort; 111 int connectTimeout; 112 Socket socket; 113 114 ConnectThread() 115 { 116 super("JNP ConnectThread"); 117 super.setDaemon(true); 118 } 119 120 124 Socket createSocket(InetAddress hostAddr, int port, 125 InetAddress localAddr, int localPort, int connectTimeout) 126 throws IOException 127 { 128 this.hostAddr = hostAddr; 129 this.port = port; 130 this.localAddr = localAddr; 131 this.localPort = localPort; 132 this.connectTimeout = connectTimeout; 133 134 try 135 { 136 synchronized( this ) 137 { 138 this.start(); 140 this.wait(connectTimeout); 142 } 143 } 144 catch(InterruptedException e) 145 { 146 throw new ConnectException ("Connect attempt timed out"); 147 } 148 149 if( ex != null ) 151 throw ex; 152 if( socket == null ) 154 throw new ConnectException ("Connect attempt timed out"); 155 156 return socket; 157 } 158 159 public void run() 160 { 161 try 162 { 163 socket = new Socket (hostAddr, port, localAddr, localPort); 164 synchronized( this ) 165 { 166 this.notify(); 167 } 168 } 169 catch(IOException e) 170 { 171 this.ex = e; 172 } 173 } 174 } 175 } 176 | Popular Tags |