OsCachingNameResolver.java from Tea Stats at Krugle
Show OsCachingNameResolver.java syntax highlighted
package net.time4tea.webstats.enhancer.dns;
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import java.io.IOException;
/**
* Originally richja 04-May-2007
*/
public class OsCachingNameResolver implements NameResolver {
private Cache cache;
private NameResolver resolver;
private GeneralCacheAdministrator admin;
private int myRefreshPeriod;
public OsCachingNameResolver(Cache cache, int cacheTtlDays, NameResolver resolver) {
this.cache = cache;
this.resolver = resolver;
myRefreshPeriod = cacheTtlDays * 86400;
}
public String reverseLookup(String hostIP) throws IOException {
boolean updated = false;
String hostname;
try {
// Get from the cache
hostname = (String) cache.getFromCache(hostIP, myRefreshPeriod);
}
catch (NeedsRefreshException nre) {
try {
try {
hostname = resolver.reverseLookup(hostIP);
}
catch (IOException e) {
hostname = "Unknown";
}
cache.putInCache(hostIP, hostname);
updated = true;
}
finally {
if (!updated) {
// It is essential that cancelUpdate is called if the
// cached content could not be rebuilt
cache.cancelUpdate(hostIP);
}
}
}
return hostname;
}
}
See more files for this project here