Search in sources :

Example 26 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class ExistDocument method unlock.

/**
 * Unlock document in database.
 */
void unlock() throws PermissionDeniedException, DocumentNotLockedException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("unlock {}", xmldbUri);
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    // Try to get document
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            final String msg = String.format("No resource found for path: %s", xmldbUri);
            LOG.debug(msg);
            throw new EXistException(msg);
        }
        // Get current userlock
        Account lock = document.getUserLock();
        // Check if Resource is already locked.
        if (lock == null) {
            LOG.debug("Resource {} is not locked.", xmldbUri);
            throw new DocumentNotLockedException("" + xmldbUri);
        }
        // Check if Resource is from subject
        if (!lock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            LOG.debug("Resource lock is from user {}", lock.getName());
            throw new PermissionDeniedException(lock.getName());
        }
        // Update document
        document.setUserLock(null);
        document.setLockToken(null);
        // Make it persistant
        broker.storeMetadata(txn, document);
        txnManager.commit(txn);
    } 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");
        }
    }
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) DocumentNotLockedException(org.exist.webdav.exceptions.DocumentNotLockedException) TriggerException(org.exist.collections.triggers.TriggerException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 27 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class ExistDocument method delete.

/**
 * Remove document from database.
 */
void delete() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", xmldbUri);
    }
    // Need to split path into collection and document name
    final XmldbURI collName = xmldbUri.removeLastSegment();
    final XmldbURI docName = xmldbUri.lastSegment();
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(collName, LockMode.WRITE_LOCK)) {
        // Open collection if possible, else abort
        if (collection == null) {
            LOG.debug("Collection does not exist");
            txnManager.abort(txn);
            return;
        }
        // Open document if possible, else abort
        try (final LockedDocument lockedResource = collection.getDocumentWithLock(broker, docName, LockMode.WRITE_LOCK)) {
            if (lockedResource == null) {
                LOG.debug("No resource found for path: {}", xmldbUri);
                txnManager.abort(txn);
                return;
            }
            final DocumentImpl resource = lockedResource.getDocument();
            if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
                collection.removeBinaryResource(txn, broker, resource.getFileURI());
            } else {
                collection.removeXMLResource(txn, broker, resource.getFileURI());
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document deleted sucessfully");
            }
        }
    } catch (final LockException e) {
        LOG.error("Resource is locked.", e);
    } catch (final EXistException | IOException | TriggerException | PermissionDeniedException e) {
        LOG.error(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished delete");
        }
    }
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) 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) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 28 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class ExistCollection method createCollection.

public XmldbURI createCollection(String name) throws PermissionDeniedException, CollectionExistsException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create  '{}' in '{}'", name, xmldbUri);
    }
    XmldbURI newCollection = xmldbUri.append(name);
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(newCollection, LockMode.WRITE_LOCK)) {
        if (collection != null) {
            final String msg = "Collection already exists";
            LOG.debug(msg);
            // XXX: double "abort" is bad thing!!!
            txnManager.abort(txn);
            throw new CollectionExistsException(msg);
        }
        // Create collection
        try (final Collection created = broker.getOrCreateCollection(txn, newCollection)) {
            broker.saveCollection(txn, created);
            broker.flush();
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Collection created sucessfully");
            }
        }
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } catch (Throwable e) {
        LOG.error(e);
        throw new EXistException(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation");
        }
    }
    return newCollection;
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) CollectionExistsException(org.exist.webdav.exceptions.CollectionExistsException)

Example 29 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class ExistCollection method delete.

/*
     * Delete document or collection.
     */
void delete() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting '{}'", xmldbUri);
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
        // Open collection if possible, else abort
        if (collection == null) {
            txnManager.abort(txn);
            return;
        }
        // Remove collection
        broker.removeCollection(txn, collection);
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Document deleted sucessfully");
        }
    } catch (EXistException | IOException | PermissionDeniedException | TriggerException e) {
        LOG.error(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished delete");
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 30 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class RecoveryTest2 method store.

@Test
public void store() throws DatabaseConfigurationException, EXistException, PermissionDeniedException, IOException, SAXException, BTreeException, LockException {
    BrokerPool.FORCE_CORRUPTION = true;
    final BrokerPool pool = startDb();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
        Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
        assertNotNull(test2);
        broker.saveCollection(transaction, test2);
        DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        assertNotNull(domDb);
        try (final Writer writer = new StringWriter()) {
            domDb.dump(writer);
        }
        // store some documents. Will be replaced below
        final Path dir = Paths.get(xmlDir);
        final List<Path> docs = FileUtils.list(dir);
        for (final Path f : docs) {
            broker.storeDocument(transaction, XmldbURI.create(FileUtils.fileName(f)), new InputSource(f.toUri().toASCIIString()), MimeType.XML_TYPE, test2);
        }
        transact.commit(transaction);
    }
}
Also used : Path(java.nio.file.Path) InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DOMFile(org.exist.storage.dom.DOMFile) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

TransactionManager (org.exist.storage.txn.TransactionManager)131 Txn (org.exist.storage.txn.Txn)128 Collection (org.exist.collections.Collection)86 DBBroker (org.exist.storage.DBBroker)70 BrokerPool (org.exist.storage.BrokerPool)61 StringInputSource (org.exist.util.StringInputSource)27 EXistException (org.exist.EXistException)21 XmldbURI (org.exist.xmldb.XmldbURI)20 Sequence (org.exist.xquery.value.Sequence)20 XQuery (org.exist.xquery.XQuery)18 InputSource (org.xml.sax.InputSource)18 DocumentImpl (org.exist.dom.persistent.DocumentImpl)17 Test (org.junit.Test)16 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 InputStream (java.io.InputStream)14 StringReader (java.io.StringReader)13 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)13 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 IOException (java.io.IOException)12 Modification (org.exist.xupdate.Modification)12