LOG.debug("create lock " + xmldbUri);
}
DBBroker broker = null;
DocumentImpl document = null;
TransactionManager txnManager = null;
Txn txn = null;
try {
broker = brokerPool.get(subject);
// Try to get document (add catch?)
document = broker.getXMLResource(xmldbUri, Lock.WRITE_LOCK);
if (document == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("No resource found for path: %s", xmldbUri));
}
//return null; // throw exception?
throw new EXistException("No resource found.");
}
// Get current userlock
Account userLock = document.getUserLock();
// Check if Resource is already locked. @@ToDo
if (userLock != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Resource was already locked, ignored.");
}
}
if ( userLock != null && userLock.getName() != null
&& !userLock.getName().equals(subject.getName())
&& !subject.hasDbaRole() ) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Resource is locked by user %s.", userLock.getName()));
}
throw new PermissionDeniedException(userLock.getName());
}
// Check for request for shared lock. @@TODO
if (inputToken.getScope() == LockToken.LOCK_SCOPE_SHARED) {
if (LOG.isDebugEnabled()) {
LOG.debug("Shared locks are not implemented.");
}
throw new EXistException("Shared locks are not implemented.");
}
// Update locktoken
inputToken.setOwner(subject.getName());
inputToken.createOpaqueLockToken();
//inputToken.setTimeOut(inputToken.getTimeOut());
inputToken.setTimeOut(LockToken.LOCK_TIMEOUT_INFINITE);
// Update document
document.getMetadata().setLockToken(inputToken);
document.setUserLock(subject);
// Make token persistant
txnManager = brokerPool.getTransactionManager();
txn = txnManager.beginTransaction();
broker.storeMetadata(txn, document);
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully retrieved token");
}
return inputToken;
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
if (txnManager != null) {
txnManager.abort(txn);
}
throw e;
} catch (TriggerException e) {
LOG.error(e);
//dead code, remove?
if (txnManager != null) {
txnManager.abort(txn);
}
//-----------------------
throw new EXistException(e);
} finally {
// TODO: check if can be done earlier
if (document != null) {
document.getUpdateLock().release(Lock.WRITE_LOCK);
}
if (txnManager != null) {
txnManager.close(txn);
}
brokerPool.release(broker);
if (LOG.isDebugEnabled()) {