if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Refreshing " + this.uri);
}
Source source = null;
Cache cache = null;
try {
cache = (Cache) this.manager.lookup(this.cacheRole);
source = this.resolver.resolveURI(this.uri);
// check if the source is really expired and invalid
CachedSourceResponse response = (CachedSourceResponse) cache.get(this.cacheKey);
if (response != null) {
final SourceValidity sourceValidity = response.getValidityObjects()[1];
if (CachingSource.isValid(sourceValidity, source)) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Cached response is still valid " +
"for source " + this.uri + ".");
}
response.getValidityObjects()[0] = new ExpiresValidity(this.expires * 1000);
return;
}
}
if (source.exists()) {
// what is in the cached response?
byte[] binary = null;
byte[] xml = null;
if (response != null) {
binary = response.getBinaryResponse();
xml = response.getXMLResponse();
}
// create a new cached response
final ExpiresValidity cacheValidity = new ExpiresValidity(this.expires * 1000);
final SourceValidity sourceValidity = source.getValidity();
response = new CachedSourceResponse(new SourceValidity[] {cacheValidity, sourceValidity});
// only create objects that have previously been used
if (binary != null) {
binary = CachingSource.readBinaryResponse(source);
response.setBinaryResponse(binary);
}
if (xml != null) {
xml = CachingSource.readXMLResponse(source, binary, this.manager);
response.setXMLResponse(xml);
}
// meta info is always set
response.setExtra(CachingSource.readMeta(source));
cache.store(this.cacheKey, response);
}
else if (response != null) {
// FIXME: There is a potential problem when the parent
// source has not yet been updated thus listing this
// source still as one of its children. We'll have to remove
// the parent's cached response here too.
if (getLogger().isDebugEnabled()) {
getLogger().debug("Source " + this.uri + " no longer exists." +
" Throwing out cached response.");
}
cache.remove(this.cacheKey);
}
} catch (Exception e) {
if (!failSafe) {
// the content expires, so remove it
cache.remove(cacheKey);
getLogger().warn("Exception during updating of source " + this.uri, e);
}
else {
getLogger().warn("Updating of source " + this.uri + " failed. " +
"Cached response (if any) will be stale.", e);