More robust dealing with failed downloads -- simply try next location until one works or we're out of options.

This commit is contained in:
Marc Schröder 2012-05-07 14:37:09 +02:00
Родитель 2206af4293
Коммит 72d57b8f8e
1 изменённых файлов: 21 добавлений и 16 удалений

Просмотреть файл

@ -701,16 +701,16 @@ public class ComponentDescription extends Observable implements Comparable<Compo
InputStream stream = null; InputStream stream = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
Exception connectException = null;
URL connectedURL = null;
for (URL u : locations) { for (URL u : locations) {
try { try {
System.out.println("Trying location "+u+"...");
// Open connection to URL. // Open connection to URL.
connection = openAndRedirectIfRequired(u); connection = openAndRedirectIfRequired(u);
// Make sure response code is in the 200 range. // Make sure response code is in the 200 range.
if (connection.getResponseCode() / 100 != 2) { if (connection.getResponseCode() / 100 != 2) {
throw new IOException("Non-OK response code: "+connection.getResponseCode()+" ("+connection.getResponseMessage()+")"); throw new IOException("Non-OK response code: "+connection.getResponseCode()+" ("+connection.getResponseMessage()+")");
} }
System.out.println("...connected");
// Check for valid content length. // Check for valid content length.
int contentLength = connection.getContentLength(); int contentLength = connection.getContentLength();
if (contentLength > -1) { if (contentLength > -1) {
@ -725,27 +725,31 @@ public class ComponentDescription extends Observable implements Comparable<Compo
size = packageSize; size = packageSize;
stateChanged(); stateChanged();
} }
connectException = null; System.out.println("...downloading"+(downloaded > 0 ? " from byte "+downloaded : ""));
connectedURL = u; boolean success = tryToDownloadFromLocation(file, stream, connection);
if (success) {
break; // current location seems OK, leave loop break; // current location seems OK, leave loop
}
} catch (Exception exc) { } catch (Exception exc) {
connectException = exc; exc.printStackTrace();
} }
} }
if (connectException != null) {
connectException.printStackTrace();
error();
return;
} }
System.out.println("Connected to "+connectedURL+", downloading "+(downloaded > 0 ? "from byte "+downloaded : "")); private boolean tryToDownloadFromLocation(RandomAccessFile file,
InputStream stream, HttpURLConnection connection) {
boolean success = false;
try { try {
// Open file and seek to the end of it. // Open file and seek to the end of it.
file = new RandomAccessFile(archiveFile, "rw"); file = new RandomAccessFile(archiveFile, "rw");
file.seek(downloaded); file.seek(downloaded);
stream = connection.getInputStream(); stream = connection.getInputStream();
if (status == Status.ERROR) {
downloaded = 0;
}
status = Status.DOWNLOADING;
byte[] buffer = new byte[MAX_BUFFER_SIZE]; byte[] buffer = new byte[MAX_BUFFER_SIZE];
while (status == Status.DOWNLOADING) { while (status == Status.DOWNLOADING) {
/* target number of bytes to download depends on how much of the /* target number of bytes to download depends on how much of the
@ -774,6 +778,7 @@ public class ComponentDescription extends Observable implements Comparable<Compo
System.err.println("ok!"); System.err.println("ok!");
writeDownloadedComponentXML(); writeDownloadedComponentXML();
status = Status.DOWNLOADED; status = Status.DOWNLOADED;
success = true;
} else { } else {
System.err.println("failed!"); System.err.println("failed!");
System.out.println("MD5 according to component description: "+packageMD5); System.out.println("MD5 according to component description: "+packageMD5);
@ -801,7 +806,7 @@ public class ComponentDescription extends Observable implements Comparable<Compo
} catch (Exception e) {} } catch (Exception e) {}
} }
} }
return success;
} }
} }