lockResource(resource);
try {
// 2. check whether the resource has a cache entry; if no, goto 4
CacheEntry entry = getCacheEntry(resource);
// 3. check whether the expiry time of the cache entry has passed; if no, returns immediately
if(entry != null && entry.getExpiryDate().after(new Date())) {
log.info("not refreshing resource {}, as the cached entry is not yet expired",resource);
return;
}
// 4.
log.debug("refreshing resource {}",resource);
try {
ClientResponse response = clientService.retrieveResource(resource);
if(response != null) {
log.info("refreshed resource {}",resource);
URI context = cacheProvider.getTripleRepository().getValueFactory().createURI(CTX_CACHE);
RepositoryConnection lmfConnection = cacheProvider.getTripleRepository().getConnection();
RepositoryConnection respConnection = response.getTriples().getConnection();
lmfConnection.remove(resource,null,null,context);
RepositoryResult<Statement> triples = respConnection.getStatements(null,null,null,true);
while(triples.hasNext()) {
Statement triple = triples.next();
try {
lmfConnection.add(triple,context);
} catch (RuntimeException ex) {
log.warn("not adding triple {}: an exception occurred ({})",triple,ex.getMessage());
}
}
lmfConnection.commit();
lmfConnection.close();
respConnection.close();
CacheEntry newEntry = new CacheEntry();
newEntry.setResource(resource);
newEntry.setExpiryDate(response.getExpires());
newEntry.setLastRetrieved(new Date());
if(entry != null) {
newEntry.setUpdateCount(entry.getUpdateCount()+1);
} else {
newEntry.setUpdateCount((long)1);
}
cacheProvider.getMetadataRepository().put(resource.stringValue(),newEntry);
}
} catch (LDClientException e) {
// on exception, save an expiry information and retry in one day
CacheEntry newEntry = new CacheEntry();
newEntry.setResource(resource);
newEntry.setExpiryDate(new Date(System.currentTimeMillis() + config.getInt("expiry", 86400)*1000));
newEntry.setLastRetrieved(new Date());
if(entry != null) {
newEntry.setUpdateCount(entry.getUpdateCount()+1);
} else {
newEntry.setUpdateCount((long)1);
}
cacheProvider.getMetadataRepository().put(resource.stringValue(), newEntry);
log.error("refreshing the remote resource {} from the Linked Data Cloud failed ({})",resource,e.getMessage());