Search in sources :

Example 26 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)

Example 27 with ChildNodeEntry

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

the class ConsistencyCheck method getPath.

/**
     * Returns the path for <code>node</code>. If an error occurs this method
     * returns the uuid of the node.
     *
     * @param node the node to retrieve the path from
     * @return the path of the node or its uuid.
     */
private String getPath(NodeState node) {
    // remember as fallback
    String uuid = node.getNodeId().toString();
    StringBuilder path = new StringBuilder();
    List<ChildNodeEntry> elements = new ArrayList<ChildNodeEntry>();
    try {
        while (node.getParentId() != null) {
            NodeId parentId = node.getParentId();
            NodeState parent = (NodeState) stateMgr.getItemState(parentId);
            ChildNodeEntry entry = parent.getChildNodeEntry(node.getNodeId());
            if (entry == null) {
                log.warn("Failed to build path: abandoned child {} of node {}. " + "Please run a repository consistency check", node.getNodeId(), parentId);
                return uuid;
            }
            elements.add(entry);
            node = parent;
        }
        for (int i = elements.size() - 1; i > -1; i--) {
            ChildNodeEntry entry = elements.get(i);
            path.append('/').append(entry.getName().getLocalName());
            if (entry.getIndex() > 1) {
                path.append('[').append(entry.getIndex()).append(']');
            }
        }
        if (path.length() == 0) {
            path.append('/');
        }
        return path.toString();
    } catch (ItemStateException e) {
        return uuid;
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) NodeId(org.apache.jackrabbit.core.id.NodeId) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 28 with ChildNodeEntry

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

the class InternalVersionManagerBase method getVersionHistoryInfoForNode.

/**
     * Returns information about the version history of the specified node
     * or <code>null</code> when unavailable.
     *
     * @param node node whose version history should be returned
     * @return identifiers of the version history and root version nodes
     * @throws RepositoryException if an error occurs
     */
public VersionHistoryInfo getVersionHistoryInfoForNode(NodeState node) throws RepositoryException {
    VersionHistoryInfo info = null;
    VersioningLock.ReadLock lock = acquireReadLock();
    try {
        String uuid = node.getNodeId().toString();
        Name name = getName(uuid);
        NodeStateEx parent = getParentNode(getHistoryRoot(), uuid, null);
        if (parent != null && parent.hasNode(name)) {
            NodeStateEx history = parent.getNode(name, 1);
            if (history == null) {
                throw new InconsistentVersioningState("Unexpected failure to get child node " + name + " on parent node " + parent.getNodeId());
            }
            ChildNodeEntry rootv = history.getState().getChildNodeEntry(JCR_ROOTVERSION, 1);
            if (rootv == null) {
                throw new InconsistentVersioningState("missing child node entry for " + JCR_ROOTVERSION + " on version history node " + history.getNodeId(), history.getNodeId(), null);
            }
            info = new VersionHistoryInfo(history.getNodeId(), rootv.getId());
        }
    } finally {
        lock.release();
    }
    return info;
}
Also used : ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) Name(org.apache.jackrabbit.spi.Name)

Example 29 with ChildNodeEntry

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

the class InternalVersionHistoryImpl method init.

/**
     * Initializes the history and loads all internal caches
     *
     * @throws RepositoryException if an error occurs
     */
private synchronized void init() throws RepositoryException {
    nameCache.clear();
    versionCache.clear();
    labelCache.clear();
    // get id
    historyId = node.getNodeId();
    // get versionable id
    versionableId = NodeId.valueOf(node.getPropertyValue(NameConstants.JCR_VERSIONABLEUUID).toString());
    // get label node
    labelNode = node.getNode(NameConstants.JCR_VERSIONLABELS, 1);
    // init label cache
    try {
        PropertyState[] labels = labelNode.getProperties();
        for (PropertyState pState : labels) {
            if (pState.getType() == PropertyType.REFERENCE) {
                Name labelName = pState.getName();
                NodeId id = pState.getValues()[0].getNodeId();
                if (node.getState().hasChildNodeEntry(id)) {
                    labelCache.put(labelName, node.getState().getChildNodeEntry(id).getName());
                } else {
                    log.warn("Error while resolving label reference. Version missing: " + id);
                }
            }
        }
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    }
    // get root version
    rootVersion = createVersionInstance(NameConstants.JCR_ROOTVERSION);
    // get version entries
    for (ChildNodeEntry child : node.getState().getChildNodeEntries()) {
        if (child.getName().equals(NameConstants.JCR_VERSIONLABELS)) {
            continue;
        }
        nameCache.put(child.getName(), child.getId());
    }
}
Also used : ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Name(org.apache.jackrabbit.spi.Name) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 30 with ChildNodeEntry

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

the class NodeImpl method getPrimaryPath.

/**
     * {@inheritDoc}
     *
     * Overridden to return a different path for shareable nodes.
     *
     * TODO SN: copies functionality in that is already available in
     *          HierarchyManagerImpl, namely composing a path by
     *          concatenating the parent path + this node's name and index:
     *          rather use hierarchy manager to do this
     */
@Override
public Path getPrimaryPath() throws RepositoryException {
    if (!isShareable()) {
        return super.getPrimaryPath();
    }
    NodeId parentId = getParentId();
    NodeImpl parentNode = (NodeImpl) getParent();
    Path parentPath = parentNode.getPrimaryPath();
    PathBuilder builder = new PathBuilder(parentPath);
    ChildNodeEntry entry = parentNode.getNodeState().getChildNodeEntry(getNodeId());
    if (entry == null) {
        String msg = "failed to build path of " + id + ": " + parentId + " has no child entry for " + id;
        log.debug(msg);
        throw new ItemNotFoundException(msg);
    }
    // add to path
    if (entry.getIndex() == 1) {
        builder.addLast(entry.getName());
    } else {
        builder.addLast(entry.getName(), entry.getIndex());
    }
    return builder.getPath();
}
Also used : Path(org.apache.jackrabbit.spi.Path) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeId(org.apache.jackrabbit.core.id.NodeId) ItemNotFoundException(javax.jcr.ItemNotFoundException)

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