Search in sources :

Example 71 with LockedDocument

use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.

the class UserXQueryJob method execute.

@Override
public final void execute(final JobExecutionContext jec) throws JobExecutionException {
    final JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap();
    // TODO why are these values not used from the class members?
    final String xqueryResource = (String) jobDataMap.get(XQUERY_SOURCE);
    final Subject user = (Subject) jobDataMap.get(ACCOUNT);
    final BrokerPool pool = (BrokerPool) jobDataMap.get(DATABASE);
    final Properties params = (Properties) jobDataMap.get(PARAMS);
    final boolean unschedule = ((Boolean) jobDataMap.get(UNSCHEDULE));
    // if invalid arguments then abort
    if ((pool == null) || (xqueryResource == null) || (user == null)) {
        abort("BrokerPool or XQueryResource or User was null!");
    }
    try (final DBBroker broker = pool.get(Optional.of(user))) {
        if (xqueryResource.indexOf(':') > 0) {
            final Source source = SourceFactory.getSource(broker, "", xqueryResource, true);
            if (source != null) {
                executeXQuery(pool, broker, source, params);
                return;
            }
        } else {
            final XmldbURI pathUri = XmldbURI.create(xqueryResource);
            try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
                if (lockedResource != null) {
                    final Source source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
                    executeXQuery(pool, broker, source, params);
                    return;
                }
            }
        }
        LOG.warn("XQuery User Job not found: {}, job not scheduled", xqueryResource);
    } catch (final EXistException ee) {
        abort("Could not get DBBroker!");
    } catch (final PermissionDeniedException pde) {
        abort("Permission denied for the scheduling user: " + user.getName() + "!");
    } catch (final XPathException xpe) {
        abort("XPathException in the Job: " + xpe.getMessage() + "!", unschedule);
    } catch (final IOException e) {
        abort("Could not load XQuery: " + e.getMessage());
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) XPathException(org.exist.xquery.XPathException) EXistException(org.exist.EXistException) IOException(java.io.IOException) Properties(java.util.Properties) Subject(org.exist.security.Subject) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 72 with LockedDocument

use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.

the class LocalCollection method removeResource.

@Override
public void removeResource(final Resource res) throws XMLDBException {
    if (res == null) {
        return;
    }
    final XmldbURI resURI;
    try {
        resURI = XmldbURI.xmldbUriFor(res.getId());
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    modify().apply((collection, broker, transaction) -> {
        // Check that the document exists
        try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, resURI, LockMode.WRITE_LOCK)) {
            if (lockedDocument == null) {
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                collection.close();
                throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource " + resURI + " not found");
            }
            if (XMLResource.RESOURCE_TYPE.equals(res.getResourceType())) {
                collection.removeXMLResource(transaction, broker, resURI);
            } else {
                collection.removeBinaryResource(transaction, broker, resURI);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
        }
        return null;
    });
    this.needsSync = true;
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException)

Example 73 with LockedDocument

use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.

the class LocalXUpdateQueryService method updateResource.

@Override
public long updateResource(final String id, final String commands) throws XMLDBException {
    return this.<Long>withDb((broker, transaction) -> {
        final long start = System.currentTimeMillis();
        final MutableDocumentSet docs = this.<MutableDocumentSet>read(broker, transaction, collection.getPathURI()).apply((collection, broker1, transaction1) -> {
            MutableDocumentSet d = new DefaultDocumentSet();
            if (id == null) {
                d = collection.allDocs(broker1, d, true);
            } else {
                try {
                    final XmldbURI resourceURI = XmldbURI.xmldbUriFor(id);
                    try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, resourceURI, Lock.LockMode.READ_LOCK)) {
                        // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                        collection.close();
                        final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
                        if (doc == null) {
                            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource not found: " + id);
                        }
                        d.add(doc);
                    }
                } catch (final URISyntaxException e) {
                    throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
                }
            }
            return d;
        });
        try (final Reader reader = new StringReader(commands)) {
            if (processor == null) {
                processor = new XUpdateProcessor(broker, docs);
            } else {
                processor.setBroker(broker);
                processor.setDocumentSet(docs);
            }
            final Modification[] modifications = processor.parse(new InputSource(reader));
            long mods = 0;
            for (Modification modification : modifications) {
                mods += modification.process(transaction);
                broker.flush();
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("xupdate took {}ms.", System.currentTimeMillis() - start);
            }
            return mods;
        } catch (final ParserConfigurationException | SAXException | LockException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        } finally {
            if (processor != null) {
                processor.reset();
            }
        }
    });
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XMLDBException(org.xmldb.api.base.XMLDBException) Reader(java.io.Reader) StringReader(java.io.StringReader) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) LockedDocument(org.exist.dom.persistent.LockedDocument) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 74 with LockedDocument

use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.

the class LocalCollectionManagementService method copyResource.

private void copyResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name, final PreserveType preserve) throws XMLDBException {
    final XmldbURI srcPath = resolve(src);
    final XmldbURI destPath = dest == null ? srcPath.removeLastSegment() : resolve(dest);
    final XmldbURI newName;
    if (name == null) {
        newName = srcPath.lastSegment();
    } else {
        newName = name;
    }
    withDb((broker, transaction) -> read(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
        try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.READ_LOCK)) {
            final DocumentImpl source = lockedSource == null ? null : lockedSource.getDocument();
            if (source == null) {
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                sourceCol.close();
                throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, "Resource " + srcPath + " not found");
            }
            return modify(b1, t1, destPath).apply((destinationCol, b2, t2) -> {
                try (final ManagedDocumentLock lockedDestination = b2.getBrokerPool().getLockManager().acquireDocumentWriteLock(destinationCol.getURI().append(newName))) {
                    try {
                        b2.copyResource(t2, source, destinationCol, newName, preserve);
                        // NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
                        destinationCol.close();
                        sourceCol.close();
                        return null;
                    } catch (final EXistException e) {
                        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "failed to copy resource " + srcPath, e);
                    }
                }
            });
        }
    }));
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) XMLDBException(org.xmldb.api.base.XMLDBException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 75 with LockedDocument

use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.

the class Eval method loadQueryFromURI.

/**
 * @param expr
 * @throws XPathException
 * @throws NullPointerException
 * @throws IllegalArgumentException
 */
private Source loadQueryFromURI(final Item expr) throws XPathException, NullPointerException, IllegalArgumentException {
    final String location = expr.getStringValue();
    Source querySource = null;
    if (location.indexOf(':') < 0 || location.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        try {
            XmldbURI locationUri = XmldbURI.xmldbUriFor(location);
            // be added.
            if (location.indexOf('/') < 0 || location.startsWith(".")) {
                final XmldbURI moduleLoadPathUri = XmldbURI.xmldbUriFor(context.getModuleLoadPath());
                locationUri = moduleLoadPathUri.resolveCollectionPath(locationUri);
            }
            try (final LockedDocument lockedSourceDoc = context.getBroker().getXMLResource(locationUri.toCollectionPathURI(), LockMode.READ_LOCK)) {
                final DocumentImpl sourceDoc = lockedSourceDoc == null ? null : lockedSourceDoc.getDocument();
                if (sourceDoc == null) {
                    throw new XPathException(this, "source for module " + location + " not found in database");
                }
                if (sourceDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(sourceDoc.getMetadata().getMimeType())) {
                    throw new XPathException(this, "source for module " + location + " is not an XQuery or " + "declares a wrong mime-type");
                }
                querySource = new DBSource(context.getBroker(), (BinaryDocument) sourceDoc, true);
            } catch (final PermissionDeniedException e) {
                throw new XPathException(this, "permission denied to read module source from " + location);
            }
        } catch (final URISyntaxException e) {
            throw new XPathException(this, e);
        }
    } else {
        // No. Load from file or URL
        try {
            // TODO: use URIs to ensure proper resolution of relative locations
            querySource = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), location, true);
            if (querySource == null) {
                throw new XPathException(this, "source for query at " + location + " not found");
            }
        } catch (final MalformedURLException e) {
            throw new XPathException(this, "source location for query at " + location + " should be a valid URL: " + e.getMessage());
        } catch (final IOException e) {
            throw new XPathException(this, "source for query at " + location + " not found: " + e.getMessage());
        } catch (final PermissionDeniedException e) {
            throw new XPathException(this, "Permission denied to access query at " + location + " : " + e.getMessage());
        }
    }
    return querySource;
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) MalformedURLException(java.net.MalformedURLException) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) FileSource(org.exist.source.FileSource) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)91 DocumentImpl (org.exist.dom.persistent.DocumentImpl)40 XmldbURI (org.exist.xmldb.XmldbURI)39 DBBroker (org.exist.storage.DBBroker)36 PermissionDeniedException (org.exist.security.PermissionDeniedException)30 Collection (org.exist.collections.Collection)28 Txn (org.exist.storage.txn.Txn)27 BrokerPool (org.exist.storage.BrokerPool)21 EXistException (org.exist.EXistException)20 Test (org.junit.Test)20 IOException (java.io.IOException)18 BinaryDocument (org.exist.dom.persistent.BinaryDocument)18 Serializer (org.exist.storage.serializers.Serializer)15 XPathException (org.exist.xquery.XPathException)14 URISyntaxException (java.net.URISyntaxException)13 InputStream (java.io.InputStream)11 LockException (org.exist.util.LockException)11 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)9 Lock (org.exist.storage.lock.Lock)9 TransactionManager (org.exist.storage.txn.TransactionManager)9