itemmanager.java

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

JAVA
781
字号
     * @throws AccessDeniedException     * @throws RepositoryException     */    synchronized NodeIterator getChildNodes(NodeId parentId)            throws ItemNotFoundException, AccessDeniedException, RepositoryException {        // check sanity of session        session.sanityCheck();        ItemState state = getItemState(parentId);        if (!state.isNode()) {            String msg = "can't list child nodes of property " + parentId;            log.debug(msg);            throw new RepositoryException(msg);        }        NodeState nodeState = (NodeState) state;        ArrayList childIds = new ArrayList();        Iterator iter = nodeState.getChildNodeEntries().iterator();        while (iter.hasNext()) {            NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) iter.next();            // check read access            if (session.getAccessManager().isGranted(entry.getId(), AccessManager.READ)) {                childIds.add(entry.getId());            }        }        return new LazyItemIterator(this, childIds);    }    /**     * @param parentId     * @return     * @throws ItemNotFoundException     * @throws AccessDeniedException     * @throws RepositoryException     */    synchronized boolean hasChildProperties(NodeId parentId)            throws ItemNotFoundException, AccessDeniedException, RepositoryException {        // check sanity of session        session.sanityCheck();        ItemState state = getItemState(parentId);        if (!state.isNode()) {            String msg = "can't list child properties of property " + parentId;            log.debug(msg);            throw new RepositoryException(msg);        }        NodeState nodeState = (NodeState) state;        Iterator iter = nodeState.getPropertyNames().iterator();        while (iter.hasNext()) {            QName propName = (QName) iter.next();            // check read access            if (session.getAccessManager().isGranted(new PropertyId(parentId, propName), AccessManager.READ)) {                return true;            }        }        return false;    }    /**     * @param parentId     * @return     * @throws ItemNotFoundException     * @throws AccessDeniedException     * @throws RepositoryException     */    synchronized PropertyIterator getChildProperties(NodeId parentId)            throws ItemNotFoundException, AccessDeniedException, RepositoryException {        // check sanity of session        session.sanityCheck();        ItemState state = getItemState(parentId);        if (!state.isNode()) {            String msg = "can't list child properties of property " + parentId;            log.debug(msg);            throw new RepositoryException(msg);        }        NodeState nodeState = (NodeState) state;        ArrayList childIds = new ArrayList();        Iterator iter = nodeState.getPropertyNames().iterator();        while (iter.hasNext()) {            QName propName = (QName) iter.next();            PropertyId id = new PropertyId(parentId, propName);            // check read access            if (session.getAccessManager().isGranted(id, AccessManager.READ)) {                childIds.add(id);            }        }        return new LazyItemIterator(this, childIds);    }    //-------------------------------------------------< item factory methods >    private ItemImpl createItemInstance(ItemId id)            throws ItemNotFoundException, RepositoryException {        // create instance of item using its state object        ItemImpl item;        ItemState state;        try {            state = itemStateProvider.getItemState(id);        } catch (NoSuchItemStateException nsise) {            throw new ItemNotFoundException(id.toString());        } catch (ItemStateException ise) {            String msg = "failed to retrieve item state of item " + id;            log.error(msg, ise);            throw new RepositoryException(msg, ise);        }        if (id.equals(rootNodeId)) {            // special handling required for root node            item = createNodeInstance((NodeState) state, rootNodeDef);        } else if (state.isNode()) {            item = createNodeInstance((NodeState) state);        } else {            item = createPropertyInstance((PropertyState) state);        }        return item;    }    NodeImpl createNodeInstance(NodeState state, NodeDefinition def)            throws RepositoryException {        NodeId id = state.getNodeId();        // we want to be informed on life cycle changes of the new node object        // in order to maintain item cache consistency        ItemLifeCycleListener[] listeners = new ItemLifeCycleListener[]{this};        // check special nodes        if (state.getNodeTypeName().equals(QName.NT_VERSION)) {            return createVersionInstance(id, state, def, listeners);        } else if (state.getNodeTypeName().equals(QName.NT_VERSIONHISTORY)) {            return createVersionHistoryInstance(id, state, def, listeners);        } else {            // create node object            return new NodeImpl(this, session, id, state, def, listeners);        }    }    NodeImpl createNodeInstance(NodeState state) throws RepositoryException {        // 1. get definition of the specified node        NodeDefinition def = getDefinition(state);        // 2. create instance        return createNodeInstance(state, def);    }    PropertyImpl createPropertyInstance(PropertyState state,                                        PropertyDefinition def) {        // we want to be informed on life cycle changes of the new property object        // in order to maintain item cache consistency        ItemLifeCycleListener[] listeners = new ItemLifeCycleListener[]{this};        // create property object        return new PropertyImpl(                this, session, state.getPropertyId(), state, def, listeners);    }    PropertyImpl createPropertyInstance(PropertyState state)            throws RepositoryException {        // 1. get definition for the specified property        PropertyDefinition def = getDefinition(state);        // 2. create instance        return createPropertyInstance(state, def);    }    /**     * Create a version instance.     * @param id node id     * @param state node state     * @param def node definition     * @param listeners listeners     * @return version instance     * @throws RepositoryException if an error occurs     */    protected VersionImpl createVersionInstance(            NodeId id, NodeState state, NodeDefinition def,            ItemLifeCycleListener[] listeners) throws RepositoryException {        return new VersionImpl(this, session, id, state, def, listeners);    }    /**     * Create a version history instance.     * @param id node id     * @param state node state     * @param def node definition     * @param listeners listeners     * @return version instance     * @throws RepositoryException if an error occurs     */    protected VersionHistoryImpl createVersionHistoryInstance(            NodeId id, NodeState state, NodeDefinition def,            ItemLifeCycleListener[] listeners) throws RepositoryException {        return new VersionHistoryImpl(this, session, id, state, def, listeners);    }    //---------------------------------------------------< item cache methods >    /**     * Returns an item reference from the cache.     *     * @param id id of the item that should be retrieved.     * @return the item reference stored in the corresponding cache entry     *         or <code>null</code> if there's no corresponding cache entry.     */    private ItemImpl retrieveItem(ItemId id) {        synchronized (itemCache) {            return (ItemImpl) itemCache.get(id);        }    }    /**     * Puts the reference of an item in the cache with     * the item's path as the key.     *     * @param item the item to cache     */    private void cacheItem(ItemImpl item) {        synchronized (itemCache) {            ItemId id = item.getId();            if (itemCache.containsKey(id)) {                log.warn("overwriting cached item " + id);            }            if (log.isDebugEnabled()) {                log.debug("caching item " + id);            }            itemCache.put(id, item);        }    }    /**     * Removes a cache entry for a specific item.     *     * @param id id of the item to remove from the cache     */    private void evictItem(ItemId id) {        if (log.isDebugEnabled()) {            log.debug("removing item " + id + " from cache");        }        synchronized (itemCache) {            itemCache.remove(id);        }    }    //-------------------------------------------------< misc. helper methods >    /**     * Failsafe conversion of internal <code>Path</code> to JCR path for use in     * error messages etc.     *     * @param path path to convert     * @return JCR path     */    String safeGetJCRPath(Path path) {        try {            return session.getJCRPath(path);        } catch (NamespaceException e) {            log.error("failed to convert " + path.toString() + " to JCR path.");            // return string representation of internal path as a fallback            return path.toString();        }    }    /**     * Failsafe translation of internal <code>ItemId</code> to JCR path for use in     * error messages etc.     *     * @param id path to convert     * @return JCR path     */    String safeGetJCRPath(ItemId id) {        try {            return safeGetJCRPath(hierMgr.getPath(id));        } catch (RepositoryException re) {            log.error(id + ": failed to determine path to");            // return string representation if id as a fallback            return id.toString();        }    }    //------------------------------------------------< ItemLifeCycleListener >    /**     * {@inheritDoc}     */    public void itemCreated(ItemImpl item) {        if (log.isDebugEnabled()) {            log.debug("created item " + item.getId());        }        // add instance to cache        cacheItem(item);    }    /**     * {@inheritDoc}     */    public void itemInvalidated(ItemId id, ItemImpl item) {        if (log.isDebugEnabled()) {            log.debug("invalidated item " + id);        }        // remove instance from cache        evictItem(id);    }    /**     * {@inheritDoc}     */    public void itemDestroyed(ItemId id, ItemImpl item) {        if (log.isDebugEnabled()) {            log.debug("destroyed item " + id);        }        // we're no longer interested in this item        item.removeLifeCycleListener(this);        // remove instance from cache        evictItem(id);    }    //-------------------------------------------------------------< Dumpable >    /**     * {@inheritDoc}     */    public synchronized void dump(PrintStream ps) {        ps.println("ItemManager (" + this + ")");        ps.println();        ps.println("Items in cache:");        ps.println();        synchronized (itemCache) {            Iterator iter = itemCache.keySet().iterator();            while (iter.hasNext()) {                ItemId id = (ItemId) iter.next();                ItemImpl item = (ItemImpl) itemCache.get(id);                if (item.isNode()) {                    ps.print("Node: ");                } else {                    ps.print("Property: ");                }                if (item.isTransient()) {                    ps.print("transient ");                } else {                    ps.print("          ");                }                ps.println(id + "\t" + item.safeGetJCRPath() + " (" + item + ")");            }        }    }    //----------------------------------------------------< ItemStateListener >    /**     * {@inheritDoc}     */    public void stateCreated(ItemState created) {        ItemImpl item = retrieveItem(created.getId());        if (item != null) {            item.stateCreated(created);        }    }    /**     * {@inheritDoc}     */    public void stateModified(ItemState modified) {        ItemImpl item = retrieveItem(modified.getId());        if (item != null) {            item.stateModified(modified);        }    }    /**     * {@inheritDoc}     */    public void stateDestroyed(ItemState destroyed) {        ItemImpl item = retrieveItem(destroyed.getId());        if (item != null) {            item.stateDestroyed(destroyed);        }    }    /**     * {@inheritDoc}     */    public void stateDiscarded(ItemState discarded) {        ItemImpl item = retrieveItem(discarded.getId());        if (item != null) {            item.stateDiscarded(discarded);        }    }}

⌨️ 快捷键说明

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