Search in sources :

Example 11 with LockedDocument

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

the class LocalCollectionManagementService method moveResource.

@Override
public void moveResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name) 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) -> modify(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
        try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.WRITE_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))) {
                    b2.moveResource(t2, source, destinationCol, newName);
                    // NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
                    destinationCol.close();
                    sourceCol.close();
                }
                return null;
            });
        }
    }));
}
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) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 12 with LockedDocument

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

the class LocalCollection method getResource.

Resource getResource(final DBBroker broker, final Txn transaction, final XmldbURI idURI) throws XMLDBException {
    return this.<Resource>read(broker, transaction).apply((collection, broker1, transaction1) -> {
        try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, idURI, LockMode.READ_LOCK)) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            final DocumentImpl document = lockedDocument == null ? null : lockedDocument.getDocument();
            if (document == null) {
                LOG.warn("Resource {} not found", idURI);
                return null;
            }
            final Resource r;
            switch(document.getResourceType()) {
                case DocumentImpl.XML_FILE:
                    r = new LocalXMLResource(user, brokerPool, this, idURI);
                    break;
                case DocumentImpl.BINARY_FILE:
                    r = new LocalBinaryResource(user, brokerPool, this, idURI);
                    break;
                default:
                    throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type");
            }
            ((AbstractEXistResource) r).setMimeType(document.getMimeType());
            return r;
        }
    });
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 13 with LockedDocument

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

the class CollectionStoreTest method store.

@Test
public void store() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
            broker.storeDocument(transaction, TEST_XML_DOC_URI, new StringInputSource(TEST_XML_DOC), MimeType.XML_TYPE, col);
            broker.saveCollection(transaction, col);
        }
        try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
            try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_XML_DOC_URI, LockMode.READ_LOCK)) {
                // NOTE: early release of collection lock inline with async locking
                col.close();
                if (lockedDoc != null) {
                    final Source expected = Input.fromString(TEST_XML_DOC).build();
                    final Source actual = Input.fromDocument(lockedDoc.getDocument()).build();
                    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
                    assertFalse(diff.toString(), diff.hasDifferences());
                }
            }
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) StringInputSource(org.exist.util.StringInputSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 14 with LockedDocument

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

the class CollectionStoreTest method storeBinary.

private void storeBinary(final PreserveType preserveOnCopy) throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
            final byte[] bin = TEST_BIN_DOC.getBytes(UTF_8);
            try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
                final int docId = broker.getNextResourceId(transaction);
                final BinaryDocument binDoc = col.addBinaryResource(transaction, broker, new BinaryDocument(broker.getBrokerPool(), col, docId, TEST_BIN_DOC_URI), is, "text/plain", bin.length, null, null, preserveOnCopy);
                assertNotNull(binDoc);
            }
            broker.saveCollection(transaction, col);
        }
        try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
            try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_BIN_DOC_URI, LockMode.READ_LOCK)) {
                // NOTE: early release of collection lock inline with async locking
                col.close();
                if (lockedDoc != null) {
                    assertTrue(lockedDoc.getDocument() instanceof BinaryDocument);
                    final BinaryDocument doc = (BinaryDocument) lockedDoc.getDocument();
                    final Try<String, IOException> docContent = broker.withBinaryFile(transaction, doc, is -> Try.TaggedTryUnchecked(IOException.class, () -> new String(Files.readAllBytes(is), UTF_8)));
                    assertEquals(TEST_BIN_DOC, docContent.get());
                }
            }
        }
        transaction.commit();
    }
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BrokerPool(org.exist.storage.BrokerPool)

Example 15 with LockedDocument

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

the class StylesheetResolverAndCompiler method templates.

public <E extends Exception> Templates templates(DBBroker broker, XSLTErrorsListener<E> errorListener) throws E, TransformerConfigurationException, IOException, PermissionDeniedException, SAXException {
    if (uri.startsWith(XmldbURI.EMBEDDED_SERVER_URI_PREFIX)) {
        final String docPath = uri.substring(XmldbURI.EMBEDDED_SERVER_URI_PREFIX.length());
        try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(docPath), LockMode.READ_LOCK)) {
            if (lockedDocument == null) {
                throw new IOException("XSL stylesheet not found: " + docPath);
            }
            final DocumentImpl doc = lockedDocument.getDocument();
            if (templates == null || doc.getLastModified() > lastModified) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("compiling stylesheet {}", doc.getURI());
                }
                templates = compileTemplates(broker, doc, errorListener);
                lastModified = doc.getLastModified();
            }
        }
    } else {
        final URL url = new URL(uri);
        final URLConnection connection = url.openConnection();
        long modified = connection.getLastModified();
        if (templates == null || modified > lastModified || modified == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("compiling stylesheet {}", url);
            }
            try (final InputStream is = connection.getInputStream()) {
                templates = factory(broker.getBrokerPool(), errorListener).newTemplates(new StreamSource(is));
            }
        }
        lastModified = modified;
    }
    return templates;
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) LockedDocument(org.exist.dom.persistent.LockedDocument) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

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