Search in sources :

Example 1 with ManagedCollectionLock

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

the class RestoreHandler method restoreCollectionEntry.

private DeferredPermission restoreCollectionEntry(final Attributes atts) throws SAXException {
    final String name = atts.getValue("name");
    if (name == null) {
        throw new SAXException("Collection requires a name attribute");
    }
    final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
    final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
    final String mode = getAttr(atts, "mode", "644");
    final String created = atts.getValue("created");
    final String strVersion = atts.getValue("version");
    if (strVersion != null) {
        try {
            this.version = Integer.parseInt(strVersion);
        } catch (final NumberFormatException nfe) {
            final String msg = "Could not parse version number for Collection '" + name + "', defaulting to version 0";
            listener.warn(msg);
            LOG.warn(msg);
            this.version = 0;
        }
    }
    try {
        listener.createdCollection(name);
        final XmldbURI collUri;
        if (version >= STRICT_URI_VERSION) {
            collUri = XmldbURI.create(name);
        } else {
            try {
                collUri = URIUtils.encodeXmldbUriFor(name);
            } catch (final URISyntaxException e) {
                listener.warn("Could not parse document name into a URI: " + e.getMessage());
                return new SkippedEntryDeferredPermission();
            }
        }
        if (version >= BLOB_STORE_VERSION) {
            this.deduplicateBlobs = Boolean.parseBoolean(atts.getValue("deduplicate-blobs"));
        } else {
            this.deduplicateBlobs = false;
        }
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        try (final Txn transaction = beginTransaction();
            final ManagedCollectionLock colLock = lockManager.acquireCollectionWriteLock(collUri)) {
            Collection collection = broker.getCollection(collUri);
            if (collection == null) {
                final Tuple2<Permission, Long> creationAttributes = Tuple(null, getDateFromXSDateTimeStringForItem(created, name).getTime());
                collection = broker.getOrCreateCollection(transaction, collUri, Optional.of(creationAttributes));
                broker.saveCollection(transaction, collection);
            }
            transaction.commit();
            this.currentCollectionUri = collection.getURI();
        }
        final DeferredPermission deferredPermission;
        if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
            // prevents restore of a backup from changing System collection ownership
            deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(mode, 8));
        } else {
            deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, owner, group, Integer.parseInt(mode, 8));
        }
        return deferredPermission;
    } catch (final IOException | LockException | TransactionException | PermissionDeniedException e) {
        final String msg = "An unrecoverable error occurred while restoring collection '" + name + "': " + e.getMessage() + ". Aborting restore!";
        LOG.error(msg, e);
        listener.warn(msg);
        throw new SAXException(msg, e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) LockManager(org.exist.storage.lock.LockManager) TransactionException(org.exist.storage.txn.TransactionException) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 2 with ManagedCollectionLock

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

the class LocalCollectionManagementService method createCollection.

@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
    final XmldbURI collName = resolve(name);
    withDb((broker, transaction) -> {
        try {
            final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
            try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
                broker.saveCollection(transaction, coll);
            }
            return null;
        } catch (final LockException | TriggerException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
    return new LocalCollection(user, brokerPool, collection, collName);
}
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) LockException(org.exist.util.LockException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) XMLDBException(org.xmldb.api.base.XMLDBException) TriggerException(org.exist.collections.triggers.TriggerException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 3 with ManagedCollectionLock

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

the class Txn method acquireCollectionLock.

public void acquireCollectionLock(final SupplierE<ManagedCollectionLock, LockException> fnLockAcquire) throws LockException {
    final ManagedCollectionLock lock = fnLockAcquire.get();
    locksHeld.add(new LockInfo(lock, lock::close));
}
Also used : ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 4 with ManagedCollectionLock

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

the class RpcConnection method createCollection.

private boolean createCollection(final XmldbURI collUri, final Date created) throws PermissionDeniedException, EXistException {
    withDb((broker, transaction) -> {
        Collection current = broker.getCollection(collUri);
        if (current != null) {
            return true;
        }
        current = broker.getOrCreateCollection(transaction, collUri, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
        try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collUri)) {
            broker.saveCollection(transaction, current);
        }
        return null;
    });
    LOG.info("collection {} has been created", collUri);
    return true;
}
Also used : Collection(org.exist.collections.Collection) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 5 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)

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