Search in sources :

Example 36 with ChildNodeEntry

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

the class NodeImpl method getIndex.

/**
     * {@inheritDoc}
     */
public int getIndex() throws RepositoryException {
    // check state of this instance
    sanityCheck();
    NodeId parentId = getParentId();
    if (parentId == null) {
        // the root node cannot have same-name siblings; always return 1
        return 1;
    }
    try {
        NodeState parent = (NodeState) stateMgr.getItemState(parentId);
        ChildNodeEntry parentEntry = parent.getChildNodeEntry(getNodeId());
        return parentEntry.getIndex();
    } catch (ItemStateException ise) {
        // should never get here...
        String msg = "internal error: failed to determine index";
        log.error(msg, ise);
        throw new RepositoryException(msg, ise);
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 37 with ChildNodeEntry

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

the class NodeImpl method clone.

/**
     * Create a child node that is a clone of a shareable node.
     *
     * @param src shareable source node
     * @param name name of new node
     * @return child node
     * @throws ItemExistsException if there already is a child node with the
     *             name given and the definition does not allow creating another one
     * @throws VersionException if this node is not checked out
     * @throws ConstraintViolationException if no definition is found in this
     *             node that would allow creating the child node
     * @throws LockException if this node is locked
     * @throws RepositoryException if some other error occurs
     */
public synchronized NodeImpl clone(NodeImpl src, Name name) throws ItemExistsException, VersionException, ConstraintViolationException, LockException, RepositoryException {
    Path nodePath;
    try {
        nodePath = PathFactoryImpl.getInstance().create(getPrimaryPath(), name, true);
    } catch (MalformedPathException e) {
        // should never happen
        String msg = "internal error: invalid path " + this;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
    // (1) make sure that parent node is checked-out
    // (2) check lock status
    // (3) check protected flag of parent (i.e. this) node
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    // (4) check for name collisions
    NodeDefinitionImpl def;
    try {
        def = getApplicableChildNodeDefinition(name, null);
    } catch (RepositoryException re) {
        String msg = "no definition found in parent node's node type for new node";
        log.debug(msg);
        throw new ConstraintViolationException(msg, re);
    }
    NodeState thisState = data.getNodeState();
    ChildNodeEntry cne = thisState.getChildNodeEntry(name, 1);
    if (cne != null) {
        // check same-name sibling setting of new node
        if (!def.allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
        // check same-name sibling setting of existing node
        NodeId newId = cne.getId();
        if (!((NodeImpl) itemMgr.getItem(newId)).getDefinition().allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
    }
    // (5) do clone operation
    NodeId parentId = getNodeId();
    src.addShareParent(parentId);
    // (6) modify the state of 'this', i.e. the parent node
    NodeId srcId = src.getNodeId();
    thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, srcId);
    return itemMgr.getNode(srcId, parentId);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Example 38 with ChildNodeEntry

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

the class NodeImpl method orderBefore.

/**
     * Same as <code>{@link Node#orderBefore(String, String)}</code> except that
     * this method takes a <code>Path.Element</code> arguments instead of
     * <code>String</code>s.
     *
     * @param srcName
     * @param dstName
     * @throws UnsupportedRepositoryOperationException
     * @throws VersionException
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws LockException
     * @throws RepositoryException
     */
public synchronized void orderBefore(Path.Element srcName, Path.Element dstName) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
    // check state of this instance
    sanityCheck();
    if (!getPrimaryNodeType().hasOrderableChildNodes()) {
        throw new UnsupportedRepositoryOperationException("child node ordering not supported on " + this);
    }
    // check arguments
    if (srcName.equals(dstName)) {
        // there's nothing to do
        return;
    }
    // check existence
    if (!hasNode(srcName.getName(), srcName.getIndex())) {
        String name;
        try {
            Path.Element[] path = new Path.Element[] { srcName };
            name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
        } catch (NameException e) {
            name = srcName.toString();
        } catch (NamespaceException e) {
            name = srcName.toString();
        }
        throw new ItemNotFoundException(this + " has no child node with name " + name);
    }
    if (dstName != null && !hasNode(dstName.getName(), dstName.getIndex())) {
        String name;
        try {
            Path.Element[] path = new Path.Element[] { dstName };
            name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
        } catch (NameException e) {
            name = dstName.toString();
        } catch (NamespaceException e) {
            name = dstName.toString();
        }
        throw new ItemNotFoundException(this + " has no child node with name " + name);
    }
    // make sure this node is checked-out and neither protected nor locked
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    /*
        make sure the session is allowed to reorder child nodes.
        since there is no specific privilege for reordering child nodes,
        test if the the node to be reordered can be removed and added,
        i.e. treating reorder similar to a move.
        TODO: properly deal with sns in which case the index would change upon reorder.
        */
    AccessManager acMgr = sessionContext.getAccessManager();
    PathBuilder pb = new PathBuilder(getPrimaryPath());
    pb.addLast(srcName.getName(), srcName.getIndex());
    Path childPath = pb.getPath();
    if (!acMgr.isGranted(childPath, Permission.MODIFY_CHILD_NODE_COLLECTION)) {
        String msg = "Not allowed to reorder child node " + sessionContext.getJCRPath(childPath) + ".";
        log.debug(msg);
        throw new AccessDeniedException(msg);
    }
    ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(data.getNodeState().getChildNodeEntries());
    int srcInd = -1, destInd = -1;
    for (int i = 0; i < list.size(); i++) {
        ChildNodeEntry entry = list.get(i);
        if (srcInd == -1) {
            if (entry.getName().equals(srcName.getName()) && (entry.getIndex() == srcName.getIndex() || srcName.getIndex() == 0 && entry.getIndex() == 1)) {
                srcInd = i;
            }
        }
        if (destInd == -1 && dstName != null) {
            if (entry.getName().equals(dstName.getName()) && (entry.getIndex() == dstName.getIndex() || dstName.getIndex() == 0 && entry.getIndex() == 1)) {
                destInd = i;
                if (srcInd != -1) {
                    break;
                }
            }
        } else {
            if (srcInd != -1) {
                break;
            }
        }
    }
    // check if resulting order would be different to current order
    if (destInd == -1) {
        if (srcInd == list.size() - 1) {
            // no change, we're done
            return;
        }
    } else {
        if ((destInd - srcInd) == 1) {
            // no change, we're done
            return;
        }
    }
    // reorder list
    if (destInd == -1) {
        list.add(list.remove(srcInd));
    } else {
        if (srcInd < destInd) {
            list.add(destInd, list.get(srcInd));
            list.remove(srcInd);
        } else {
            list.add(destInd, list.remove(srcInd));
        }
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    thisState.setChildNodeEntries(list);
}
Also used : AccessManager(org.apache.jackrabbit.core.security.AccessManager) Path(org.apache.jackrabbit.spi.Path) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) AccessDeniedException(javax.jcr.AccessDeniedException) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 39 with ChildNodeEntry

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

the class NodeImpl method onRemove.

protected void onRemove(NodeId parentId) throws RepositoryException {
    // modify the state of 'this', i.e. the target node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // remove this node from its shared set
    if (thisState.isShareable()) {
        if (thisState.removeShare(parentId) > 0) {
            // this state is still connected to some parents, so
            // leave the child node entries and properties
            // set state of this instance to 'invalid'
            data.setStatus(STATUS_INVALIDATED);
            // notify the item manager that this instance has been
            // temporarily invalidated
            itemMgr.itemInvalidated(id, data);
            return;
        }
    }
    if (thisState.hasChildNodeEntries()) {
        // remove child nodes
        // use temp array to avoid ConcurrentModificationException
        ArrayList<ChildNodeEntry> tmp = new ArrayList<ChildNodeEntry>(thisState.getChildNodeEntries());
        // remove from tail to avoid problems with same-name siblings
        for (int i = tmp.size() - 1; i >= 0; i--) {
            ChildNodeEntry entry = tmp.get(i);
            // recursively remove child node
            NodeId childId = entry.getId();
            //NodeImpl childNode = (NodeImpl) itemMgr.getItem(childId);
            try {
                /* omit the read-permission check upon retrieving the
                       child item as this is an internal call to remove the
                       subtree which may contain (protected) child items which
                       are not visible to the caller of the removal. the actual
                       validation of the remove permission however is only
                       executed during Item.save(). 
                     */
                NodeImpl childNode = itemMgr.getNode(childId, getNodeId(), false);
                childNode.onRemove(thisState.getNodeId());
            // remove the child node entry
            } catch (ItemNotFoundException e) {
                boolean ignoreError = false;
                if (parentId != null && sessionContext.getSessionImpl().autoFixCorruptions()) {
                    // it might be an access right problem
                    // we need to check if the item doesn't exist in the ism
                    ItemStateManager ism = sessionContext.getItemStateManager();
                    if (!ism.hasItemState(childId)) {
                        log.warn("Child named " + entry.getName() + " (index " + entry.getIndex() + ", " + "node id " + childId + ") " + "not found when trying to remove " + getPath() + " " + "(node id " + getNodeId() + ") - ignored", e);
                        ignoreError = true;
                    }
                }
                if (!ignoreError) {
                    throw e;
                }
            }
            thisState.removeChildNodeEntry(childId);
        }
    }
    // remove properties
    // use temp set to avoid ConcurrentModificationException
    HashSet<Name> tmp = new HashSet<Name>(thisState.getPropertyNames());
    for (Name propName : tmp) {
        // remove the property entry
        thisState.removePropertyName(propName);
        // remove property
        PropertyId propId = new PropertyId(thisState.getNodeId(), propName);
        /* omit the read-permission check upon retrieving the
               child item as this is an internal call to remove the
               subtree which may contain (protected) child items which
               are not visible to the caller of the removal. the actual
               validation of the remove permission however is only
               executed during Item.save().
             */
        itemMgr.getItem(propId, false).setRemoved();
    }
    // finally remove this node
    thisState.setParentId(null);
    setRemoved();
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) NodeId(org.apache.jackrabbit.core.id.NodeId) ItemStateManager(org.apache.jackrabbit.core.state.ItemStateManager) ItemNotFoundException(javax.jcr.ItemNotFoundException) HashSet(java.util.HashSet)

Example 40 with ChildNodeEntry

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

the class XMLPersistenceManager method store.

/**
     * {@inheritDoc}
     */
protected void store(NodeState state) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    NodeId id = state.getNodeId();
    String nodeFilePath = buildNodeFilePath(id);
    FileSystemResource nodeFile = new FileSystemResource(itemStateFS, nodeFilePath);
    try {
        nodeFile.makeParentDirs();
        OutputStream os = nodeFile.getOutputStream();
        Writer writer = null;
        try {
            String encoding = DEFAULT_ENCODING;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(os, encoding));
            } catch (UnsupportedEncodingException e) {
                // should never get here!
                OutputStreamWriter osw = new OutputStreamWriter(os);
                encoding = osw.getEncoding();
                writer = new BufferedWriter(osw);
            }
            String parentId = (state.getParentId() == null) ? "" : state.getParentId().toString();
            String encodedNodeType = Text.encodeIllegalXMLCharacters(state.getNodeTypeName().toString());
            writer.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
            writer.write("<" + NODE_ELEMENT + " " + UUID_ATTRIBUTE + "=\"" + id + "\" " + PARENTUUID_ATTRIBUTE + "=\"" + parentId + "\" " + MODCOUNT_ATTRIBUTE + "=\"" + state.getModCount() + "\" " + NODETYPE_ATTRIBUTE + "=\"" + encodedNodeType + "\">\n");
            // mixin types
            writer.write("\t<" + MIXINTYPES_ELEMENT + ">\n");
            for (Name mixin : state.getMixinTypeNames()) {
                writer.write("\t\t<" + MIXINTYPE_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(mixin.toString()) + "\"/>\n");
            }
            writer.write("\t</" + MIXINTYPES_ELEMENT + ">\n");
            // properties
            writer.write("\t<" + PROPERTIES_ELEMENT + ">\n");
            for (Name propName : state.getPropertyNames()) {
                writer.write("\t\t<" + PROPERTY_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(propName.toString()) + "\">\n");
                // @todo serialize type, definition id and values
                writer.write("\t\t</" + PROPERTY_ELEMENT + ">\n");
            }
            writer.write("\t</" + PROPERTIES_ELEMENT + ">\n");
            // child nodes
            writer.write("\t<" + NODES_ELEMENT + ">\n");
            for (ChildNodeEntry entry : state.getChildNodeEntries()) {
                writer.write("\t\t<" + NODE_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(entry.getName().toString()) + "\" " + UUID_ATTRIBUTE + "=\"" + entry.getId() + "\">\n");
                writer.write("\t\t</" + NODE_ELEMENT + ">\n");
            }
            writer.write("\t</" + NODES_ELEMENT + ">\n");
            writer.write("</" + NODE_ELEMENT + ">\n");
        } finally {
            writer.close();
        }
    } catch (Exception e) {
        String msg = "failed to write node state: " + id;
        log.debug(msg);
        throw new ItemStateException(msg, e);
    }
}
Also used : OutputStream(java.io.OutputStream) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedWriter(java.io.BufferedWriter) Name(org.apache.jackrabbit.spi.Name) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) NodeId(org.apache.jackrabbit.core.id.NodeId) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Aggregations

ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)44 NodeState (org.apache.jackrabbit.core.state.NodeState)34 RepositoryException (javax.jcr.RepositoryException)25 NodeId (org.apache.jackrabbit.core.id.NodeId)23 Name (org.apache.jackrabbit.spi.Name)22 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)19 PropertyState (org.apache.jackrabbit.core.state.PropertyState)13 Path (org.apache.jackrabbit.spi.Path)12 ItemNotFoundException (javax.jcr.ItemNotFoundException)11 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)11 PropertyId (org.apache.jackrabbit.core.id.PropertyId)11 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)10 ArrayList (java.util.ArrayList)9 ItemExistsException (javax.jcr.ItemExistsException)9 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)9 InvalidItemStateException (javax.jcr.InvalidItemStateException)7 HashSet (java.util.HashSet)6 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)5 InternalValue (org.apache.jackrabbit.core.value.InternalValue)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5