use of org.exist.dom.persistent.LockToken in project exist by eXist-db.
the class ExistDocument method getCurrentLock.
/**
* Get lock token from database.
*
* @return current lock token.
*/
public LockToken getCurrentLock() {
if (LOG.isDebugEnabled()) {
LOG.debug("Get current lock {}", xmldbUri);
}
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.READ_LOCK)) {
final DocumentImpl document = lockedDocument.getDocument();
if (document == null) {
LOG.debug("No resource found for path: {}", xmldbUri);
return null;
}
// TODO consider. A Webdav lock can be set without subject lock.
Account lock = document.getUserLock();
if (lock == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Document {} does not contain userlock", xmldbUri);
}
return null;
}
// Retrieve Locktoken from document metadata
org.exist.dom.persistent.LockToken token = document.getLockToken();
if (token == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Document meta data does not contain a LockToken");
}
return null;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully retrieved token");
}
return token;
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
return null;
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished probe lock");
}
}
}
use of org.exist.dom.persistent.LockToken in project exist by eXist-db.
the class ExistDocument method refreshLock.
public LockToken refreshLock(String token) throws PermissionDeniedException, DocumentAlreadyLockedException, EXistException, DocumentNotLockedException {
if (LOG.isDebugEnabled()) {
LOG.debug("refresh lock {} lock={}", xmldbUri, token);
}
if (token == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("token is null");
}
throw new EXistException("token is null");
}
// Try to get document
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
final DocumentImpl document = lockedDocument.getDocument();
if (document == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No resource found for path: {}", xmldbUri);
}
// return null; // throw exception?
throw new EXistException("No resource found.");
}
// Get current userlock
Account userLock = document.getUserLock();
// Check if Resource is already locked.
if (userLock == null) {
final String msg = "Resource was not locked.";
if (LOG.isDebugEnabled()) {
LOG.debug(msg);
}
throw new DocumentNotLockedException(msg);
}
if (userLock.getName() != null && !userLock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Resource is locked by {}", userLock.getName());
}
throw new PermissionDeniedException(userLock.getName());
}
LockToken lockToken = document.getLockToken();
if (!token.equals(lockToken.getOpaqueLockToken())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Token does not match");
}
throw new PermissionDeniedException(String.format("Token %s does not match %s", token, lockToken.getOpaqueLockToken()));
}
lockToken.setTimeOut(LockToken.LOCK_TIMEOUT_INFINITE);
// Make token persistant
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final Txn txn = txnManager.beginTransaction()) {
broker.storeXMLResource(txn, document);
txnManager.commit(txn);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully retrieved token");
}
return lockToken;
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished create lock");
}
}
}
Aggregations