1 11 package org.eclipse.update.internal.core.connection; 12 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.HttpURLConnection ; 17 import java.net.URL ; 18 import java.net.URLConnection ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.MultiStatus; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.update.internal.core.Messages; 28 import org.eclipse.update.internal.core.UpdateCore; 29 30 48 public class ConnectionThreadManager { 49 50 private static final String CONNECT_TIMEOUT = "60000"; private static final String READ_TIMEOUT = "60000"; private static final int MAX_COUNT = 9; 56 private List threads = new ArrayList (MAX_COUNT); 57 58 59 60 public static class StreamRunnable implements Runnable { 61 62 private URLConnection urlConnection; 63 private IOException ioException; 64 private Exception exception; 65 private InputStream is; 66 private boolean disconnected; 67 68 public StreamRunnable(URLConnection urlConnection) { 69 this.urlConnection = urlConnection; 70 } 71 72 public InputStream getInputStream() { 73 return is; 74 } 75 76 public URL getURL() { 77 return urlConnection.getURL(); 78 } 79 80 public IOException getIOException() { 81 return ioException; 82 } 83 84 public Exception getException() { 85 return exception; 86 } 87 88 public void disconnect() { 89 if (urlConnection instanceof HttpURLConnection ) 90 ((HttpURLConnection )urlConnection).disconnect(); 91 disconnected = true; 92 } 93 94 public void run() { 95 try { 96 is = urlConnection.getInputStream(); 97 if (disconnected) { 98 if (is != null) { 104 try { 105 is.close(); 106 } catch (IOException ex) { 107 } finally { 109 is = null; 110 } 111 } 112 } 113 } catch (IOException e) { 114 ioException = e; 115 } catch (Exception e) { 116 exception = e; 117 } finally { 118 } 120 } 121 } 122 123 124 class ConnectionThread extends Thread { 125 126 private StreamRunnable runnable; 127 128 public ConnectionThread(StreamRunnable runnable) { 129 super(runnable, "update-connection"); this.runnable = runnable; 131 } 132 133 public StreamRunnable getRunnable() { 134 return runnable; 135 } 136 } 137 138 public ConnectionThreadManager() { 139 setIfNotDefaultProperty("sun.net.client.defaultConnectTimeout", CONNECT_TIMEOUT); setIfNotDefaultProperty("sun.net.client.defaultReadTimeout", READ_TIMEOUT); } 143 144 private void setIfNotDefaultProperty(String key, String value) { 145 String oldValue = System.getProperty(key); 146 if (oldValue==null || oldValue.equals("-1")) System.setProperty(key, value); 148 } 149 150 public Thread getConnectionThread(StreamRunnable runnable) throws CoreException { 151 152 validateExistingThreads(); 153 154 Thread t = new Thread (runnable); 157 t.setDaemon(true); 158 threads.add(t); 159 return t; 160 } 161 162 167 private void validateExistingThreads() throws CoreException { 168 169 if ((threads == null) || (threads.size() == 0)) 170 return; 171 172 int aliveCount = purgeTerminatedThreads(); 173 174 if (aliveCount > MAX_COUNT) { 175 ArrayList children = new ArrayList (); 176 String pluginId = 177 UpdateCore.getPlugin().getBundle().getSymbolicName(); 178 for (int i = 0; i < threads.size(); i++) { 179 ConnectionThread t = (ConnectionThread) threads.get(i); 180 String url = t.getRunnable().getURL().toString(); 181 IStatus status = 182 new Status( 183 IStatus.ERROR, 184 pluginId, 185 IStatus.OK, 186 NLS.bind(Messages.ConnectionThreadManager_unresponsiveURL, (new String [] { url })), 187 null); 188 children.add(status); 189 } 190 MultiStatus parentStatus = 191 new MultiStatus( 192 pluginId, 193 IStatus.OK, 194 (IStatus[]) children.toArray(new IStatus[children.size()]), 195 Messages.ConnectionThreadManager_tooManyConnections, 196 null); 197 throw new CoreException(parentStatus); 198 } 199 } 200 201 205 206 private int purgeTerminatedThreads() { 207 208 if (threads.size() == 0) { 209 return 0; 210 } 211 212 int aliveCount = 0; 213 214 Object [] array = threads.toArray(); 215 for (int i = 0; i < array.length; i++) { 216 Thread t = (Thread ) array[i]; 217 if (!t.isAlive()) 218 threads.remove(t); 219 else 220 aliveCount++; 221 } 222 return aliveCount; 223 } 224 225 public void shutdown() { 226 threads.clear(); 230 } 231 } 232 | Popular Tags |