diff options
author | Thomas Meyer <[email protected]> | 2012-04-10 19:10:43 +0200 |
---|---|---|
committer | Thomas Meyer <[email protected]> | 2012-04-10 19:10:43 +0200 |
commit | b942de5bc239dded7694330f6f7c9d25e4352b03 (patch) | |
tree | dd578a6894b9534b4ad283b9e0381cb5206eaa94 /netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java | |
parent | 055577a36255b0ae4dc130793c12f5207c11bd2c (diff) |
Validate the recently_used file at every load. This will fix the StringIndex-
OutOfBoundsException for a corrupted path entry thrown in
CacheUtil.pathToURLPath(). This Exception was catched in
RessourceTracker.Downloader.run() and only printed in debug mode.
Diffstat (limited to 'netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java')
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java b/netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java index e148bd3..a1eef67 100644 --- a/netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java +++ b/netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java @@ -36,6 +36,7 @@ exception statement from your version. */ package net.sourceforge.jnlp.cache; +import java.util.Set; import static net.sourceforge.jnlp.runtime.Translator.R; import java.io.File; @@ -45,6 +46,7 @@ import java.nio.channels.OverlappingFileLockException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import java.util.Map.Entry; @@ -107,6 +109,57 @@ enum CacheLRUWrapper { */ public synchronized void load() { cacheOrder.load(); + /* + * clean up possibly corrupted entries + */ + if (checkData()) { + if (JNLPRuntime.isDebug()) { + new LruCacheException().printStackTrace(); + } + System.out.println(R("CFakeCache")); + store(); + System.out.println(R("CFakedCache")); + } + } + + /** + * check content of cacheOrder and remove invalid/corrupt entries + * + * @return true, if cache was coruupted and affected entry removed + */ + private boolean checkData () { + boolean modified = false; + Set<Entry<Object, Object>> q = cacheOrder.entrySet(); + for (Iterator<Entry<Object, Object>> it = q.iterator(); it.hasNext();) { + Entry<Object, Object> currentEntry = it.next(); + + final String key = (String) currentEntry.getKey(); + final String path = (String) currentEntry.getValue(); + + // 1. check key format: "milliseconds,number" + try { + String sa[] = key.split(","); + Long l1 = Long.parseLong(sa[0]); + Long l2 = Long.parseLong(sa[1]); + } catch (Exception ex) { + it.remove(); + modified = true; + continue; + } + + // 2. check path format - does the path look correct? + if (path != null) { + if (path.indexOf(cacheDir) < 0) { + it.remove(); + modified = true; + } + } else { + it.remove(); + modified = true; + } + } + + return modified; } /** @@ -174,15 +227,11 @@ enum CacheLRUWrapper { Collections.sort(entries, new Comparator<Entry<String, String>>() { @Override public int compare(Entry<String, String> e1, Entry<String, String> e2) { - try { - Long t1 = Long.parseLong(e1.getKey().split(",")[0]); - Long t2 = Long.parseLong(e2.getKey().split(",")[0]); - - int c = t1.compareTo(t2); - return c < 0 ? 1 : (c > 0 ? -1 : 0); - } catch (Exception e) { - throw new LruCacheException(R("Corrupt LRU file entries")); - } + Long t1 = Long.parseLong(e1.getKey().split(",")[0]); + Long t2 = Long.parseLong(e2.getKey().split(",")[0]); + + int c = t1.compareTo(t2); + return c < 0 ? 1 : (c > 0 ? -1 : 0); } }); return entries; |