1 11 package org.eclipse.team.internal.ccvs.core.util; 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 import org.eclipse.team.internal.ccvs.core.*; 20 21 import com.jcraft.jsch.SocketFactory; 22 23 26 public class ResponsiveSocketFactory implements SocketFactory { 27 private static final String JAVA_NET_PROXY="java.net.Proxy"; private static final int DEFAULT_TIMEOUT=60; InputStream in = null; 30 OutputStream out = null; 31 private IProgressMonitor monitor; 32 private final int timeout; 33 private static Class proxyClass; 34 private static boolean hasProxyClass = true; 35 public ResponsiveSocketFactory(IProgressMonitor monitor, int timeout) { 36 if (monitor == null) 37 monitor = new NullProgressMonitor(); 38 this.monitor = monitor; 39 this.timeout=timeout; 40 } 41 public InputStream getInputStream(Socket socket) throws IOException { 42 if (in == null) 43 in = socket.getInputStream(); 44 return in; 45 } 46 public OutputStream getOutputStream(Socket socket) throws IOException { 47 if (out == null) 48 out = socket.getOutputStream(); 49 return out; 50 } 51 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 52 Socket socket = null; 53 socket = createSocket(host, port, timeout / 1000, monitor); 54 monitor = new NullProgressMonitor(); 57 socket.setSoTimeout(timeout); 59 return socket; 60 } 61 62 68 private Socket createSocket(final String host, final int port, int timeout, IProgressMonitor monitor) throws UnknownHostException, IOException { 69 70 final Socket[] socket = new Socket[] { null }; 72 final Exception [] exception = new Exception [] {null }; 73 final Thread thread = new Thread (new Runnable () { 74 public void run() { 75 try { 76 Socket newSocket = internalCreateSocket(host, port); 77 synchronized (socket) { 78 if (Thread.interrupted()) { 79 newSocket.close(); 81 } else { 82 socket[0] = newSocket; 83 } 84 } 85 } catch (UnknownHostException e) { 86 exception[0] = e; 87 } catch (IOException e) { 88 exception[0] = e; 89 } 90 } 91 }); 92 thread.start(); 93 94 if (timeout == 0) timeout = DEFAULT_TIMEOUT; 96 for (int i = 0; i < timeout; i++) { 97 try { 98 thread.join(1000); 100 } catch (InterruptedException e) { 101 } 104 synchronized (socket) { 105 if (monitor.isCanceled()) { 107 if (thread.isAlive()) { 108 thread.interrupt(); 109 } 110 if (socket[0] != null) { 111 socket[0].close(); 112 } 113 Policy.checkCanceled(monitor); 115 } 116 } 117 } 118 synchronized (socket) { 120 if (thread.isAlive()) { 121 thread.interrupt(); 122 } 123 } 124 if (exception[0] != null) { 125 if (exception[0] instanceof UnknownHostException) 126 throw (UnknownHostException)exception[0]; 127 else 128 throw (IOException)exception[0]; 129 } 130 if (socket[0] == null) { 131 throw new InterruptedIOException(NLS.bind(CVSMessages.Util_timeout, new String [] { host })); 132 } 133 return socket[0]; 134 } 135 136 Socket internalCreateSocket(final String host, final int port) 137 throws UnknownHostException, IOException{ 138 Class proxyClass = getProxyClass(); 139 if (proxyClass != null) { 140 try{ 142 143 Field field = proxyClass.getField("NO_PROXY"); Object noProxyObject = field.get(null); 146 Constructor constructor = Socket.class.getConstructor(new Class [] { proxyClass }); 147 Object o = constructor.newInstance(new Object [] { noProxyObject }); 148 if(o instanceof Socket){ 149 Socket socket=(Socket)o; 150 socket.connect(new InetSocketAddress(host, port), timeout * 1000); 151 return socket; 152 } 153 } 154 catch(SecurityException e){ 155 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 157 catch(NoSuchFieldException e){ 158 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 160 catch(IllegalArgumentException e){ 161 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 163 catch(IllegalAccessException e){ 164 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 166 catch(NoSuchMethodException e){ 167 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 169 catch(InstantiationException e){ 170 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 172 catch(InvocationTargetException e){ 173 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An internal error occurred while connecting to {0}", host), e); } 175 176 } 177 return new Socket(host, port); 178 } 179 180 private synchronized Class getProxyClass() { 181 if (hasProxyClass && proxyClass == null) { 182 try{ 183 proxyClass = Class.forName(JAVA_NET_PROXY); 184 } 185 catch(ClassNotFoundException e){ 186 hasProxyClass = false; 188 } 189 } 190 return proxyClass; 191 } 192 193 } 194 | Popular Tags |