public InstanceResource find(String id)
throws ManageException, DoesNotExistException {
if (id == null) {
throw new ManageException("id may not be null");
}
final InstanceResource resource;
final Lock lock = this.lockManager.getLock(id);
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
throw new ManageException(e.getMessage(), e);
}
try {
final Element el = this.cache.get(id);
if (el == null) {
resource = this.newInstance(id);
resource.load(id); // throws DoesNotExistException if not in db
final Calendar currTime = Calendar.getInstance();
final Calendar termTime = resource.getTerminationTime();
if (termTime != null && termTime.before(currTime)) {
boolean destroyed = this.destroy(id);
if (destroyed) {
throw new DoesNotExistException(Lager.id(id) + " expired");
}
}
} else {
resource = (InstanceResource) el.getObjectValue();
}
} catch (DoesNotExistException e) {
this.cache.remove(id);
throw e;
} catch (CreationException e) {
throw new ManageException(e.getMessage(), e); // ...
} finally {
lock.unlock();
}
return resource;