* enforce type-checking just in case

This commit is contained in:
Reinhard Pointner 2011-12-15 16:16:33 +00:00
parent 673b2cc7b5
commit 828d3dfd1e
2 changed files with 34 additions and 17 deletions

View File

@ -48,12 +48,19 @@ public class ReleaseInfo {
names.put(it.getName().toLowerCase(), it.getName()); names.put(it.getName().toLowerCase(), it.getName());
} }
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(ReleaseInfo.class.getClass().getName()).log(Level.WARNING, "Failed to lookup info by id: " + e.getMessage()); Logger.getLogger(ReleaseInfo.class.getClass().getName()).log(Level.WARNING, "Failed to lookup info by id: " + e.getMessage(), e);
} }
// match common word sequence and clean detected word sequence from unwanted elements // match common word sequence and clean detected word sequence from unwanted elements
Collection<String> matches = new SeriesNameMatcher().matchAll(files.toArray(new File[files.size()])); Collection<String> matches = new SeriesNameMatcher().matchAll(files.toArray(new File[files.size()]));
for (String it : releaseInfo.cleanRG(matches)) {
try {
matches = releaseInfo.cleanRG(matches);
} catch (Exception e) {
Logger.getLogger(ReleaseInfo.class.getClass().getName()).log(Level.WARNING, "Failed to clean matches: " + e.getMessage(), e);
}
for (String it : matches) {
names.put(it.toLowerCase(), it); names.put(it.toLowerCase(), it);
} }
@ -225,7 +232,7 @@ public class ReleaseInfo {
// fetch release group names online and try to update the data every other day // fetch release group names online and try to update the data every other day
protected final CachedResource<String[]> releaseGroupResource = new CachedResource<String[]>(getBundle(getClass().getName()).getString("url.release-groups"), DAYS.toMillis(2)) { protected final CachedResource<String[]> releaseGroupResource = new CachedResource<String[]>(getBundle(getClass().getName()).getString("url.release-groups"), String[].class, DAYS.toMillis(2)) {
@Override @Override
public String[] process(ByteBuffer data) { public String[] process(ByteBuffer data) {

View File

@ -8,6 +8,8 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.ehcache.Cache; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
@ -18,12 +20,14 @@ public abstract class CachedResource<T extends Serializable> {
private Cache cache; private Cache cache;
private String resource; private String resource;
private Class<T> type;
private long expirationTime; private long expirationTime;
public CachedResource(String resource, long expirationTime) { public CachedResource(String resource, Class<T> type, long expirationTime) {
this.cache = CacheManager.getInstance().getCache("web-persistent-datasource"); this.cache = CacheManager.getInstance().getCache("web-persistent-datasource");
this.resource = resource; this.resource = resource;
this.type = type;
this.expirationTime = expirationTime; this.expirationTime = expirationTime;
} }
@ -34,23 +38,29 @@ public abstract class CachedResource<T extends Serializable> {
public abstract T process(ByteBuffer data); public abstract T process(ByteBuffer data);
@SuppressWarnings("unchecked")
public synchronized T get() throws IOException { public synchronized T get() throws IOException {
Element element = cache.get(resource); Element element = cache.get(resource);
long lastUpdateTime = (element != null) ? element.getLatestOfCreationAndUpdateTime() : 0; long lastUpdateTime = (element != null) ? element.getLatestOfCreationAndUpdateTime() : 0;
if (element == null || System.currentTimeMillis() - lastUpdateTime > expirationTime) { // fetch from cache
ByteBuffer data = fetchIfModified(new URL(resource), element != null ? lastUpdateTime : 0); if (element != null && System.currentTimeMillis() - lastUpdateTime < expirationTime) {
try {
if (data != null) { return type.cast(element.getValue());
element = new Element(resource, process(data)); } catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage(), e);
} }
// update cached data and last-updated time
cache.put(element);
} }
return (T) element.getValue(); // fetch and process resource
ByteBuffer data = fetchIfModified(new URL(resource), element != null ? lastUpdateTime : 0);
if (data != null) {
element = new Element(resource, process(data));
}
// update cached data and last-updated time
cache.put(element);
return type.cast(element.getValue());
} }
} }