Permalink
Browse files

tentative support for using cached database.xml when source is offline

Rearranged the code a bit so that the local database.xml file (if any) is not nuked when trying to download a new one.

Changes look promising, but not ready to merge yet for a few reasons:

- Since Quaddicted is offline, I can't easily test the "download success" case to make sure that it still works. There are ways around that which I can do if necessary (i.e. host a database.xml file on some other webserver).

- I don't have a complete current database.xml file to test as the local/cached version. I've been using a truncated database.xml that I manually repaired.

- Currently the code will just silently revert to using the cached copy if the download fails. It would be nice to pop a warning to tell the user that this is going on. Ideally someone else can help me with the best way to do that. :-)
  • Loading branch information...
1 parent 5897b7e commit c2fcb0765e86f74e4d2563b62ad6ec345099be3b @neogeographica committed Jul 30, 2015
Showing with 61 additions and 27 deletions.
  1. +61 −27 src/de/haukerehfeld/quakeinjector/QuakeInjector.java
@@ -341,54 +341,88 @@ private InputStream downloadDatabase(String databaseUrl) throws IOException {
final SwingWorker<List<Requirement>, Void> dbParse
= new SwingWorker<List<Requirement>,Void>() {
+ /** the stream for the database download **/
+ private BufferedInputStream downloadStream;
/** we need to try to download the db to a tmp file first so the old one doesn't get overwritten */
private File tmpFile;
- /** the cached database file */
- private File cache;
/** the stream to the temporary file */
- private FileOutputStream cacheStream;
+ private BufferedOutputStream tmpWriteStream;
+ /** the cached database file **/
+ private File cache;
+ /** stream from the cached database file, if needed **/
+ private BufferedInputStream cacheReadStream;
+ /** whether the temporary file was populated with a good DB **/
+ private boolean updateCache = false;
+ private BufferedInputStream cachedDatabaseStream() throws IOException {
+ if (cache != null && cache.exists() && cache.canRead()) {
+ try {
+ return new BufferedInputStream(new FileInputStream(cache));
+ }
+ catch (IOException e) {}
+ }
+ throw new IOException("cannot download package database or read local cache");
+ }
+
@Override
public List<Requirement> doInBackground() throws IOException, org.xml.sax.SAXException {
cache = getConfig().LocalDatabaseFile.get();
cache = cache.getAbsoluteFile();
InputStream db;
try {
//download database and dump to file
- tmpFile = File.createTempFile(cache.getName(), ".xml", cache.getParentFile());
- cacheStream = new FileOutputStream(tmpFile);
- OutputStream out = new BufferedOutputStream(cacheStream);
- db = new DumpInputStream(new BufferedInputStream(downloadDatabase(databaseUrl)), out);
+ downloadStream = new BufferedInputStream(downloadDatabase(databaseUrl));
+ tmpFile = File.createTempFile(cache.getName(), null, cache.getParentFile());
+ tmpWriteStream = new BufferedOutputStream(new FileOutputStream(tmpFile));
+ db = new DumpInputStream(downloadStream, tmpWriteStream);
+ List<Requirement> parseResult = parseDatabase(db);
+ updateCache = true;
+ return parseResult;
}
+ // if using java 7 we could more nicely do:
+ // catch (IOException | org.xml.sax.SAXException | javax.xml.ws.http.HTTPException e) {
catch (IOException e) {
- //try reading the cached version if downloading fails
- System.err.println("Downloading the database failed. "+databaseUrl);
- if (cache.exists() && cache.canRead()) {
- System.err.println("Using cached database file (" + cache + ") instead.");
- db = new BufferedInputStream(new FileInputStream(cache));
- }
- else {
- System.err.println("using cached database file instead.");
- throw e;
- }
+ cacheReadStream = cachedDatabaseStream();
+ return parseDatabase(cacheReadStream);
+ }
+ catch (org.xml.sax.SAXException e) {
+ cacheReadStream = cachedDatabaseStream();
+ return parseDatabase(cacheReadStream);
+ }
+ catch (javax.xml.ws.http.HTTPException e) {
+ cacheReadStream = cachedDatabaseStream();
+ return parseDatabase(cacheReadStream);
}
- return parseDatabase(db);
}
@Override
public void done() {
try {
- cacheStream.close();
- }
- catch (IOException e) {
- System.out.println("Couldn't close tmp cache outputstream!" + e);
+ if (cacheReadStream != null) {
+ cacheReadStream.close();
+ }
+ if (tmpWriteStream != null) {
+ tmpWriteStream.close();
+ }
+ if (downloadStream != null) {
+ downloadStream.close();
+ }
}
-
- if (cache.exists() && !cache.delete()) {
- System.err.println("Couldn't delete the real cache file!");
+ catch (IOException e) {}
+ if (updateCache == true) {
+ if (cache.exists()) {
+ if (cache.delete() == false) {
+ System.err.println("Couldn't delete the real cache file!");
+ }
+ }
+ if (tmpFile.renameTo(cache) == false) {
+ System.err.println("Couldn't move the temporary cache file to the real cache file!");
+ }
}
- if (tmpFile.renameTo(cache) != true) {
- System.err.println("Couldn't move the temporary cache file to the real cache file!");
+ else {
+ if (tmpFile != null && tmpFile.exists()) {
+ tmpFile.delete();
+ }
}
}
};

0 comments on commit c2fcb07

Please sign in to comment.