1 11 package org.eclipse.jsch.internal.core; 12 13 import java.io.*; 14 import java.lang.reflect.*; 15 import java.net.*; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 20 import com.jcraft.jsch.SocketFactory; 21 22 public class ResponsiveSocketFactory implements SocketFactory { 23 private static final String JAVA_NET_PROXY="java.net.Proxy"; private static final int DEFAULT_TIMEOUT=60; InputStream in = null; 26 OutputStream out = null; 27 private IProgressMonitor monitor; 28 private final int timeout; 29 private static Class proxyClass; 30 private static boolean hasProxyClass = true; 31 public ResponsiveSocketFactory(IProgressMonitor monitor, int timeout) { 32 if (monitor == null) 33 monitor = new NullProgressMonitor(); 34 this.monitor = monitor; 35 this.timeout=timeout; 36 } 37 public InputStream getInputStream(Socket socket) throws IOException { 38 if (in == null) 39 in = socket.getInputStream(); 40 return in; 41 } 42 public OutputStream getOutputStream(Socket socket) throws IOException { 43 if (out == null) 44 out = socket.getOutputStream(); 45 return out; 46 } 47 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 48 Socket socket = null; 49 socket = createSocket(host, port, timeout / 1000, monitor); 50 monitor = new NullProgressMonitor(); 53 socket.setSoTimeout(timeout); 55 return socket; 56 } 57 58 64 private Socket createSocket(final String host, final int port, int timeout, IProgressMonitor monitor) throws UnknownHostException, IOException { 65 66 final Socket[] socket = new Socket[] { null }; 68 final Exception [] exception = new Exception [] {null }; 69 final Thread thread = new Thread (new Runnable () { 70 public void run() { 71 try { 72 Socket newSocket = internalCreateSocket(host, port); 73 synchronized (socket) { 74 if (Thread.interrupted()) { 75 newSocket.close(); 77 } else { 78 socket[0] = newSocket; 79 } 80 } 81 } catch (UnknownHostException e) { 82 exception[0] = e; 83 } catch (IOException e) { 84 exception[0] = e; 85 } 86 } 87 }); 88 thread.start(); 89 90 if (timeout == 0) timeout = DEFAULT_TIMEOUT; 92 for (int i = 0; i < timeout; i++) { 93 try { 94 thread.join(1000); 96 } catch (InterruptedException e) { 97 } 100 synchronized (socket) { 101 if (monitor.isCanceled()) { 103 if (thread.isAlive()) { 104 thread.interrupt(); 105 } 106 if (socket[0] != null) { 107 socket[0].close(); 108 } 109 Policy.checkCanceled(monitor); 111 } 112 } 113 } 114 synchronized (socket) { 116 if (thread.isAlive()) { 117 thread.interrupt(); 118 } 119 } 120 if (exception[0] != null) { 121 if (exception[0] instanceof UnknownHostException) 122 throw (UnknownHostException)exception[0]; 123 else 124 throw (IOException)exception[0]; 125 } 126 if (socket[0] == null) { 127 throw new InterruptedIOException(NLS.bind(Messages.Util_timeout, new String [] { host })); 128 } 129 return socket[0]; 130 } 131 132 Socket internalCreateSocket(final String host, final int port) 133 throws UnknownHostException, IOException{ 134 Class proxyClass = getProxyClass(); 135 if (proxyClass != null) { 136 try{ 138 139 Field field = proxyClass.getField("NO_PROXY"); Object noProxyObject = field.get(null); 142 Constructor constructor = Socket.class.getConstructor(new Class [] { proxyClass }); 143 Object o = constructor.newInstance(new Object [] { noProxyObject }); 144 if(o instanceof Socket){ 145 Socket socket=(Socket)o; 146 socket.connect(new InetSocketAddress(host, port), timeout * 1000); 147 return socket; 148 } 149 } 150 catch(SecurityException e){ 151 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 153 catch(NoSuchFieldException e){ 154 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 156 catch(IllegalArgumentException e){ 157 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 159 catch(IllegalAccessException e){ 160 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 162 catch(NoSuchMethodException e){ 163 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 165 catch(InstantiationException e){ 166 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 168 catch(InvocationTargetException e){ 169 JSchCorePlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 171 172 } 173 return new Socket(host, port); 174 } 175 176 private synchronized Class getProxyClass() { 177 if (hasProxyClass && proxyClass == null) { 178 try{ 179 proxyClass = Class.forName(JAVA_NET_PROXY); 180 } 181 catch(ClassNotFoundException e){ 182 hasProxyClass = false; 184 } 185 } 186 return proxyClass; 187 } 188 189 } 190 | Popular Tags |