1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-04 16:35:08 -05:00

* 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());
}
} 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
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);
}
@ -225,7 +232,7 @@ public class ReleaseInfo {
// 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
public String[] process(ByteBuffer data) {

View File

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