Search in sources :

Example 1 with StoredNode

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

the class Modification method selectAndLock.

/**
 * Acquire a lock on all documents processed by this modification. We have
 * to avoid that node positions change during the operation.
 * feature trigger_update :
 * At the same time we leverage on the fact that it's called before
 * database modification to call the eventual triggers.
 *
 * @param transaction the database transaction.
 *
 * @return The selected document nodes.
 *
 * @throws LockException if a lock error occurs
 * @throws PermissionDeniedException if the caller has insufficient priviledges
 * @throws EXistException if the database raises an error
 * @throws XPathException if the XPath raises an error
 * @throws TriggerException if a trigger raises an error
 */
protected final StoredNode[] selectAndLock(Txn transaction) throws LockException, PermissionDeniedException, EXistException, XPathException, TriggerException {
    final java.util.concurrent.locks.Lock globalLock = broker.getBrokerPool().getGlobalUpdateLock();
    globalLock.lock();
    try {
        final NodeList nl = select(docs);
        final DocumentSet lockedDocuments = ((NodeSet) nl).getDocumentSet();
        // acquire a lock on all documents
        // we have to avoid that node positions change
        // during the modification
        lockedDocumentsLocks = lockedDocuments.lock(broker, true);
        final StoredNode[] ql = new StoredNode[nl.getLength()];
        for (int i = 0; i < ql.length; i++) {
            ql[i] = (StoredNode) nl.item(i);
            final DocumentImpl doc = ql[i].getOwnerDocument();
            // call the eventual triggers
            // TODO -jmv separate loop on docs and not on nodes
            // prepare Trigger
            prepareTrigger(transaction, doc);
        }
        return ql;
    } finally {
        globalLock.unlock();
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NodeList(org.w3c.dom.NodeList) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 2 with StoredNode

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

the class Append method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            node.appendChildren(transaction, children, child);
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        // release all acquired locks
        unlockDocuments(transaction);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 3 with StoredNode

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

the class Insert method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        final int len = children.getLength();
        if (LOG.isDebugEnabled()) {
            LOG.debug("found {} nodes to insert", len);
        }
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("permission to update document denied");
            }
            final NodeImpl parent = (NodeImpl) getParent(node);
            switch(mode) {
                case INSERT_BEFORE:
                    parent.insertBefore(transaction, children, node);
                    break;
                case INSERT_AFTER:
                    parent.insertAfter(transaction, children, node);
                    break;
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        unlockDocuments(transaction);
    }
}
Also used : NodeImpl(org.exist.dom.persistent.NodeImpl) NodeList(org.w3c.dom.NodeList) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 4 with StoredNode

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

the class Remove method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            final NodeImpl parent = (NodeImpl) getParent(node);
            if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE) {
                throw new EXistException("you cannot remove the document element. Use update " + "instead");
            } else {
                parent.removeChild(transaction, node);
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        unlockDocuments(transaction);
    }
}
Also used : NodeImpl(org.exist.dom.persistent.NodeImpl) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 5 with StoredNode

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

the class Update method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    int modifications = children.getLength();
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            if (node == null) {
                LOG.warn("select {} returned empty node", selectStmt);
                continue;
            }
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            switch(node.getNodeType()) {
                case Node.ELEMENT_NODE:
                    if (modifications == 0) {
                        modifications = 1;
                    }
                    ((ElementImpl) node).update(transaction, children);
                    break;
                case Node.TEXT_NODE:
                    final ElementImpl textParent = (ElementImpl) node.getParentNode();
                    final Node textTemp = children.item(0);
                    final TextImpl text = new TextImpl(textTemp.getNodeValue());
                    modifications = 1;
                    text.setOwnerDocument(doc);
                    textParent.updateChild(transaction, node, text);
                    break;
                case Node.ATTRIBUTE_NODE:
                    final ElementImpl attrParent = (ElementImpl) ((Attr) node).getOwnerElement();
                    if (attrParent == null) {
                        LOG.warn("parent node not found for {}", node.getNodeId());
                        break;
                    }
                    final AttrImpl attr = (AttrImpl) node;
                    final Node attrTemp = children.item(0);
                    final AttrImpl attribute = new AttrImpl(attr.getQName(), attrTemp.getNodeValue(), broker.getBrokerPool().getSymbols());
                    attribute.setOwnerDocument(doc);
                    attrParent.updateChild(transaction, node, attribute);
                    break;
                default:
                    throw new EXistException("unsupported node-type");
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
    } finally {
        unlockDocuments(transaction);
    }
    return modifications;
}
Also used : ElementImpl(org.exist.dom.persistent.ElementImpl) NodeList(org.w3c.dom.NodeList) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) AttrImpl(org.exist.dom.persistent.AttrImpl) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) TextImpl(org.exist.dom.persistent.TextImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Aggregations

StoredNode (org.exist.dom.persistent.StoredNode)13 DocumentImpl (org.exist.dom.persistent.DocumentImpl)12 NotificationService (org.exist.storage.NotificationService)9 EXistException (org.exist.EXistException)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 NodeList (org.w3c.dom.NodeList)8 TriggerException (org.exist.collections.triggers.TriggerException)4 AttrImpl (org.exist.dom.persistent.AttrImpl)4 ElementImpl (org.exist.dom.persistent.ElementImpl)4 NodeImpl (org.exist.dom.persistent.NodeImpl)4 TextImpl (org.exist.dom.persistent.TextImpl)4 Txn (org.exist.storage.txn.Txn)4 LockException (org.exist.util.LockException)4 XPathException (org.exist.xquery.XPathException)4 Sequence (org.exist.xquery.value.Sequence)4 StringValue (org.exist.xquery.value.StringValue)4 ValueSequence (org.exist.xquery.value.ValueSequence)4 Item (org.exist.xquery.value.Item)3 Node (org.w3c.dom.Node)3 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)2