SDK Updater: refine error handling when fetching sources.

This commit is contained in:
Raphael
2009-06-30 20:32:40 -07:00
parent 9cf8f819fa
commit 329c13cbe3

View File

@@ -123,8 +123,6 @@ public class RepoSource implements IDescription {
monitor.setProgressMax(4); monitor.setProgressMax(4);
setDefaultDescription(); setDefaultDescription();
mFetchError = null; // reset fetch error
String url = mUrl; String url = mUrl;
if (forceHttp) { if (forceHttp) {
@@ -136,50 +134,68 @@ public class RepoSource implements IDescription {
ByteArrayInputStream xml = null; ByteArrayInputStream xml = null;
try { for (int tentative = 0; tentative < 2 && xml == null; tentative++) {
// reset fetch error and fetch
mFetchError = null;
xml = fetchUrl(url, monitor); xml = fetchUrl(url, monitor);
} catch (FileNotFoundException e1) {
if (!url.endsWith(SdkRepository.URL_DEFAULT_XML_FILE)) { if (xml == null) {
// Try again by explicitely requesting our default file name mDescription += String.format("\nFailed to fetch URL %1$s", url);
mFetchError = "Failed to fetch URL";
monitor.setResult("Failed to fetch URL %1$s", url);
} else {
// We got a document. It might not be XML or it might not be valid.
monitor.setDescription("Validate XML");
if (validateXml(xml, url, monitor)) {
// We got a valid XML, keep it and use it.
if (tentative > 0) {
// If the second tentative succeeded, indicate it in the console,
// otherwise the user will only see the first failure
// message and will think the whole thing failed. This also
// indicates we modifed the URL.
monitor.setResult("Repository found instead at %1$s", url);
}
break;
} else {
mDescription += String.format("\nFailed to validate XML at %1$s", url);
mFetchError = "Failed to validate XML";
monitor.setResult("Failed to validate XML at %1$s", url);
// forget this XML, it wasn't any good.
xml = null;
}
}
// If we failed the first time and the URL doesn't explicitly end with
// our filename, make another tentative. Otherwise abort.
if (tentative == 0 && !url.endsWith(SdkRepository.URL_DEFAULT_XML_FILE)) {
if (!url.endsWith("/")) { //$NON-NLS-1$ if (!url.endsWith("/")) { //$NON-NLS-1$
url += "/"; //$NON-NLS-1$ url += "/"; //$NON-NLS-1$
} }
url += SdkRepository.URL_DEFAULT_XML_FILE; url += SdkRepository.URL_DEFAULT_XML_FILE;
} else {
try { break;
xml = fetchUrl(url, monitor);
} catch (FileNotFoundException e2) {
// pass, xml will be null below
}
} }
} }
if (xml == null) {
mDescription += String.format("\nFailed to fetch URL %1$s", url);
mFetchError = "Failed to fetch URL";
monitor.setResult("Failed to fetch URL %1$s", url);
return;
}
monitor.setDescription("Validate XML");
monitor.incProgress(1); monitor.incProgress(1);
if (!validateXml(xml, monitor)) { if (xml != null) {
mDescription += String.format("\nFailed to validate XML at %1$s", url); monitor.setDescription("Parse XML");
mFetchError = "Failed to validate XML"; monitor.incProgress(1);
monitor.setResult("\nFailed to validate XML at %1$s", url); parsePackages(xml, monitor);
return; if (mPackages.length == 0) {
} mDescription += "\nNo packages found.";
} else if (mPackages.length == 1) {
monitor.setDescription("Parse XML"); mDescription += "\nOne package found.";
monitor.incProgress(1); } else {
parsePackages(xml, monitor); mDescription += String.format("\n%1$d packages found.", mPackages.length);
if (mPackages.length == 0) { }
mDescription += "\nNo packages found.";
} else if (mPackages.length == 1) {
mDescription += "\nOne package found.";
} else {
mDescription += String.format("\n%1$d packages found.", mPackages.length);
} }
// done // done
@@ -202,11 +218,8 @@ public class RepoSource implements IDescription {
* Java URL Connection: http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html * Java URL Connection: http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
* Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html * Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
* Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html * Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html
*
* @throws FileNotFoundException if the URL does not match an existing resource.
*/ */
private ByteArrayInputStream fetchUrl(String urlString, ITaskMonitor monitor) private ByteArrayInputStream fetchUrl(String urlString, ITaskMonitor monitor) {
throws FileNotFoundException {
URL url; URL url;
try { try {
url = new URL(urlString); url = new URL(urlString);
@@ -243,14 +256,10 @@ public class RepoSource implements IDescription {
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// FNF derives from IOException. We want to be able to report that one // The FNF message is just the URL. Make it a bit more useful.
// only to the caller so we need to catch before generic IOException and monitor.setResult("File not found: %1$s", e.getMessage());
// re-throw it.
throw e;
} catch (IOException e) { } catch (IOException e) {
// Generic IOException other than FNF are simply listed in the monitor
// output and ignored. The method will return null since it hasn't fetched
// anything.
monitor.setResult(e.getMessage()); monitor.setResult(e.getMessage());
} }
@@ -261,7 +270,7 @@ public class RepoSource implements IDescription {
* Validates this XML against the SDK Repository schema. * Validates this XML against the SDK Repository schema.
* Returns true if the XML was correctly validated. * Returns true if the XML was correctly validated.
*/ */
private boolean validateXml(ByteArrayInputStream xml, ITaskMonitor monitor) { private boolean validateXml(ByteArrayInputStream xml, String url, ITaskMonitor monitor) {
try { try {
Validator validator = getValidator(); Validator validator = getValidator();
@@ -269,11 +278,10 @@ public class RepoSource implements IDescription {
validator.validate(new StreamSource(xml)); validator.validate(new StreamSource(xml));
return true; return true;
} catch (SAXException e) { } catch (Exception e) {
monitor.setResult(e.getMessage()); monitor.setResult("XML verification failed for %1$s.\nError: %2$s",
url,
} catch (IOException e) { e.getMessage());
monitor.setResult(e.getMessage());
} }
return false; return false;