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:
parent
673b2cc7b5
commit
828d3dfd1e
@ -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) {
|
||||||
|
@ -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,39 +20,47 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert resource data into usable data
|
* Convert resource data into usable data
|
||||||
*/
|
*/
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user