internalversionhistoryimpl.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core.version;import org.apache.jackrabbit.core.NodeImpl;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.state.ItemStateException;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.name.QName;import org.apache.jackrabbit.uuid.UUID;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.jcr.PropertyType;import javax.jcr.ReferentialIntegrityException;import javax.jcr.RepositoryException;import javax.jcr.Value;import javax.jcr.version.VersionException;import java.util.Calendar;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Set;/** * Implements a <code>InternalVersionHistory</code> */class InternalVersionHistoryImpl extends InternalVersionItemImpl        implements InternalVersionHistory {    /**     * default logger     */    private static Logger log = LoggerFactory.getLogger(InternalVersionHistory.class);    /**     * the cache of the version labels     * key = version label (String)     * value = version     */    private HashMap labelCache = new HashMap();    /**     * the root version of this history     */    private InternalVersion rootVersion;    /**     * the hashmap of all versions     * key = versionId (NodeId)     * value = version     */    private HashMap versionCache = new HashMap();    /**     * Temporary version cache, used on a refresh.     */    private HashMap tempVersionCache = new HashMap();    /**     * the node that holds the label nodes     */    private NodeStateEx labelNode;    /**     * the id of this history     */    private NodeId historyId;    /**     * the id of the versionable node     */    private NodeId versionableId;    /**     * Creates a new VersionHistory object for the given node state.     */    public InternalVersionHistoryImpl(AbstractVersionManager vMgr, NodeStateEx node)            throws RepositoryException {        super(vMgr, node);        init();    }    /**     * Initialies the history and loads all internal caches     *     * @throws RepositoryException     */    private void init() throws RepositoryException {        versionCache.clear();        labelCache.clear();        // get id        historyId = node.getNodeId();        // get versionable id        versionableId = NodeId.valueOf(node.getPropertyValue(QName.JCR_VERSIONABLEUUID).toString());        // get entries        NodeStateEx[] children = node.getChildNodes();        for (int i = 0; i < children.length; i++) {            NodeStateEx child = children[i];            if (child.getName().equals(QName.JCR_VERSIONLABELS)) {                labelNode = child;                continue;            }            InternalVersionImpl v = createVersionInstance(child);            versionCache.put(v.getId(), v);            if (v.isRootVersion()) {                rootVersion = v;            }            vMgr.versionCreated(v);        }        // check for legacy version nodes that had 'virtual' jcr:successor property        if (rootVersion.getSuccessors().length==0 && versionCache.size()>1) {            // resolve successors and predecessors            Iterator iter = versionCache.values().iterator();            while (iter.hasNext()) {                InternalVersionImpl v = (InternalVersionImpl) iter.next();                v.legacyResolveSuccessors();            }        }        try {            // init label cache            PropertyState[] labels = labelNode.getProperties();            for (int i = 0; i < labels.length; i++) {                PropertyState pState = labels[i];                if (pState.getType() == PropertyType.REFERENCE) {                    QName name = pState.getName();                    UUID ref = (UUID) pState.getValues()[0].internalValue();                    InternalVersionImpl v = (InternalVersionImpl) getVersion(new NodeId(ref));                    if (v != null) {                        labelCache.put(name, v);                        v.internalAddLabel(name);                    } else {                        log.warn("Error while resolving label reference. Version missing: " + ref);                    }                }            }        } catch (ItemStateException e) {            throw new RepositoryException(e);        }    }    /**     * Reload this object and all its dependent version objects.     */    void reload() throws RepositoryException {        tempVersionCache.putAll(versionCache);        init();        // invalidate all versions that are not referenced any more        Iterator iter = tempVersionCache.values().iterator();        while (iter.hasNext()) {            InternalVersionImpl v = (InternalVersionImpl) iter.next();            v.invalidate();        }        tempVersionCache.clear();    }    /**     * Create a version instance. May resurrect versions temporarily swapped     * out when refreshing this history.     */    InternalVersionImpl createVersionInstance(NodeStateEx child) {        InternalVersionImpl v = (InternalVersionImpl) tempVersionCache.remove(child.getNodeId());        if (v != null) {            v.clear();        } else {            v = new InternalVersionImpl(this, child, child.getName());        }        return v;    }    /**     * {@inheritDoc}     */    public NodeId getId() {        return historyId;    }    /**     * {@inheritDoc}     */    public InternalVersionItem getParent() {        return null;    }    /**     * {@inheritDoc}     */    public InternalVersion getRootVersion() {        return rootVersion;    }    /**     * {@inheritDoc}     */    public InternalVersion getVersion(QName versionName) throws VersionException {        // maybe add cache by name?        Iterator iter = versionCache.values().iterator();        while (iter.hasNext()) {            InternalVersion v = (InternalVersion) iter.next();            if (v.getName().equals(versionName)) {                return v;            }        }        throw new VersionException("Version " + versionName + " does not exist.");    }    /**     * {@inheritDoc}     */    public boolean hasVersion(QName versionName) {        // maybe add cache?        Iterator iter = versionCache.values().iterator();        while (iter.hasNext()) {            InternalVersion v = (InternalVersion) iter.next();            if (v.getName().equals(versionName)) {                return true;            }        }        return false;    }    /**     * {@inheritDoc}     */    public boolean hasVersion(NodeId id) {        return versionCache.containsKey(id);    }    /**     * {@inheritDoc}     */    public InternalVersion getVersion(NodeId id) {        return (InternalVersion) versionCache.get(id);    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?