Search in sources :

Example 11 with ReferentialIntegrityException

use of javax.jcr.ReferentialIntegrityException in project jackrabbit by apache.

the class InternalVersionHistoryImpl method removeVersion.

/**
 * Removes the indicated version from this VersionHistory. If the specified
 * vesion does not exist, if it specifies the root version or if it is
 * referenced by any node e.g. as base version, a VersionException is thrown.
 * <p>
 * all successors of the removed version become successors of the
 * predecessors of the removed version and vice versa. then, the entire
 * version node and all its subnodes are removed.
 *
 * @param versionName name of the version to remove
 * @throws VersionException if removal is not possible
 */
synchronized void removeVersion(Name versionName) throws RepositoryException {
    InternalVersionImpl v = (InternalVersionImpl) getVersion(versionName);
    if (v.equals(rootVersion)) {
        String msg = "Removal of " + versionName + " not allowed.";
        log.debug(msg);
        throw new VersionException(msg);
    }
    // check if any references (from outside the version storage) exist on this version
    if (vMgr.hasItemReferences(v.getId())) {
        throw new ReferentialIntegrityException("Unable to remove version. At least once referenced.");
    }
    // unregister from labels
    Name[] labels = v.internalGetLabels();
    for (Name label : labels) {
        v.internalRemoveLabel(label);
        labelNode.removeProperty(label);
    }
    // detach from the version graph
    v.internalDetach();
    // check if referenced by an activity
    InternalActivityImpl activity = v.getActivity();
    if (activity != null) {
        activity.removeVersion(v);
    }
    // remove from persistence state
    node.removeNode(v.getName());
    // and remove from history
    versionCache.remove(v.getId());
    nameCache.remove(versionName);
    vMgr.versionDestroyed(v);
    // Check if this was the last version in addition to the root version
    if (!vMgr.hasItemReferences(node.getNodeId())) {
        log.debug("Current version history has no references");
        NodeStateEx[] childNodes = node.getChildNodes();
        // Check if there is only root version and version labels nodes
        if (childNodes.length == 2) {
            log.debug("Removing orphan version history as it contains only two children");
            NodeStateEx parentNode = node.getParent();
            // Remove version history node
            parentNode.removeNode(node.getName());
            // store changes for this node and his children
            parentNode.store();
        } else {
            node.store();
        }
    } else {
        log.debug("Current version history has at least one reference");
        // store changes
        node.store();
    }
    // now also remove from labelCache
    for (Name label : labels) {
        labelCache.remove(label);
    }
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) VersionException(javax.jcr.version.VersionException) Name(org.apache.jackrabbit.spi.Name)

Example 12 with ReferentialIntegrityException

use of javax.jcr.ReferentialIntegrityException in project jackrabbit by apache.

the class InternalVersionManagerBase method internalRemoveActivity.

/**
 * Removes the specified activity
 *
 * @param activity the activity to remove
 * @throws javax.jcr.RepositoryException if any other error occurs.
 */
protected void internalRemoveActivity(InternalActivityImpl activity) throws RepositoryException {
    WriteOperation operation = startWriteOperation();
    try {
        // check if the activity has any references in the workspaces
        NodeId nodeId = activity.getId();
        if (stateMgr.hasNodeReferences(nodeId)) {
            NodeReferences refs = stateMgr.getNodeReferences(nodeId);
            if (refs.hasReferences()) {
                throw new ReferentialIntegrityException("Unable to delete activity. still referenced.");
            }
        }
        // TODO:
        // check if the activity is used in anywhere in the version storage
        // and reject removal
        // remove activity and possible empty parent directories
        NodeStateEx act = getNodeStateEx(nodeId);
        NodeId parentId = act.getParentId();
        Name name = act.getName();
        while (parentId != null) {
            NodeStateEx parent = getNodeStateEx(parentId);
            parent.removeNode(name);
            parent.store();
            if (parent.getChildNodes().length == 0 && !parentId.equals(activitiesId)) {
                name = parent.getName();
                parentId = parent.getParentId();
            } else {
                parentId = null;
            }
        }
        operation.save();
    } catch (ItemStateException e) {
        log.error("Error while storing: " + e.toString());
    } finally {
        operation.close();
    }
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) NodeId(org.apache.jackrabbit.core.id.NodeId) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) Name(org.apache.jackrabbit.spi.Name) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 13 with ReferentialIntegrityException

use of javax.jcr.ReferentialIntegrityException in project jackrabbit by apache.

the class NodeImplTest method testReferentialIntegrityCorruptionGetPath.

/**
 * Test corruption in session / persistence state after
 * {@link ReferentialIntegrityException}.
 * <p>
 * This is a variant of {@link #testReferentialIntegrityCorruption()}
 * that checks that {@link Node#getPath()} works after the save operation.
 *
 * @see <a href="https://issues.apache.org/jira/browse/JCR-3018">JCR-3018</a>
 */
public void testReferentialIntegrityCorruptionGetPath() throws Exception {
    Session session = testRootNode.getSession();
    Node root = testRootNode.addNode("testReferentialIntegrityCorruption");
    // Create test nodes P1 and P2
    Node nodeP1 = root.addNode("P1");
    nodeP1.addMixin("mix:referenceable");
    Node nodeP2 = root.addNode("P2");
    nodeP2.addMixin("mix:referenceable");
    session.save();
    // Create reference from P2 to P1 and save
    nodeP2.setProperty("referencetoP1", nodeP1);
    session.save();
    // Add node P3
    Node nodeP3 = root.addNode("P3");
    nodeP3.addMixin("mix:referenceable");
    String nodeP3path = nodeP3.getPath();
    // And try to remove P1 while P2 still references P1
    nodeP1.remove();
    try {
        session.save();
    } catch (ReferentialIntegrityException expected) {
    // Got ReferentialIntegrityException as expected
    }
    // should be removed and P3 should exist
    try {
        nodeP2.remove();
        session.save();
    } catch (Exception e) {
        String msg = "JCR-3018: Saving delete after" + " ReferentialIntegrityException failed";
        log.error(msg, e);
        fail(msg);
    }
    try {
        assertEquals(nodeP3path, nodeP3.getPath());
        nodeP3 = session.getNodeByIdentifier(nodeP3.getIdentifier());
    } catch (Exception e) {
        String msg = "JCR-3018: getting path of P3 failed. Corrupt session?";
        log.error(msg, e);
        fail(msg);
    }
    root.remove();
    session.save();
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) Node(javax.jcr.Node) ItemExistsException(javax.jcr.ItemExistsException) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) RepositoryException(javax.jcr.RepositoryException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Session(javax.jcr.Session)

Example 14 with ReferentialIntegrityException

use of javax.jcr.ReferentialIntegrityException in project jackrabbit by apache.

the class NodeImplTest method testReferentialIntegrityCorruption.

/**
 * Test corruption in session / persistence state after
 * {@link ReferentialIntegrityException}.
 *
 * @see <a href="https://issues.apache.org/jira/browse/JCR-2503">JCR-2503</a>
 */
public void testReferentialIntegrityCorruption() throws Exception {
    Session session = testRootNode.getSession();
    Node root = testRootNode.addNode("testReferentialIntegrityCorruption");
    // Create test nodes P1 and P2
    Node nodeP1 = root.addNode("P1");
    nodeP1.addMixin("mix:referenceable");
    Node nodeP2 = root.addNode("P2");
    nodeP2.addMixin("mix:referenceable");
    session.save();
    // Create reference from P2 to P1 and save
    nodeP2.setProperty("referencetoP1", nodeP1);
    session.save();
    // Add node P3
    Node nodeP3 = root.addNode("P3");
    nodeP3.addMixin("mix:referenceable");
    // And try to remove P1 while P2 still references P1
    nodeP1.remove();
    try {
        session.save();
    } catch (ReferentialIntegrityException expected) {
    // Got ReferentialIntegrityException as expected
    }
    // should be removed and P3 should exist
    try {
        nodeP2.remove();
        session.save();
    } catch (Exception e) {
        String msg = "JCR-2503: Saving delete after" + " ReferentialIntegrityException failed";
        log.error(msg, e);
        fail(msg);
    }
    try {
        nodeP3 = session.getNodeByIdentifier(nodeP3.getIdentifier());
    } catch (Exception e) {
        String msg = "JCR-2503: Retrieving P3 by uuid failed. Corrupt session?";
        log.error(msg, e);
        fail(msg);
    }
    try {
        nodeP3.remove();
        session.save();
    } catch (Exception e) {
        String msg = "JCR-2503: Removing P3 failed. Corrupt session?";
        log.error(msg, e);
        fail(msg);
    }
    try {
        root = testRootNode.getNode("testReferentialIntegrityCorruption");
        for (Node ignore : JcrUtils.getChildNodes(root)) {
        }
    } catch (Exception e) {
        String msg = "JCR-2503: Failed to scan empty node. Corrupt session?";
        log.error(msg, e);
        fail(msg);
    }
    root.remove();
    session.save();
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) Node(javax.jcr.Node) ItemExistsException(javax.jcr.ItemExistsException) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) RepositoryException(javax.jcr.RepositoryException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Session(javax.jcr.Session)

Aggregations

ReferentialIntegrityException (javax.jcr.ReferentialIntegrityException)14 Node (javax.jcr.Node)11 RepositoryException (javax.jcr.RepositoryException)3 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)3 ItemExistsException (javax.jcr.ItemExistsException)2 Session (javax.jcr.Session)2 Version (javax.jcr.version.Version)2 NodeId (org.apache.jackrabbit.core.id.NodeId)2 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)2 NodeReferences (org.apache.jackrabbit.core.state.NodeReferences)2 Name (org.apache.jackrabbit.spi.Name)2 AccessDeniedException (javax.jcr.AccessDeniedException)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 Value (javax.jcr.Value)1 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)1 VersionException (javax.jcr.version.VersionException)1 VersionManager (javax.jcr.version.VersionManager)1 UserTransactionImpl (org.apache.jackrabbit.core.UserTransactionImpl)1 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)1