Search in sources :

Example 6 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class InMemPersistenceManager method loadReferencesTo.

/**
     * {@inheritDoc}
     */
public synchronized NodeReferences loadReferencesTo(NodeId id) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    byte[] data = refsStore.get(id);
    if (data == null) {
        throw new NoSuchItemStateException(id.toString());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    try {
        NodeReferences refs = new NodeReferences(id);
        Serializer.deserialize(refs, in);
        return refs;
    } catch (Exception e) {
        String msg = "failed to load references: " + id;
        log.debug(msg);
        throw new ItemStateException(msg, e);
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 7 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class ObjectPersistenceManager method loadReferencesTo.

/**
     * {@inheritDoc}
     */
public synchronized NodeReferences loadReferencesTo(NodeId id) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    String refsFilePath = buildNodeReferencesFilePath(id);
    try {
        if (!itemStateFS.isFile(refsFilePath)) {
            throw new NoSuchItemStateException(id.toString());
        }
    } catch (FileSystemException fse) {
        String msg = "failed to load references: " + id;
        log.debug(msg);
        throw new ItemStateException(msg, fse);
    }
    try {
        BufferedInputStream in = new BufferedInputStream(itemStateFS.getInputStream(refsFilePath));
        try {
            NodeReferences refs = new NodeReferences(id);
            Serializer.deserialize(refs, in);
            return refs;
        } finally {
            in.close();
        }
    } catch (Exception e) {
        String msg = "failed to load references: " + id;
        log.debug(msg);
        throw new ItemStateException(msg, e);
    }
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) BufferedInputStream(java.io.BufferedInputStream) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 8 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class BatchedItemOperations method checkRemoveNode.

/**
     * Checks if removing the given target node from the specifed parent
     * is allowed in the current context.
     *
     * @param targetState
     * @param parentId
     * @param options     bit-wise OR'ed flags specifying the checks that should be
     *                    performed; any combination of the following constants:
     *                    <ul>
     *                    <li><code>{@link #CHECK_ACCESS}</code>: make sure
     *                    current session is granted read access on parent
     *                    and remove privilege on target node</li>
     *                    <li><code>{@link #CHECK_LOCK}</code>: make sure
     *                    there's no foreign lock on parent node</li>
     *                    <li><code>{@link #CHECK_CHECKED_OUT}</code>: make sure
     *                    parent node is checked-out</li>
     *                    <li><code>{@link #CHECK_CONSTRAINTS}</code>:
     *                    make sure no node type constraints would be violated</li>
     *                    <li><code>{@link #CHECK_REFERENCES}</code>:
     *                    make sure no references exist on target node</li>
     *                    <li><code>{@link #CHECK_HOLD}</code>: check for effective holds preventing the add operation</li>
     *                    <li><code>{@link #CHECK_RETENTION}</code>: check for effective retention policy preventing the add operation</li>
     *                    </ul>
     * @throws ConstraintViolationException
     * @throws AccessDeniedException
     * @throws VersionException
     * @throws LockException
     * @throws ItemNotFoundException
     * @throws ReferentialIntegrityException
     * @throws RepositoryException
     */
public void checkRemoveNode(NodeState targetState, NodeId parentId, int options) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ReferentialIntegrityException, RepositoryException {
    if (targetState.getParentId() == null) {
        // root or orphaned node
        throw new ConstraintViolationException("cannot remove root node");
    }
    Path targetPath = hierMgr.getPath(targetState.getNodeId());
    NodeState parentState = getNodeState(parentId);
    Path parentPath = hierMgr.getPath(parentId);
    if ((options & CHECK_LOCK) == CHECK_LOCK) {
        // make sure there's no foreign lock on parent node
        verifyUnlocked(parentPath);
    }
    if ((options & CHECK_CHECKED_OUT) == CHECK_CHECKED_OUT) {
        // make sure parent node is checked-out
        verifyCheckedOut(parentPath);
    }
    if ((options & CHECK_ACCESS) == CHECK_ACCESS) {
        try {
            AccessManager accessMgr = context.getAccessManager();
            // make sure current session is granted read access on parent node
            if (!accessMgr.isGranted(targetPath, Permission.READ)) {
                throw new PathNotFoundException(safeGetJCRPath(targetPath));
            }
            // make sure current session is allowed to remove target node
            if (!accessMgr.isGranted(targetPath, Permission.REMOVE_NODE)) {
                throw new AccessDeniedException(safeGetJCRPath(targetPath) + ": not allowed to remove node");
            }
        } catch (ItemNotFoundException infe) {
            String msg = "internal error: failed to check access rights for " + safeGetJCRPath(targetPath);
            log.debug(msg);
            throw new RepositoryException(msg, infe);
        }
    }
    if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) {
        QItemDefinition parentDef = context.getItemManager().getDefinition(parentState).unwrap();
        if (parentDef.isProtected()) {
            throw new ConstraintViolationException(safeGetJCRPath(parentId) + ": cannot remove child node of protected parent node");
        }
        QItemDefinition targetDef = context.getItemManager().getDefinition(targetState).unwrap();
        if (targetDef.isMandatory()) {
            throw new ConstraintViolationException(safeGetJCRPath(targetPath) + ": cannot remove mandatory node");
        }
        if (targetDef.isProtected()) {
            throw new ConstraintViolationException(safeGetJCRPath(targetPath) + ": cannot remove protected node");
        }
    }
    if ((options & CHECK_REFERENCES) == CHECK_REFERENCES) {
        EffectiveNodeType ent = getEffectiveNodeType(targetState);
        if (ent.includesNodeType(NameConstants.MIX_REFERENCEABLE)) {
            NodeId targetId = targetState.getNodeId();
            if (stateMgr.hasNodeReferences(targetId)) {
                try {
                    NodeReferences refs = stateMgr.getNodeReferences(targetId);
                    if (refs.hasReferences()) {
                        throw new ReferentialIntegrityException(safeGetJCRPath(targetPath) + ": cannot remove node with references");
                    }
                } catch (ItemStateException ise) {
                    String msg = "internal error: failed to check references on " + safeGetJCRPath(targetPath);
                    log.error(msg, ise);
                    throw new RepositoryException(msg, ise);
                }
            }
        }
    }
    RetentionRegistry retentionReg = context.getSessionImpl().getRetentionRegistry();
    if ((options & CHECK_HOLD) == CHECK_HOLD) {
        if (retentionReg.hasEffectiveHold(targetPath, true)) {
            throw new RepositoryException("Unable to perform removal. Node is affected by a hold.");
        }
    }
    if ((options & CHECK_RETENTION) == CHECK_RETENTION) {
        if (retentionReg.hasEffectiveRetention(targetPath, true)) {
            throw new RepositoryException("Unable to perform removal. Node is affected by a retention.");
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) AccessManager(org.apache.jackrabbit.core.security.AccessManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeState(org.apache.jackrabbit.core.state.NodeState) RepositoryException(javax.jcr.RepositoryException) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) RetentionRegistry(org.apache.jackrabbit.core.retention.RetentionRegistry) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 9 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class DatabasePersistenceManager method loadReferencesTo.

/**
     * {@inheritDoc}
     */
public NodeReferences loadReferencesTo(NodeId targetId) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    synchronized (nodeReferenceSelectSQL) {
        ResultSet rs = null;
        InputStream in = null;
        try {
            Statement stmt = executeStmt(nodeReferenceSelectSQL, new Object[] { targetId.toString() });
            rs = stmt.getResultSet();
            if (!rs.next()) {
                throw new NoSuchItemStateException(targetId.toString());
            }
            in = rs.getBinaryStream(1);
            NodeReferences refs = new NodeReferences(targetId);
            Serializer.deserialize(refs, in);
            return refs;
        } catch (Exception e) {
            if (e instanceof NoSuchItemStateException) {
                throw (NoSuchItemStateException) e;
            }
            String msg = "failed to read node references: " + targetId;
            log.error(msg, e);
            throw new ItemStateException(msg, e);
        } finally {
            IOUtils.closeQuietly(in);
            closeResultSet(rs);
        }
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) SQLException(java.sql.SQLException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 10 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class AbstractBundlePersistenceManager method storeInternal.

/**
     * Stores the given changelog and updates the bundle cache.
     *
     * @param changeLog the changelog to store
     * @throws ItemStateException on failure
     */
private void storeInternal(ChangeLog changeLog) throws ItemStateException {
    // delete bundles
    HashSet<ItemId> deleted = new HashSet<ItemId>();
    for (ItemState state : changeLog.deletedStates()) {
        if (state.isNode()) {
            NodePropBundle bundle = getBundle((NodeId) state.getId());
            if (bundle == null) {
                throw new NoSuchItemStateException(state.getId().toString());
            }
            deleteBundle(bundle);
            deleted.add(state.getId());
        }
    }
    // gather added node states
    HashMap<ItemId, NodePropBundle> modified = new HashMap<ItemId, NodePropBundle>();
    for (ItemState state : changeLog.addedStates()) {
        if (state.isNode()) {
            NodePropBundle bundle = new NodePropBundle((NodeState) state);
            modified.put(state.getId(), bundle);
        }
    }
    // gather modified node states
    for (ItemState state : changeLog.modifiedStates()) {
        if (state.isNode()) {
            NodeId nodeId = (NodeId) state.getId();
            NodePropBundle bundle = modified.get(nodeId);
            if (bundle == null) {
                bundle = getBundle(nodeId);
                if (bundle == null) {
                    throw new NoSuchItemStateException(nodeId.toString());
                }
                modified.put(nodeId, bundle);
            }
            bundle.update((NodeState) state);
        } else {
            PropertyId id = (PropertyId) state.getId();
            // skip redundant primaryType and uuid properties
            if (id.getName().equals(JCR_PRIMARYTYPE) || id.getName().equals(JCR_UUID)) {
                continue;
            }
            NodeId nodeId = id.getParentId();
            NodePropBundle bundle = modified.get(nodeId);
            if (bundle == null) {
                bundle = getBundle(nodeId);
                if (bundle == null) {
                    throw new NoSuchItemStateException(nodeId.toString());
                }
                modified.put(nodeId, bundle);
            }
            bundle.addProperty((PropertyState) state, getBlobStore());
        }
    }
    // add removed properties
    for (ItemState state : changeLog.deletedStates()) {
        if (state.isNode()) {
            // check consistency
            NodeId parentId = state.getParentId();
            if (!modified.containsKey(parentId) && !deleted.contains(parentId)) {
                log.warn("Deleted node state's parent is not modified or deleted: " + parentId + "/" + state.getId());
            }
        } else {
            PropertyId id = (PropertyId) state.getId();
            NodeId nodeId = id.getParentId();
            if (!deleted.contains(nodeId)) {
                NodePropBundle bundle = modified.get(nodeId);
                if (bundle == null) {
                    // should actually not happen
                    log.warn("deleted property state's parent not modified!");
                    bundle = getBundle(nodeId);
                    if (bundle == null) {
                        throw new NoSuchItemStateException(nodeId.toString());
                    }
                    modified.put(nodeId, bundle);
                }
                bundle.removeProperty(id.getName(), getBlobStore());
            }
        }
    }
    // add added properties
    for (ItemState state : changeLog.addedStates()) {
        if (!state.isNode()) {
            PropertyId id = (PropertyId) state.getId();
            // skip primaryType and uuid properties
            if (id.getName().equals(JCR_PRIMARYTYPE) || id.getName().equals(JCR_UUID)) {
                continue;
            }
            NodeId nodeId = id.getParentId();
            NodePropBundle bundle = modified.get(nodeId);
            if (bundle == null) {
                // should actually not happen
                log.warn("added property state's parent not modified!");
                bundle = getBundle(nodeId);
                if (bundle == null) {
                    throw new NoSuchItemStateException(nodeId.toString());
                }
                modified.put(nodeId, bundle);
            }
            bundle.addProperty((PropertyState) state, getBlobStore());
        }
    }
    // now store all modified bundles
    long updateSize = 0;
    for (NodePropBundle bundle : modified.values()) {
        putBundle(bundle);
        updateSize += bundle.getSize();
    }
    changeLog.setUpdateSize(updateSize);
    // store the refs
    for (NodeReferences refs : changeLog.modifiedRefs()) {
        if (refs.hasReferences()) {
            store(refs);
        } else {
            destroy(refs);
        }
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ItemState(org.apache.jackrabbit.core.state.ItemState) NodePropBundle(org.apache.jackrabbit.core.persistence.util.NodePropBundle) NodeId(org.apache.jackrabbit.core.id.NodeId) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) ItemId(org.apache.jackrabbit.core.id.ItemId) HashSet(java.util.HashSet) PropertyId(org.apache.jackrabbit.core.id.PropertyId)

Aggregations

NodeReferences (org.apache.jackrabbit.core.state.NodeReferences)15 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)13 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)10 RepositoryException (javax.jcr.RepositoryException)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 PropertyId (org.apache.jackrabbit.core.id.PropertyId)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 NodeId (org.apache.jackrabbit.core.id.NodeId)4 SQLException (java.sql.SQLException)3 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)3 ChangeLog (org.apache.jackrabbit.core.state.ChangeLog)3 NodeState (org.apache.jackrabbit.core.state.NodeState)3 Name (org.apache.jackrabbit.spi.Name)3 FilterInputStream (java.io.FilterInputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ResultSet (java.sql.ResultSet)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)2 ReferentialIntegrityException (javax.jcr.ReferentialIntegrityException)2 PropertyState (org.apache.jackrabbit.core.state.PropertyState)2