Search in sources :

Example 71 with ItemStateException

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

the class BatchedItemOperations method copyNodeState.

/**
     * Recursively copies the specified node state including its properties and
     * child nodes.
     *
     * @param srcState
     * @param srcPath
     * @param srcStateMgr
     * @param srcAccessMgr
     * @param destParentId
     * @param flag           one of
     *                       <ul>
     *                       <li><code>COPY</code></li>
     *                       <li><code>CLONE</code></li>
     *                       <li><code>CLONE_REMOVE_EXISTING</code></li>
     *                       </ul>
     * @param refTracker     tracks uuid mappings and processed reference properties
     * @return a deep copy of the given node state and its children
     * @throws RepositoryException if an error occurs
     */
private NodeState copyNodeState(NodeState srcState, Path srcPath, ItemStateManager srcStateMgr, AccessManager srcAccessMgr, NodeId destParentId, int flag, ReferenceChangeTracker refTracker) throws RepositoryException {
    NodeState newState;
    try {
        NodeId id = null;
        EffectiveNodeType ent = getEffectiveNodeType(srcState);
        boolean referenceable = ent.includesNodeType(NameConstants.MIX_REFERENCEABLE);
        boolean versionable = ent.includesNodeType(NameConstants.MIX_SIMPLE_VERSIONABLE);
        boolean fullVersionable = ent.includesNodeType(NameConstants.MIX_VERSIONABLE);
        boolean shareable = ent.includesNodeType(NameConstants.MIX_SHAREABLE);
        switch(flag) {
            case COPY:
                /* if this node is shareable and another node in the same shared set
                     * has been already been copied and given a new uuid, use this one
                     * (see section 14.5 of the specification)
                     */
                if (shareable && refTracker.getMappedId(srcState.getNodeId()) != null) {
                    NodeId newId = refTracker.getMappedId(srcState.getNodeId());
                    NodeState sharedState = (NodeState) stateMgr.getItemState(newId);
                    sharedState.addShare(destParentId);
                    return sharedState;
                }
                break;
            case CLONE:
                if (!referenceable) {
                    // non-referenceable node: always create new node id
                    break;
                }
                // use same uuid as source node
                id = srcState.getNodeId();
                if (stateMgr.hasItemState(id)) {
                    if (shareable) {
                        NodeState sharedState = (NodeState) stateMgr.getItemState(id);
                        sharedState.addShare(destParentId);
                        return sharedState;
                    }
                    // node with this uuid already exists
                    throw new ItemExistsException(safeGetJCRPath(id));
                }
                break;
            case CLONE_REMOVE_EXISTING:
                if (!referenceable) {
                    // non-referenceable node: always create new node id
                    break;
                }
                // use same uuid as source node
                id = srcState.getNodeId();
                if (stateMgr.hasItemState(id)) {
                    NodeState existingState = (NodeState) stateMgr.getItemState(id);
                    // or an ancestor thereof
                    if (id.equals(destParentId) || hierMgr.isAncestor(id, destParentId)) {
                        String msg = "cannot remove node " + safeGetJCRPath(srcPath) + " because it is an ancestor of the destination";
                        log.debug(msg);
                        throw new RepositoryException(msg);
                    }
                    // check if existing can be removed
                    // (access rights, locking & versioning status,
                    // node type constraints and retention/hold)
                    checkRemoveNode(existingState, CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
                    // do remove existing
                    removeNodeState(existingState);
                }
                break;
            default:
                throw new IllegalArgumentException("unknown flag for copying node state: " + flag);
        }
        newState = stateMgr.createNew(id, srcState.getNodeTypeName(), destParentId);
        id = newState.getNodeId();
        if (flag == COPY && referenceable) {
            // remember uuid mapping
            refTracker.mappedId(srcState.getNodeId(), id);
        }
        // copy node state
        newState.setMixinTypeNames(srcState.getMixinTypeNames());
        if (shareable) {
            // initialize shared set
            newState.addShare(destParentId);
        }
        // copy child nodes
        for (ChildNodeEntry entry : srcState.getChildNodeEntries()) {
            Path srcChildPath = PathFactoryImpl.getInstance().create(srcPath, entry.getName(), true);
            if (!srcAccessMgr.isGranted(srcChildPath, Permission.READ)) {
                continue;
            }
            NodeId nodeId = entry.getId();
            NodeState srcChildState = (NodeState) srcStateMgr.getItemState(nodeId);
            /**
                 * If child is shareble and its UUID has already been remapped,
                 * then simply add a reference to the state with that remapped
                 * UUID instead of copying the whole subtree.
                 */
            if (srcChildState.isShareable()) {
                NodeId mappedId = refTracker.getMappedId(srcChildState.getNodeId());
                if (mappedId != null) {
                    if (stateMgr.hasItemState(mappedId)) {
                        NodeState destState = (NodeState) stateMgr.getItemState(mappedId);
                        if (!destState.isShareable()) {
                            String msg = "Remapped child (" + safeGetJCRPath(srcPath) + ") is not shareable.";
                            throw new ItemStateException(msg);
                        }
                        if (!destState.addShare(id)) {
                            String msg = "Unable to add share to node: " + id;
                            throw new ItemStateException(msg);
                        }
                        stateMgr.store(destState);
                        newState.addChildNodeEntry(entry.getName(), mappedId);
                        continue;
                    }
                }
            }
            // recursive copying of child node
            NodeState newChildState = copyNodeState(srcChildState, srcChildPath, srcStateMgr, srcAccessMgr, id, flag, refTracker);
            // store new child node
            stateMgr.store(newChildState);
            // add new child node entry to new node
            newState.addChildNodeEntry(entry.getName(), newChildState.getNodeId());
        }
        // init version history if needed
        VersionHistoryInfo history = null;
        if (versionable && flag == COPY) {
            NodeId copiedFrom = null;
            if (fullVersionable) {
                // base version of copied versionable node is reference value of
                // the histories jcr:copiedFrom property
                PropertyId propId = new PropertyId(srcState.getNodeId(), NameConstants.JCR_BASEVERSION);
                PropertyState prop = (PropertyState) srcStateMgr.getItemState(propId);
                copiedFrom = prop.getValues()[0].getNodeId();
            }
            InternalVersionManager manager = session.getInternalVersionManager();
            history = manager.getVersionHistory(session, newState, copiedFrom);
        }
        // copy properties
        for (Name propName : srcState.getPropertyNames()) {
            Path propPath = PathFactoryImpl.getInstance().create(srcPath, propName, true);
            PropertyId propId = new PropertyId(srcState.getNodeId(), propName);
            if (!srcAccessMgr.canRead(propPath, propId)) {
                continue;
            }
            PropertyState srcChildState = (PropertyState) srcStateMgr.getItemState(propId);
            /**
                 * special handling required for properties with special semantics
                 * (e.g. those defined by mix:referenceable, mix:versionable,
                 * mix:lockable, et.al.)
                 *
                 * todo FIXME delegate to 'node type instance handler'
                 */
            QPropertyDefinition def = ent.getApplicablePropertyDef(srcChildState.getName(), srcChildState.getType(), srcChildState.isMultiValued());
            if (NameConstants.MIX_LOCKABLE.equals(def.getDeclaringNodeType())) {
                // skip properties defined by mix:lockable
                continue;
            }
            PropertyState newChildState = copyPropertyState(srcChildState, id, propName, def);
            if (history != null) {
                if (fullVersionable) {
                    if (propName.equals(NameConstants.JCR_VERSIONHISTORY)) {
                        // jcr:versionHistory
                        InternalValue value = InternalValue.create(history.getVersionHistoryId());
                        newChildState.setValues(new InternalValue[] { value });
                    } else if (propName.equals(NameConstants.JCR_BASEVERSION) || propName.equals(NameConstants.JCR_PREDECESSORS)) {
                        // jcr:baseVersion or jcr:predecessors
                        InternalValue value = InternalValue.create(history.getRootVersionId());
                        newChildState.setValues(new InternalValue[] { value });
                    } else if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
                        // jcr:isCheckedOut
                        newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
                    }
                } else {
                    // version history when we see the jcr:isCheckedOut
                    if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
                        // jcr:isCheckedOut
                        newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
                    }
                }
            }
            if (newChildState.getType() == PropertyType.REFERENCE || newChildState.getType() == PropertyType.WEAKREFERENCE) {
                refTracker.processedReference(newChildState);
            }
            // store new property
            stateMgr.store(newChildState);
            // add new property entry to new node
            newState.addPropertyName(propName);
        }
        return newState;
    } catch (ItemStateException ise) {
        String msg = "internal error: failed to copy state of " + srcState.getNodeId();
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) VersionHistoryInfo(org.apache.jackrabbit.core.version.VersionHistoryInfo) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) RepositoryException(javax.jcr.RepositoryException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) NodeId(org.apache.jackrabbit.core.id.NodeId) InternalVersionManager(org.apache.jackrabbit.core.version.InternalVersionManager)

Example 72 with ItemStateException

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

the class CachingHierarchyManager method resolvePath.

//-------------------------------------------------< base class overrides >
/**
     * {@inheritDoc}
     */
protected ItemId resolvePath(Path path, int typesAllowed) throws RepositoryException {
    Path pathToNode = path;
    if ((typesAllowed & RETURN_NODE) == 0) {
        // if we must not return a node, pass parent path
        // (since we only cache nodes)
        pathToNode = path.getAncestor(1);
    }
    PathMap.Element<LRUEntry> element = map(pathToNode);
    if (element == null) {
        // not even intermediate match: call base class
        return super.resolvePath(path, typesAllowed);
    }
    LRUEntry entry = element.get();
    if (element.hasPath(path)) {
        // exact match: return answer
        synchronized (cacheMonitor) {
            entry.touch();
        }
        return entry.getId();
    }
    Path.Element[] elements = path.getElements();
    try {
        return resolvePath(elements, element.getDepth() + 1, entry.getId(), typesAllowed);
    } catch (ItemStateException e) {
        String msg = "failed to retrieve state of intermediary node for entry: " + entry.getId() + ", path: " + path.getString();
        logItemStateException(msg, e);
        log.debug(msg);
        // probably stale cache entry -> evict
        evictAll(entry.getId(), true);
    }
    // JCR-3617: fall back to super class in case of ItemStateException
    return super.resolvePath(path, typesAllowed);
}
Also used : Path(org.apache.jackrabbit.spi.Path) PathMap(org.apache.jackrabbit.spi.commons.name.PathMap) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 73 with ItemStateException

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

the class MultiIndex method createIndex.

/**
     * Recursively creates an index starting with the NodeState
     * <code>node</code>.
     *
     * @param node     the current NodeState.
     * @param path     the path of the current <code>node</code> state.
     * @param stateMgr the shared item state manager.
     * @param count    the number of nodes already indexed.
     * @return the number of nodes indexed so far.
     * @throws IOException         if an error occurs while writing to the
     *                             index.
     * @throws ItemStateException  if an node state cannot be found.
     * @throws RepositoryException if any other error occurs
     */
private long createIndex(NodeState node, Path path, ItemStateManager stateMgr, long count) throws IOException, ItemStateException, RepositoryException {
    NodeId id = node.getNodeId();
    if (excludedIDs.contains(id)) {
        return count;
    }
    executeAndLog(new AddNode(getTransactionId(), id));
    if (++count % 100 == 0) {
        PathResolver resolver = new DefaultNamePathResolver(handler.getContext().getNamespaceRegistry());
        log.info("indexing... {} ({})", resolver.getJCRPath(path), count);
    }
    if (count % 10 == 0) {
        checkIndexingQueue(true);
    }
    checkVolatileCommit();
    for (ChildNodeEntry child : node.getChildNodeEntries()) {
        Path childPath = PATH_FACTORY.create(path, child.getName(), child.getIndex(), false);
        NodeState childState = null;
        try {
            childState = (NodeState) stateMgr.getItemState(child.getId());
        } catch (NoSuchItemStateException e) {
            handler.getOnWorkspaceInconsistencyHandler().handleMissingChildNode(e, handler, path, node, child);
        } catch (ItemStateException e) {
            // JCR-3268 log bundle corruption and continue
            handler.getOnWorkspaceInconsistencyHandler().logError(e, handler, childPath, node, child);
        }
        if (childState != null) {
            count = createIndex(childState, childPath, stateMgr, count);
        }
    }
    return count;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.core.state.NodeState) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeId(org.apache.jackrabbit.core.id.NodeId) PathResolver(org.apache.jackrabbit.spi.commons.conversion.PathResolver) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 74 with ItemStateException

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

the class NodeIndexer method createDoc.

/**
     * Creates a lucene Document.
     *
     * @return the lucene Document with the index layout.
     * @throws RepositoryException if an error occurs while reading property
     *                             values from the <code>ItemStateProvider</code>.
     */
public Document createDoc() throws RepositoryException {
    doNotUseInExcerpt.clear();
    Document doc = new Document();
    doc.setBoost(getNodeBoost());
    // special fields
    // UUID
    doc.add(new IDField(node.getNodeId()));
    try {
        // parent UUID
        if (node.getParentId() == null) {
            // root node
            Field parent = new Field(FieldNames.PARENT, false, "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO);
            parent.setIndexOptions(FieldInfo.IndexOptions.DOCS_ONLY);
            doc.add(parent);
            addNodeName(doc, "", "");
        } else if (node.getSharedSet().isEmpty()) {
            addParentChildRelation(doc, node.getParentId());
        } else {
            // shareable node
            for (NodeId id : node.getSharedSet()) {
                addParentChildRelation(doc, id);
            }
            // mark shareable nodes
            doc.add(new Field(FieldNames.SHAREABLE_NODE, false, "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
        }
    } catch (NoSuchItemStateException e) {
        throwRepositoryException(e);
    } catch (ItemStateException e) {
        throwRepositoryException(e);
    } catch (NamespaceException e) {
    // will never happen, because this.mappings will dynamically add
    // unknown uri<->prefix mappings
    }
    Set<Name> props = node.getPropertyNames();
    for (Name propName : props) {
        if (isIndexed(propName)) {
            PropertyId id = new PropertyId(node.getNodeId(), propName);
            try {
                PropertyState propState = (PropertyState) stateProvider.getItemState(id);
                // beginning with V2
                if (indexFormatVersion.getVersion() >= IndexFormatVersion.V2.getVersion()) {
                    addPropertyName(doc, propState.getName());
                }
                InternalValue[] values = propState.getValues();
                for (InternalValue value : values) {
                    addValue(doc, value, propState.getName());
                }
                if (values.length > 1) {
                    // real multi-valued
                    addMVPName(doc, propState.getName());
                }
            } catch (NoSuchItemStateException e) {
                throwRepositoryException(e);
            } catch (ItemStateException e) {
                throwRepositoryException(e);
            }
        }
    }
    // now add fields that are not used in excerpt (must go at the end)
    for (Fieldable field : doNotUseInExcerpt) {
        doc.add(field);
    }
    return doc;
}
Also used : Document(org.apache.lucene.document.Document) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Field(org.apache.lucene.document.Field) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) Fieldable(org.apache.lucene.document.Fieldable) NodeId(org.apache.jackrabbit.core.id.NodeId) NamespaceException(javax.jcr.NamespaceException)

Example 75 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit-oak by apache.

the class BundleLoader method loadBundle.

NodePropBundle loadBundle(NodeId id) throws ItemStateException {
    if (loadBundle != null) {
        try {
            return checkNotNull((NodePropBundle) loadBundle.invoke(pm, id), "Could not load NodePropBundle for id [%s]", id);
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof ItemStateException) {
                throw (ItemStateException) e.getCause();
            }
        // fall through
        } catch (IllegalArgumentException e) {
        // fall through
        } catch (IllegalAccessException e) {
        // fall through
        }
    }
    NodeState state = pm.load(id);
    checkNotNull(state, "Could not load NodeState for id [%s]", id);
    NodePropBundle bundle = new NodePropBundle(state);
    for (Name name : state.getPropertyNames()) {
        if (NameConstants.JCR_UUID.equals(name)) {
            bundle.setReferenceable(true);
        } else if (!NameConstants.JCR_PRIMARYTYPE.equals(name) && !NameConstants.JCR_MIXINTYPES.equals(name)) {
            bundle.addProperty(new PropertyEntry(pm.load(new PropertyId(id, name))));
        }
    }
    return bundle;
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) PropertyEntry(org.apache.jackrabbit.core.persistence.util.NodePropBundle.PropertyEntry) NodePropBundle(org.apache.jackrabbit.core.persistence.util.NodePropBundle) InvocationTargetException(java.lang.reflect.InvocationTargetException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId)

Aggregations

ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)141 RepositoryException (javax.jcr.RepositoryException)87 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)86 NodeId (org.apache.jackrabbit.core.id.NodeId)44 NodeState (org.apache.jackrabbit.core.state.NodeState)39 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)31 IOException (java.io.IOException)28 InvalidItemStateException (javax.jcr.InvalidItemStateException)27 PropertyState (org.apache.jackrabbit.core.state.PropertyState)24 SQLException (java.sql.SQLException)23 PropertyId (org.apache.jackrabbit.core.id.PropertyId)22 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)19 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)19 Name (org.apache.jackrabbit.spi.Name)17 ItemNotFoundException (javax.jcr.ItemNotFoundException)15 InternalValue (org.apache.jackrabbit.core.value.InternalValue)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 NodeReferences (org.apache.jackrabbit.core.state.NodeReferences)13 InputStream (java.io.InputStream)12