1 11 package org.eclipse.update.internal.core; 12 13 import java.io.*; 14 import java.net.*; 15 16 import org.eclipse.core.runtime.*; 17 18 public class OtherResponse implements Response { 19 private static final long POLLING_INTERVAL = 200; 20 protected URL url; 21 protected InputStream in; 22 protected URLConnection connection; 23 protected long lastModified; 24 25 public OtherResponse(URL url) throws IOException { 26 this.url = url; 27 } 29 30 public InputStream getInputStream() throws IOException { 31 if (in == null && url != null) { 32 if (connection == null) 33 connection = url.openConnection(); 34 in = connection.getInputStream(); 35 this.lastModified = connection.getLastModified(); 36 } 37 return in; 38 } 39 42 public InputStream getInputStream(IProgressMonitor monitor) 43 throws IOException, CoreException { 44 if (in == null && url != null) { 45 if (connection == null) 46 connection = url.openConnection(); 47 48 if (monitor != null) { 49 this.in = 50 openStreamWithCancel(connection, monitor); 51 } else { 52 this.in = connection.getInputStream(); 53 } 54 if (in != null) { 55 this.lastModified = connection.getLastModified(); 56 } 57 } 58 return in; 59 } 60 61 public long getContentLength() { 62 if (connection != null) 63 return connection.getContentLength(); 64 return 0; 65 } 66 67 public int getStatusCode() { 68 return IStatusCodes.HTTP_OK; 69 } 70 71 public String getStatusMessage() { 72 return ""; } 74 75 public long getLastModified() { 76 if (lastModified == 0 && connection != null) { 77 lastModified = connection.getLastModified(); 78 } 79 return lastModified; 80 } 81 82 private InputStream openStreamWithCancel( 83 URLConnection urlConnection, 84 IProgressMonitor monitor) 85 throws IOException, CoreException { 86 ConnectionThreadManager.StreamRunnable runnable = 87 new ConnectionThreadManager.StreamRunnable(urlConnection); 88 Thread t = 89 UpdateCore.getPlugin().getConnectionManager().createThread( 90 runnable); 91 t.start(); 92 InputStream is = null; 93 try { 94 for (;;) { 95 if (monitor.isCanceled()) { 96 runnable.disconnect(); 97 connection = null; 98 break; 99 } 100 if (runnable.getInputStream() != null) { 101 is = runnable.getInputStream(); 102 break; 103 } 104 if (runnable.getException() != null) { 105 if (runnable.getException() instanceof IOException) 106 throw (IOException) runnable.getException(); 107 else 108 throw new CoreException(new Status(IStatus.ERROR, 109 UpdateCore.getPlugin().getBundle() 110 .getSymbolicName(), IStatus.OK, 111 runnable.getException().getMessage(), runnable 112 .getException())); 113 } 114 t.join(POLLING_INTERVAL); 115 } 116 } catch (InterruptedException e) { 117 } 118 return is; 119 } 120 } 121 | Popular Tags |