Search in sources :

Example 1 with TransactionManager

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

the class CollectionConfigurationManager method checkRootCollectionConfig.

/**
 * Create a stored default configuration document for the root collection
 *
 * @param broker The broker which will do the operation
 * @throws EXistException if something goes wrong
 * @throws PermissionDeniedException if user does not have sufficient rights
 */
public void checkRootCollectionConfig(DBBroker broker) throws EXistException, PermissionDeniedException {
    // Copied from the legacy conf.xml in order to make the test suite work
    // TODO : backward compatibility could be ensured by copying the
    // relevant parts of conf.xml
    final String configuration = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\">" + "    <index>" + "    </index>" + "</collection>";
    final TransactionManager transact = broker.getDatabase().getTransactionManager();
    try (final Txn txn = transact.beginTransaction()) {
        try (final Collection collection = broker.openCollection(XmldbURI.ROOT_COLLECTION_URI, LockMode.READ_LOCK)) {
            if (collection == null) {
                transact.abort(txn);
                throw new EXistException("collection " + XmldbURI.ROOT_COLLECTION_URI + " not found!");
            }
            final CollectionConfiguration conf = getConfiguration(collection);
            if (conf != null) {
                // it
                if (conf.getDocName() != null) {
                    transact.abort(txn);
                    return;
                }
            }
            // Configure the root collection
            addConfiguration(txn, broker, collection, configuration);
            LOG.info("Configured '{}'", collection.getURI());
        }
        transact.commit(txn);
    } catch (final CollectionConfigurationException e) {
        throw new EXistException(e.getMessage());
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 2 with TransactionManager

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

the class Configurator method save.

public static DocumentImpl save(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI uri) throws IOException, ConfigurationException {
    final StringWriter writer = new StringWriter();
    final SAXSerializer serializer = new SAXSerializer(writer, null);
    try {
        serializer.startDocument();
        serialize(instance, serializer);
        serializer.endDocument();
    } catch (final SAXException saxe) {
        throw new ConfigurationException(saxe.getMessage(), saxe);
    }
    final String data = writer.toString();
    if (data == null || data.length() == 0) {
        return null;
    }
    FullXmldbURI fullURI = null;
    final BrokerPool pool = broker.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    LOG.info("Storing configuration {}/{}", collection.getURI(), uri);
    final SecurityManager securityManager = pool.getSecurityManager();
    try {
        final Subject systemSubject = securityManager.getSystemSubject();
        broker.pushSubject(systemSubject);
        Txn txn = broker.getCurrentTransaction();
        final boolean txnInProgress = txn != null;
        if (!txnInProgress) {
            txn = transact.beginTransaction();
        }
        try {
            txn.acquireCollectionLock(() -> pool.getLockManager().acquireCollectionWriteLock(collection.getURI()));
            fullURI = getFullURI(pool, collection.getURI().append(uri));
            saving.add(fullURI);
            final Permission systemResourcePermission = PermissionFactory.getDefaultResourcePermission(pool.getSecurityManager());
            systemResourcePermission.setOwner(systemSubject);
            systemResourcePermission.setGroup(systemSubject.getDefaultGroup());
            systemResourcePermission.setMode(Permission.DEFAULT_SYSTEM_RESOURCE_PERM);
            broker.storeDocument(txn, uri, new StringInputSource(data), MimeType.XML_TYPE, null, null, systemResourcePermission, null, null, collection);
            broker.saveCollection(txn, collection);
            if (!txnInProgress) {
                transact.commit(txn);
            }
        } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
            if (!txnInProgress) {
                transact.abort(txn);
            }
            throw e;
        } finally {
            if (!txnInProgress) {
                txn.close();
            }
        }
        saving.remove(fullURI);
        broker.flush();
        broker.sync(Sync.MAJOR);
        return collection.getDocument(broker, uri.lastSegment());
    } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
        LOG.error(e);
        if (fullURI != null) {
            saving.remove(fullURI);
        }
        throw new IOException(e);
    } finally {
        broker.popSubject();
    }
}
Also used : SecurityManager(org.exist.security.SecurityManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) StringInputSource(org.exist.util.StringInputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) FullXmldbURI(org.exist.xmldb.FullXmldbURI) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool)

Example 3 with TransactionManager

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

the class XQueryStartupTrigger method createAutostartCollection.

/**
 * Create autostart collection when not existent
 *
 * @param broker The exist-db broker
 */
private void createAutostartCollection(DBBroker broker) {
    LOG.info("Creating {}", AUTOSTART_COLLECTION);
    final TransactionManager txnManager = broker.getBrokerPool().getTransactionManager();
    try (final Txn txn = txnManager.beginTransaction()) {
        XmldbURI newCollection = XmldbURI.create(AUTOSTART_COLLECTION, true);
        // Create collection
        final Collection created = broker.getOrCreateCollection(txn, newCollection);
        // Set ownership and mode
        PermissionFactory.chown(broker, created, Optional.of(SecurityManager.SYSTEM), Optional.of(SecurityManager.DBA_GROUP));
        PermissionFactory.chmod(broker, created, Optional.of(Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM), Optional.empty());
        broker.saveCollection(txn, created);
        broker.flush();
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation of collection");
        }
    } catch (Throwable ex) {
        LOG.error(ex);
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 4 with TransactionManager

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

the class ElementImpl method insertBefore.

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {
    if (refChild == null) {
        return appendChild(newChild);
    } else if (!(refChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
    }
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        insertBefore(transaction, nl, refChild);
        broker.storeXMLResource(transaction, getOwnerDocument());
        transact.commit(transaction);
        return refChild.getPreviousSibling();
    } catch (final TransactionException e) {
        throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, e.getMessage());
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : org.w3c.dom(org.w3c.dom) TransactionException(org.exist.storage.txn.TransactionException) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 5 with TransactionManager

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

the class ElementImpl method appendChild.

@Override
public Node appendChild(final Node newChild) throws DOMException {
    if (newChild.getNodeType() != Node.DOCUMENT_NODE && newChild.getOwnerDocument() != null && newChild.getOwnerDocument() != ownerDocument) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Owning document IDs do not match");
    }
    if (newChild == this) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Cannot append an element to itself");
    }
    if (newChild.getNodeType() == DOCUMENT_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Node may not be appended to an element");
    }
    if (newChild.getNodeType() == DOCUMENT_TYPE_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Type Node may not be appended to an element");
    }
    if (newChild instanceof IStoredNode) {
        final NodeId newChildId = ((IStoredNode) newChild).getNodeId();
        if (newChildId != null && getNodeId().isDescendantOf(newChildId)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "The node to append is one of this node's ancestors");
        }
    }
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        appendChildren(transaction, nl, 0);
        broker.storeXMLResource(transaction, getOwnerDocument());
        // bugID 3419602
        transact.commit(transaction);
        return getLastChild();
    } catch (final Exception e) {
        throw new DOMException(DOMException.INVALID_STATE_ERR, e.getMessage());
    }
}
Also used : org.w3c.dom(org.w3c.dom) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) Txn(org.exist.storage.txn.Txn) XMLStreamException(javax.xml.stream.XMLStreamException) EXistException(org.exist.EXistException) IllegalQNameException(org.exist.dom.QName.IllegalQNameException) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException)

Aggregations

TransactionManager (org.exist.storage.txn.TransactionManager)131 Txn (org.exist.storage.txn.Txn)128 Collection (org.exist.collections.Collection)86 DBBroker (org.exist.storage.DBBroker)70 BrokerPool (org.exist.storage.BrokerPool)61 StringInputSource (org.exist.util.StringInputSource)27 EXistException (org.exist.EXistException)21 XmldbURI (org.exist.xmldb.XmldbURI)20 Sequence (org.exist.xquery.value.Sequence)20 XQuery (org.exist.xquery.XQuery)18 InputSource (org.xml.sax.InputSource)18 DocumentImpl (org.exist.dom.persistent.DocumentImpl)17 Test (org.junit.Test)16 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 InputStream (java.io.InputStream)14 StringReader (java.io.StringReader)13 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)13 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 IOException (java.io.IOException)12 Modification (org.exist.xupdate.Modification)12