sessionitemstatemanager.java

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

JAVA
968
字号
/* * 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.state;import org.apache.commons.collections.iterators.IteratorChain;import org.apache.jackrabbit.core.CachingHierarchyManager;import org.apache.jackrabbit.core.HierarchyManager;import org.apache.jackrabbit.core.ItemId;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.PropertyId;import org.apache.jackrabbit.core.ZombieHierarchyManager;import org.apache.jackrabbit.core.util.Dumpable;import org.apache.jackrabbit.name.PathResolver;import org.apache.jackrabbit.name.QName;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.PrintStream;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.List;import javax.jcr.InvalidItemStateException;import javax.jcr.ItemNotFoundException;import javax.jcr.ReferentialIntegrityException;import javax.jcr.RepositoryException;/** * Item state manager that handles both transient and persistent items. */public class SessionItemStateManager        implements UpdatableItemStateManager, Dumpable, NodeStateListener {    private static Logger log = LoggerFactory.getLogger(SessionItemStateManager.class);    /**     * State manager that allows updates     */    private final UpdatableItemStateManager stateMgr;    /**     * Hierarchy manager     */    private CachingHierarchyManager hierMgr;    /**     * map of those states that have been removed transiently     */    private final ItemStateStore atticStore;    /**     * map of new or modified transient states     */    private final ItemStateStore transientStore;    /**     * ItemStateManager view of the states in the attic; lazily instantiated     * in {@link #getAttic()}     */    private AtticItemStateManager attic;    /**     * State change dispatcher.     */    private final transient StateChangeDispatcher dispatcher = new StateChangeDispatcher();    /**     * Creates a new <code>SessionItemStateManager</code> instance.     *     * @param rootNodeId the root node id     * @param stateMgr the local item state manager     * @param resolver path resolver for outputting user-friendly paths     */    public SessionItemStateManager(NodeId rootNodeId,                                   LocalItemStateManager stateMgr,                                   PathResolver resolver) {        transientStore = new ItemStateMap();        atticStore = new ItemStateMap();        this.stateMgr = stateMgr;        stateMgr.addListener(this);        // create hierarchy manager that uses both transient and persistent state        hierMgr = new CachingHierarchyManager(rootNodeId, this, resolver);        addListener(hierMgr);    }    /**     * Returns the hierarchy manager     *     * @return the hierarchy manager     */    public HierarchyManager getHierarchyMgr() {        return hierMgr;    }    /**     * Returns an attic-aware hierarchy manager, i.e. an hierarchy manager that     * is also able to build/resolve paths of those items that have been moved     * or removed (i.e. moved to the attic).     *     * @return an attic-aware hierarchy manager     */    public HierarchyManager getAtticAwareHierarchyMgr() {        return new ZombieHierarchyManager(hierMgr, this, getAttic());    }    //-------------------------------------------------------------< Dumpable >    /**     * {@inheritDoc}     */    public void dump(PrintStream ps) {        ps.println("SessionItemStateManager (" + this + ")");        ps.println();        ps.print("[transient] ");        if (transientStore instanceof Dumpable) {            ((Dumpable) transientStore).dump(ps);        } else {            ps.println(transientStore.toString());        }        ps.println();        ps.print("[attic]     ");        if (atticStore instanceof Dumpable) {            ((Dumpable) atticStore).dump(ps);        } else {            ps.println(atticStore.toString());        }        ps.println();    }    //-----------------------------------------------------< ItemStateManager >    /**     * {@inheritDoc}     */    public ItemState getItemState(ItemId id)            throws NoSuchItemStateException, ItemStateException {        // first check if the specified item has been transiently removed        if (atticStore.contains(id)) {            /**             * check if there's new transient state for the specified item             * (e.g. if a property with name 'x' has been removed and a new             * property with same name has been created);             * this will throw a NoSuchItemStateException if there's no new             * transient state             */            return getTransientItemState(id);        }        // check if there's transient state for the specified item        if (transientStore.contains(id)) {            return getTransientItemState(id);        }        // check if there's persistent state for the specified item        if (stateMgr.hasItemState(id)) {            return stateMgr.getItemState(id);        }        throw new NoSuchItemStateException(id.toString());    }    /**     * {@inheritDoc}     */    public boolean hasItemState(ItemId id) {        // first check if the specified item has been transiently removed        if (atticStore.contains(id)) {            /**             * check if there's new transient state for the specified item             * (e.g. if a property with name 'x' has been removed and a new             * property with same name has been created);             */            return transientStore.contains(id);        }        // check if there's transient state for the specified item        if (transientStore.contains(id)) {            return true;        }        // check if there's persistent state for the specified item        return stateMgr.hasItemState(id);    }    /**     * {@inheritDoc}     */    public NodeReferences getNodeReferences(NodeReferencesId id)            throws NoSuchItemStateException, ItemStateException {        return stateMgr.getNodeReferences(id);    }    /**     * {@inheritDoc}     */    public boolean hasNodeReferences(NodeReferencesId id) {        return stateMgr.hasNodeReferences(id);    }    //--------------------------------------------< UpdatableItemStateManager >    /**     * {@inheritDoc}     */    public void edit() throws IllegalStateException {        stateMgr.edit();    }    /**     * {@inheritDoc}     */    public boolean inEditMode() {        return stateMgr.inEditMode();    }    /**     * {@inheritDoc}     */    public NodeState createNew(NodeId id, QName nodeTypeName,                               NodeId parentId)            throws IllegalStateException {        return stateMgr.createNew(id, nodeTypeName, parentId);    }    /**     * Customized variant of {@link #createNew(NodeId, QName, NodeId)} that     * connects the newly created persistent state with the transient state.     */    public NodeState createNew(NodeState transientState)            throws IllegalStateException {        NodeState persistentState = createNew(transientState.getNodeId(),                transientState.getNodeTypeName(),                transientState.getParentId());        transientState.connect(persistentState);        return persistentState;    }    /**     * {@inheritDoc}     */    public PropertyState createNew(QName propName, NodeId parentId)            throws IllegalStateException {        return stateMgr.createNew(propName, parentId);    }    /**     * Customized variant of {@link #createNew(QName, NodeId)} that     * connects the newly created persistent state with the transient state.     */    public PropertyState createNew(PropertyState transientState)            throws IllegalStateException {        PropertyState persistentState = createNew(transientState.getName(),                transientState.getParentId());        transientState.connect(persistentState);        return persistentState;    }    /**     * {@inheritDoc}     */    public void store(ItemState state) throws IllegalStateException {        stateMgr.store(state);    }    /**     * {@inheritDoc}     */    public void destroy(ItemState state) throws IllegalStateException {        stateMgr.destroy(state);    }    /**     * {@inheritDoc}     */    public void cancel() throws IllegalStateException {        stateMgr.cancel();    }    /**     * {@inheritDoc}     */    public void update()            throws ReferentialIntegrityException, StaleItemStateException,            ItemStateException, IllegalStateException {        stateMgr.update();    }    /**     * {@inheritDoc}     */    public void dispose() {        // discard all transient changes        disposeAllTransientItemStates();        // dispose our (i.e. 'local') state manager        stateMgr.dispose();    }    //< more methods for listing and retrieving transient ItemState instances >    /**     * @param id     * @return

⌨️ 快捷键说明

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