* persistent cache for subLanguageID map

This commit is contained in:
Reinhard Pointner 2011-12-13 12:34:34 +00:00
parent c1ed273158
commit 506a2938ac
1 changed files with 51 additions and 37 deletions

View File

@ -3,7 +3,6 @@ package net.sourceforge.filebot.web;
import static java.lang.Math.*;
import static java.util.Collections.*;
import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*;
import java.io.File;
@ -18,7 +17,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
@ -28,6 +26,9 @@ import javax.swing.Icon;
import redstone.xmlrpc.XmlRpcException;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.Query;
import net.sourceforge.tuned.Timer;
@ -276,44 +277,57 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
}
};
/**
* SubLanguageID by English language name
*/
private static final Map<String, String> subLanguageCache = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
@SuppressWarnings("unchecked")
protected synchronized Map<String, String> getSubLanguageMap() throws Exception {
synchronized (subLanguageCache) {
if (subLanguageCache.isEmpty()) {
for (Entry<String, String> entry : xmlrpc.getSubLanguages().entrySet()) {
// map id by name
subLanguageCache.put(entry.getValue(), entry.getKey());
}
Cache cache = CacheManager.getInstance().getCache("web-persistent-datasource");
String key = getClass().getName() + ".getSubLanguageMap";
// some additional special handling
subLanguageCache.put("Brazilian", "pob");
Element element = cache.get(key);
Map<String, String> subLanguageMap;
if (element == null) {
subLanguageMap = new HashMap<String, String>();
// fetch language data
for (Entry<String, String> entry : xmlrpc.getSubLanguages().entrySet()) {
// map id by name
subLanguageMap.put(entry.getValue().toLowerCase(), entry.getKey().toLowerCase());
}
return unmodifiableMap(subLanguageCache);
// some additional special handling
subLanguageMap.put("brazilian", "pob");
// cache data
cache.put(new Element(key, subLanguageMap));
} else {
// use cached entry
subLanguageMap = (Map<String, String>) element.getValue();
}
return subLanguageMap;
}
protected String getSubLanguageID(String languageName) throws Exception {
if (!getSubLanguageMap().containsKey(languageName)) {
if (!getSubLanguageMap().containsKey(languageName.toLowerCase())) {
throw new IllegalArgumentException(String.format("SubLanguageID for '%s' not found", languageName));
}
return getSubLanguageMap().get(languageName);
return getSubLanguageMap().get(languageName.toLowerCase());
}
protected String getLanguageName(String subLanguageID) throws Exception {
for (Entry<String, String> it : getSubLanguageMap().entrySet()) {
if (it.getValue().equals(subLanguageID))
if (it.getValue().equals(subLanguageID.toLowerCase()))
return it.getKey();
}
return null;
}
}