Search in sources :

Example 6 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class PersistentDomTest method createCollection.

private static void createCollection(final DBBroker broker, final Txn transaction, final XmldbURI collectionUri, final Tuple2<XmldbURI, String>... docs) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
    try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collectionUri)) {
        final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
        broker.saveCollection(transaction, collection);
        for (final Tuple2<XmldbURI, String> doc : docs) {
            broker.storeDocument(transaction, doc._1, new StringInputSource(doc._2), MimeType.XML_TYPE, collection);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 7 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class EmbeddedBinariesTest method storeBinaryFile.

@Override
protected void storeBinaryFile(final XmldbURI filePath, final byte[] content) throws Exception {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final ManagedCollectionLock collectionLock = brokerPool.getLockManager().acquireCollectionWriteLock(filePath.removeLastSegment())) {
            final Collection collection = broker.getOrCreateCollection(transaction, filePath.removeLastSegment());
            broker.storeDocument(transaction, filePath.lastSegment(), new StringInputSource(content), MimeType.BINARY_TYPE, collection);
            broker.saveCollection(transaction, collection);
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 8 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class EmbeddedXMLStreamReaderTest method createCollection.

private static void createCollection(final DBBroker broker, final Txn transaction, final XmldbURI collectionUri, final Tuple2<XmldbURI, String>... docs) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
    try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collectionUri)) {
        final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
        broker.saveCollection(transaction, collection);
        for (final Tuple2<XmldbURI, String> doc : docs) {
            broker.storeDocument(transaction, doc._1, new StringInputSource(doc._2), MimeType.XML_TYPE, collection);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 9 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class TransformTest method createCollection.

@SafeVarargs
private static void createCollection(final DBBroker broker, final Txn transaction, final XmldbURI collectionUri, final Tuple2<XmldbURI, String>... docs) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
    try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collectionUri)) {
        final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
        broker.saveCollection(transaction, collection);
        for (final Tuple2<XmldbURI, String> doc : docs) {
            broker.storeDocument(transaction, doc._1, new StringInputSource(doc._2), MimeType.XML_TYPE, collection);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 10 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class PackageTriggerTest method setup.

@BeforeClass
public static void setup() throws PermissionDeniedException, SAXException, EXistException, IOException, LockException, XPathException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    // Create a collection to test the trigger in
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        final Collection collection = broker.getOrCreateCollection(transaction, triggerTestCollection);
        broker.saveCollection(transaction, collection);
        transaction.commit();
    }
    // Store XAR in database
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final ManagedCollectionLock collectionLock = brokerPool.getLockManager().acquireCollectionWriteLock(xarUri.removeLastSegment())) {
            final Collection collection = broker.getOrCreateCollection(transaction, xarUri.removeLastSegment());
            broker.storeDocument(transaction, xarUri.lastSegment(), new InputStreamSupplierInputSource(() -> PackageTriggerTest.class.getResourceAsStream("/" + xarFile)), MimeType.EXPATH_PKG_TYPE, collection);
            broker.saveCollection(transaction, collection);
        }
        transaction.commit();
    }
    // Install and deploy XAR
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final Sequence result = xquery.execute(broker, "repo:install-and-deploy-from-db('/db/" + xarFile + "')", null);
        Assert.assertEquals(1, result.getItemCount());
    }
    // Store collection.xconf in newly created collection under /db/system/config
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final Sequence result = xquery.execute(broker, "xmldb:create-collection('/db/system/config/db','trigger-test'), " + "xmldb:store('/db/system/config/db/trigger-test', '" + DEFAULT_COLLECTION_CONFIG_FILE + "', " + "<collection xmlns=\"http://exist-db.org/collection-config/1.0\"><triggers><trigger class=\"org.exist.repo.ExampleTrigger\"/></triggers></collection>)", null);
        Assert.assertEquals(2, result.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) InputStreamSupplierInputSource(org.exist.util.InputStreamSupplierInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) BeforeClass(org.junit.BeforeClass)

Aggregations

ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)11 Collection (org.exist.collections.Collection)9 StringInputSource (org.exist.util.StringInputSource)5 XmldbURI (org.exist.xmldb.XmldbURI)5 BrokerPool (org.exist.storage.BrokerPool)4 Txn (org.exist.storage.txn.Txn)4 DBBroker (org.exist.storage.DBBroker)3 URISyntaxException (java.net.URISyntaxException)2 EXistException (org.exist.EXistException)2 TriggerException (org.exist.collections.triggers.TriggerException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 SAXException (org.xml.sax.SAXException)2 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1