Search in sources :

Example 6 with LockMode

use of org.exist.storage.lock.Lock.LockMode 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);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DBBroker(org.exist.storage.DBBroker) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 7 with LockMode

use of org.exist.storage.lock.Lock.LockMode 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);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

LockMode (org.exist.storage.lock.Lock.LockMode)7 Collection (org.exist.collections.Collection)4 XmldbURI (org.exist.xmldb.XmldbURI)4 IOException (java.io.IOException)3 EXistException (org.exist.EXistException)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 Map (java.util.Map)2 TriggerException (org.exist.collections.triggers.TriggerException)2 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 DBBroker (org.exist.storage.DBBroker)2 LockType (org.exist.storage.lock.Lock.LockType)2 LockCountTraces (org.exist.storage.lock.LockTable.LockCountTraces)2 LockModeOwner (org.exist.storage.lock.LockTable.LockModeOwner)2 TransactionManager (org.exist.storage.txn.TransactionManager)2 Txn (org.exist.storage.txn.Txn)2 Try (com.evolvedbinary.j8fu.Try.Try)1 InputStream (java.io.InputStream)1 java.net (java.net)1