Search in sources :

Example 6 with TransactionException

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

the class NativeBroker method getBinaryResource.

@Override
public InputStream getBinaryResource(final BinaryDocument blob) throws IOException {
    // TODO(AR) how best to get the transaction?
    try (final Txn transaction = continueOrBeginTransaction()) {
        final InputStream is = getBinaryResource(transaction, blob);
        transaction.commit();
        return is;
    } catch (final TransactionException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : TransactionException(org.exist.storage.txn.TransactionException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Txn(org.exist.storage.txn.Txn)

Example 7 with TransactionException

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

the class MetadataFunctions method extractMetadataFromLocalResource.

private Sequence extractMetadataFromLocalResource(final XmldbURI docUri) throws XPathException {
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docUri, LockMode.READ_LOCK)) {
        if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument) {
            final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
            final BrokerPool pool = context.getBroker().getBrokerPool();
            final BlobStore blobStore = pool.getBlobStore();
            try (final Txn transaction = pool.getTransactionManager().beginTransaction()) {
                final Sequence result = blobStore.with(transaction, binDoc.getBlobId(), blobFile -> TaggedTryUnchecked(XPathException.class, () -> exifToolExtract(blobFile))).get();
                transaction.commit();
                return result;
            }
        } else {
            throw new XPathException(this, "The binary document at " + docUri.toString() + " cannot be found.");
        }
    } catch (PermissionDeniedException | IOException | TransactionException e) {
        throw new XPathException(this, "Could not access binary document: " + e.getMessage(), e);
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) FunctionSignature(org.exist.xquery.FunctionSignature) PermissionDeniedException(org.exist.security.PermissionDeniedException) Cardinality(org.exist.xquery.Cardinality) BlobStore(org.exist.storage.blob.BlobStore) BasicFunction(org.exist.xquery.BasicFunction) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Source(org.exist.source.Source) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) XmldbURI(org.exist.xmldb.XmldbURI) URI(java.net.URI) Path(java.nio.file.Path) XQueryContext(org.exist.xquery.XQueryContext) OutputStream(java.io.OutputStream) InputSource(org.xml.sax.InputSource) LockedDocument(org.exist.dom.persistent.LockedDocument) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException) Logger(org.apache.logging.log4j.Logger) TaggedTryUnchecked(com.evolvedbinary.j8fu.Try.TaggedTryUnchecked) ModuleUtils(org.exist.xquery.modules.ModuleUtils) SAXException(org.xml.sax.SAXException) SourceFactory(org.exist.source.SourceFactory) Sequence(org.exist.xquery.value.Sequence) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) TransactionException(org.exist.storage.txn.TransactionException) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) BrokerPool(org.exist.storage.BrokerPool) BlobStore(org.exist.storage.blob.BlobStore)

Example 8 with TransactionException

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

the class Deploy method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole())
        throw new XPathException(this, EXPathErrorCode.EXPDY003, "Permission denied. You need to be a member " + "of the dba group to use repo:deploy/undeploy");
    final String pkgName = args[0].getStringValue();
    try {
        Deployment deployment = new Deployment();
        final Optional<String> target;
        if (isCalledAs("deploy")) {
            String userTarget = null;
            if (getArgumentCount() == 2) {
                userTarget = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = deployment.deploy(context.getBroker(), transaction, pkgName, context.getRepository(), userTarget);
                transaction.commit();
            }
        } else if (isCalledAs("install-and-deploy")) {
            String version = null;
            final String repoURI;
            if (getArgumentCount() == 3) {
                version = args[1].getStringValue();
                repoURI = args[2].getStringValue();
            } else {
                repoURI = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = installAndDeploy(transaction, pkgName, version, repoURI);
                transaction.commit();
            }
        } else if (isCalledAs("install-and-deploy-from-db")) {
            String repoURI = null;
            if (getArgumentCount() == 2) {
                repoURI = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = installAndDeployFromDb(transaction, pkgName, repoURI);
                transaction.commit();
            }
        } else {
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = deployment.undeploy(context.getBroker(), transaction, pkgName, context.getRepository());
                transaction.commit();
            }
        }
        target.orElseThrow(() -> new XPathException("expath repository is not available."));
        return statusReport(target);
    } catch (PackageException e) {
        throw new XPathException(this, EXPathErrorCode.EXPDY001, e.getMessage(), args[0], e);
    } catch (IOException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Caught IO error while deploying expath archive", args[0], e);
    } catch (TransactionException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Caught transaction error while deploying expath archive", args[0], e);
    }
}
Also used : TransactionException(org.exist.storage.txn.TransactionException) Deployment(org.exist.repo.Deployment) PackageException(org.expath.pkg.repo.PackageException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException)

Example 9 with TransactionException

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

the class EXistURIResolver method databaseSource.

private Source databaseSource(final String path) throws TransformerException {
    final XmldbURI uri = XmldbURI.create(path);
    final DBBroker broker = db.getActiveBroker();
    final DocumentImpl doc;
    try {
        doc = broker.getResource(uri, Permission.READ);
        if (doc == null) {
            LOG.error("Document {} not found", path);
            throw new TransformerException("Resource " + path + " not found in database.");
        }
        final Source source;
        if (doc instanceof BinaryDocument) {
            /*
         * NOTE: this is extremely unpleasant as we let a reference to the blob file
         * escape from the closure into the StreamSource. This means that the file could have been deleted
         * by time the user comes to access the StreamSource, however this was also
         * the case with eXist-db's previous design, and due to the lack of resource
         * management of the StreamSource class, there is little we can do to improve
         * the situation - AR.
         */
            try (final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
                source = broker.withBinaryFile(transaction, (BinaryDocument) doc, p -> {
                    final StreamSource source1 = new StreamSource(p.toFile());
                    source1.setSystemId(p.toUri().toString());
                    return source1;
                });
                transaction.commit();
                return source;
            }
        } else {
            source = new EXistDbSource(broker, doc);
            source.setSystemId(uri.toASCIIString());
            return source;
        }
    } catch (final PermissionDeniedException | TransactionException | IOException e) {
        throw new TransformerException(e.getMessage(), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Txn(org.exist.storage.txn.Txn) Array(java.lang.reflect.Array) BrokerPool(org.exist.storage.BrokerPool) TransformerException(javax.xml.transform.TransformerException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) StreamSource(javax.xml.transform.stream.StreamSource) URIResolver(javax.xml.transform.URIResolver) IOException(java.io.IOException) Source(javax.xml.transform.Source) PermissionDeniedException(org.exist.security.PermissionDeniedException) TransactionException(org.exist.storage.txn.TransactionException) Logger(org.apache.logging.log4j.Logger) DBBroker(org.exist.storage.DBBroker) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) URI(java.net.URI) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Permission(org.exist.security.Permission) StreamSource(javax.xml.transform.stream.StreamSource) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) TransactionException(org.exist.storage.txn.TransactionException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) TransformerException(javax.xml.transform.TransformerException)

Example 10 with TransactionException

use of org.exist.storage.txn.TransactionException 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());
        }
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) TransactionException(org.exist.storage.txn.TransactionException) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

TransactionException (org.exist.storage.txn.TransactionException)13 Txn (org.exist.storage.txn.Txn)13 IOException (java.io.IOException)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 XmldbURI (org.exist.xmldb.XmldbURI)5 URISyntaxException (java.net.URISyntaxException)4 BinaryDocument (org.exist.dom.persistent.BinaryDocument)4 LockedDocument (org.exist.dom.persistent.LockedDocument)4 XPathException (org.exist.xquery.XPathException)4 URI (java.net.URI)3 DocumentImpl (org.exist.dom.persistent.DocumentImpl)3 InputStream (java.io.InputStream)2 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Collection (org.exist.collections.Collection)2 Permission (org.exist.security.Permission)2 BrokerPool (org.exist.storage.BrokerPool)2 SAXException (org.xml.sax.SAXException)2 TaggedTryUnchecked (com.evolvedbinary.j8fu.Try.TaggedTryUnchecked)1