use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class AbstractRealm method initialiseRealmStorage.
private void initialiseRealmStorage(final DBBroker broker, final Txn transaction) throws EXistException {
final XmldbURI realmCollectionURL = SecurityManager.SECURITY_COLLECTION_URI.append(getId());
try {
collectionRealm = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL);
collectionAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts"));
collectionGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups"));
collectionRemovedAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts").append("removed"));
collectionRemovedGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups").append("removed"));
} catch (final PermissionDeniedException | IOException | TriggerException | LockException e) {
throw new EXistException(e);
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class ExistDocument method lock.
/**
* Lock document.
*
* @param inputToken Lock token.
* @return Input lock token.
* @throws PermissionDeniedException Permission denied
* @throws DocumentAlreadyLockedException Document is already locked
* @throws EXistException Generic existdb exception
*/
public LockToken lock(LockToken inputToken) throws PermissionDeniedException, DocumentAlreadyLockedException, EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("create lock {}", xmldbUri);
}
// 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. @@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("Resource is locked by user {}.", userLock.getName());
}
throw new PermissionDeniedException(userLock.getName());
}
// Check for request for shared lock. @@TODO
if (inputToken.getScope() == LockToken.LockScope.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.setLockToken(inputToken);
document.setUserLock(subject);
// Make token persistant
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final Txn 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);
throw e;
} catch (TriggerException e) {
LOG.error(e);
throw new EXistException(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished create lock");
}
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class ExistDocument method resourceCopyMove.
/**
* Copy document or collection in database.
*/
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("{} {} to {} named {}", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
}
XmldbURI newNameUri = null;
try {
newNameUri = XmldbURI.xmldbUriFor(newName);
} catch (URISyntaxException ex) {
LOG.error(ex);
throw new EXistException(ex.getMessage());
}
// use WRITE_LOCK if moving or if src and dest collection are the same
final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(xmldbUri.removeLastSegment()) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
DocumentImpl srcDocument = null;
// Need to split path into collection and document name
final XmldbURI srcCollectionUri = xmldbUri.removeLastSegment();
final XmldbURI srdDocumentUri = xmldbUri.lastSegment();
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
// Open collection if possible, else abort
if (srcCollection == null) {
txnManager.abort(txn);
// TODO throw
return;
}
// Open document if possible, else abort
srcDocument = srcCollection.getDocument(broker, srdDocumentUri);
if (srcDocument == null) {
LOG.debug("No resource found for path: {}", xmldbUri);
txnManager.abort(txn);
return;
}
// Open collection if possible, else abort
try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
if (destCollection == null) {
LOG.debug("Destination collection {} does not exist.", xmldbUri);
txnManager.abort(txn);
return;
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyResource(txn, srcDocument, destCollection, newNameUri);
} else {
broker.moveResource(txn, srcDocument, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document {}d successfully", mode);
}
}
} catch (LockException e) {
LOG.error("Resource is locked.", e);
throw new EXistException(e.getMessage());
} catch (EXistException e) {
LOG.error(e);
throw e;
} catch (IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
throw new EXistException(e.getMessage());
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished {}", mode);
}
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class ExistCollection method resourceCopyMove.
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("{} '{}' to '{}' named '{}'", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
}
XmldbURI newNameUri = null;
try {
newNameUri = XmldbURI.xmldbUriFor(newName);
} catch (URISyntaxException ex) {
LOG.error(ex);
throw new EXistException(ex.getMessage());
}
// This class contains already the URI of the resource that shall be moved/copied
XmldbURI srcCollectionUri = xmldbUri;
// use WRITE_LOCK if moving or if src and dest collection are the same
final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(srcCollectionUri) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
// Open collection if possible, else abort
if (srcCollection == null) {
txnManager.abort(txn);
// TODO throw
return;
}
// Open collection if possible, else abort
try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
if (destCollection == null) {
LOG.debug("Destination collection {} does not exist.", xmldbUri);
txnManager.abort(txn);
// TODO throw?
return;
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyCollection(txn, srcCollection, destCollection, newNameUri);
} else {
broker.moveCollection(txn, srcCollection, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Collection {}d successfully", mode);
}
}
} catch (LockException e) {
LOG.error("Resource is locked.", e);
throw new EXistException(e.getMessage());
} catch (EXistException e) {
LOG.error(e);
throw e;
} catch (IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
throw new EXistException(e.getMessage());
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished {}", mode);
}
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class RestoreHandler method restoreDeletedEntry.
private void restoreDeletedEntry(final Attributes atts) {
final String name = atts.getValue("name");
final String type = atts.getValue("type");
if ("collection".equals(type)) {
try {
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK)) {
if (collection != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
broker.removeCollection(transaction, collection);
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
}
} catch (final PermissionDeniedException | IOException | TriggerException | TransactionException e) {
listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
}
} else if ("resource".equals(type)) {
final XmldbURI docName = XmldbURI.create(name);
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK);
final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, docName, Lock.LockMode.WRITE_LOCK)) {
// Check that the document exists
if (lockedDocument != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
final boolean xmlType = !(lockedDocument.getDocument() instanceof BinaryDocument);
if (xmlType) {
collection.removeXMLResource(transaction, broker, docName);
} else {
collection.removeBinaryResource(transaction, broker, docName);
}
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
} catch (final PermissionDeniedException | TransactionException | TriggerException | LockException | IOException e) {
listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
}
}
}
Aggregations