*/
@Override
public void refreshResource(URI resource, boolean forceRefresh) {
resourceLocks.lock(resource.stringValue());
try {
LDCachingConnection cacheConnection = backend.getCacheConnection(resource.stringValue());
CacheEntry entry = null;
try {
cacheConnection.begin();
// 2. check whether the resource has a cache entry; if no, goto 4
entry = cacheConnection.getCacheEntry(resource);
// commit/close the connection, the retrieveResource method takes too long to hold the DB connection open
cacheConnection.commit();
// 3. check whether the expiry time of the cache entry has passed; if no, returns immediately
if(!forceRefresh && entry != null && entry.getExpiryDate().after(new Date())) {
log.debug("not refreshing resource {}, as the cached entry is not yet expired",resource);
return;
}
} catch(RepositoryException ex) {
cacheConnection.rollback();
} finally {
cacheConnection.close();
}
// 4.
log.debug("refreshing resource {}",resource);
this.lock.readLock().lock();
try {
ClientResponse response = ldclient.retrieveResource(resource.stringValue());
if(response != null) {
log.info("refreshed resource {}",resource);
// obtain a new cache connection, since we closed the original connection above
LDCachingConnection cacheConnection1 = backend.getCacheConnection(resource.stringValue());
cacheConnection1.begin();
try {
URI subject = cacheConnection1.getValueFactory().createURI(resource.stringValue());
RepositoryConnection respConnection = response.getTriples().getConnection();
cacheConnection1.remove(subject, null, null);
int count = 0;
RepositoryResult<Statement> triples = respConnection.getStatements(null,null,null,true);
while(triples.hasNext()) {
Statement triple = triples.next();
try {
cacheConnection1.add(triple);
} catch (RuntimeException ex) {
log.warn("not adding triple {}: an exception occurred ({})",triple,ex.getMessage());
}
count++;
}
triples.close();
respConnection.close();
CacheEntry newEntry = new CacheEntry();
newEntry.setResource(subject);
newEntry.setExpiryDate(response.getExpires());
newEntry.setLastRetrieved(new Date());
if(entry != null) {
newEntry.setUpdateCount(entry.getUpdateCount()+1);
} else {
newEntry.setUpdateCount(1);
}
newEntry.setTripleCount(count);
cacheConnection1.removeCacheEntry(resource);
cacheConnection1.addCacheEntry(resource, newEntry);
cacheConnection1.commit();
} catch (RepositoryException e) {
log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, e);
cacheConnection1.rollback();
} finally {
cacheConnection1.close();
}
}
} catch (DataRetrievalException e) {
// on exception, save an expiry information and retry in one day
CacheEntry newEntry = new CacheEntry();
newEntry.setResource(cacheConnection.getValueFactory().createURI(resource.stringValue()));
newEntry.setExpiryDate(new Date(System.currentTimeMillis() + config.getDefaultExpiry()*1000));
newEntry.setLastRetrieved(new Date());
if(entry != null) {
newEntry.setUpdateCount(entry.getUpdateCount()+1);
} else {
newEntry.setUpdateCount(1);
}
newEntry.setTripleCount(0);
LDCachingConnection cacheConnection2 = backend.getCacheConnection(resource.stringValue());
cacheConnection2.begin();
try {
cacheConnection2.removeCacheEntry(resource);
cacheConnection2.addCacheEntry(resource, newEntry);
cacheConnection2.commit();
log.error("refreshing the remote resource {} from the Linked Data Cloud failed ({})",resource,e.getMessage());
//log.info("exception was:",e);
return;
} catch (RepositoryException ex) {
log.error("repository error while refreshing the remote resource {} from the Linked Data Cloud", resource, ex);
cacheConnection2.rollback();
} finally {
cacheConnection2.close();
}
} finally {
this.lock.readLock().unlock();
}
} catch (RepositoryException e) {