eventstatecollection.java

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

JAVA
570
字号
                        Path.PathElement removedElem = Path.create(name, oldIndex).getNameElement();                        events.add(EventState.childNodeRemoved(n.getNodeId(),                                parentPath,                                child.getId(),                                removedElem,                                nodeType.getQName(),                                mixins,                                session));                        events.add(EventState.childNodeAdded(n.getNodeId(),                                parentPath,                                child.getId(),                                addedElem,                                nodeType.getQName(),                                mixins,                                session));                    }                }            } else {                // property changed                Path path = getPath(state.getId(), hmgr);                NodeState parent = (NodeState) stateMgr.getItemState(state.getParentId());                NodeTypeImpl nodeType = getNodeType(parent, session);                Set mixins = parent.getMixinTypeNames();                events.add(EventState.propertyChanged(state.getParentId(),                        getParent(path),                        path.getNameElement(),                        nodeType.getQName(),                        mixins,                        session));            }        }        // 2. removed items        for (Iterator it = changes.deletedStates(); it.hasNext();) {            ItemState state = (ItemState) it.next();            if (state.isNode()) {                // node deleted                NodeState n = (NodeState) state;                NodeState parent = (NodeState) stateMgr.getItemState(n.getParentId());                NodeTypeImpl nodeType = getNodeType(parent, session);                Set mixins = parent.getMixinTypeNames();                Path path = getZombiePath(state.getId(), hmgr);                events.add(EventState.childNodeRemoved(n.getParentId(),                        getParent(path),                        n.getNodeId(),                        path.getNameElement(),                        nodeType.getQName(),                        mixins,                        session));            } else {                // property removed                // only create an event if node still exists                try {                    NodeState n = (NodeState) changes.get(state.getParentId());                    // node state exists -> only property removed                    NodeTypeImpl nodeType = getNodeType(n, session);                    Set mixins = n.getMixinTypeNames();                    Path path = getZombiePath(state.getId(), hmgr);                    events.add(EventState.propertyRemoved(state.getParentId(),                            getParent(path),                            path.getNameElement(),                            nodeType.getQName(),                            mixins,                            session));                } catch (NoSuchItemStateException e) {                    // node removed as well -> do not create an event                }            }        }        // 3. added items        for (Iterator it = changes.addedStates(); it.hasNext();) {            ItemState state = (ItemState) it.next();            if (state.isNode()) {                // node created                NodeState n = (NodeState) state;                NodeId parentId = n.getParentId();                // the parent of an added item is always modified or new                NodeState parent = (NodeState) changes.get(parentId);                NodeTypeImpl nodeType = getNodeType(parent, session);                Set mixins = parent.getMixinTypeNames();                Path path = getPath(n.getNodeId(), hmgr);                events.add(EventState.childNodeAdded(parentId,                        getParent(path),                        n.getNodeId(),                        path.getNameElement(),                        nodeType.getQName(),                        mixins,                        session));            } else {                // property created / set                NodeState n = (NodeState) changes.get(state.getParentId());                NodeTypeImpl nodeType = getNodeType(n, session);                Set mixins = n.getMixinTypeNames();                Path path = getPath(state.getId(), hmgr);                events.add(EventState.propertyAdded(state.getParentId(),                        getParent(path),                        path.getNameElement(),                        nodeType.getQName(),                        mixins,                        session));            }        }    }    /**     * Adds all event states in the given collection to this collection     *     * @param c     */    public void addAll(Collection c) {        events.addAll(c);    }    /**     * Prepares already added events for dispatching.     */    public void prepare() {        dispatcher.prepareEvents(this);    }    /**     * Prepares deleted items from <code>changes</code>.     *     * @param changes the changes to prepare.     */    public void prepareDeleted(ChangeLog changes) {        dispatcher.prepareDeleted(this, changes);    }    /**     * Dispatches the events to the {@link javax.jcr.observation.EventListener}s.     */    public void dispatch() {        dispatcher.dispatchEvents(this);    }    /**     * Returns the path prefix for this event state collection or <code>null</code>     * if no path prefix was set in the constructor of this collection. See     * also {@link EventStateCollection#EventStateCollection}.     *     * @return the path prefix for this event state collection.     */    public Path getPathPrefix() {        return pathPrefix;    }    /**     * Returns an iterator over {@link EventState} instance.     *     * @return an iterator over {@link EventState} instance.     */    Iterator iterator() {        return events.iterator();    }    /**     * Return the list of events.     * @return list of events     */    public List getEvents() {        return Collections.unmodifiableList(events);    }    /**     * Return the session who is the origin of this events.     * @return event source     */    SessionImpl getSession() {        return session;    }    /**     * Resolves the node type name in <code>node</code> into a {@link NodeType}     * object using the {@link NodeTypeManager} of <code>session</code>.     *     * @param node    the node.     * @param session the session.     * @return the {@link NodeType} of <code>node</code>.     * @throws ItemStateException if the nodetype cannot be resolved.     */    private NodeTypeImpl getNodeType(NodeState node, SessionImpl session)            throws ItemStateException {        try {            return session.getNodeTypeManager().getNodeType(node.getNodeTypeName());        } catch (Exception e) {            // also catch eventual runtime exceptions here            // should never happen actually            String msg = "Item " + node.getNodeId() + " has unknown node type: " + node.getNodeTypeName();            log.error(msg);            throw new ItemStateException(msg, e);        }    }    /**     * Returns the path of the parent node of node at <code>path</code>..     *     * @param p the path.     * @return the parent path.     * @throws ItemStateException if <code>p</code> does not have a parent     *                            path. E.g. <code>p</code> designates root.     */    private Path getParent(Path p) throws ItemStateException {        try {            return p.getAncestor(1);        } catch (PathNotFoundException e) {            // should never happen actually            String msg = "Unable to resolve parent for path: " + p;            log.error(msg);            throw new ItemStateException(msg, e);        }    }    /**     * Resolves the path of the Item with id <code>itemId</code>.     *     * @param itemId the id of the item.     * @return the path of the item.     * @throws ItemStateException if the path cannot be resolved.     */    private Path getPath(ItemId itemId, HierarchyManager hmgr)            throws ItemStateException {        try {            return prefixPath(hmgr.getPath(itemId));        } catch (RepositoryException e) {            // should never happen actually            String msg = "Unable to resolve path for item: " + itemId;            log.error(msg);            throw new ItemStateException(msg, e);        }    }    /**     * Resolves the <i>zombie</i> (i.e. the old) path of the Item with id     * <code>itemId</code>.     *     * @param itemId the id of the item.     * @return the path of the item.     * @throws ItemStateException if the path cannot be resolved.     */    private Path getZombiePath(ItemId itemId, ChangeLogBasedHierarchyMgr hmgr)            throws ItemStateException {        try {            return prefixPath(hmgr.getZombiePath(itemId));        } catch (RepositoryException e) {            // should never happen actually            String msg = "Unable to resolve zombie path for item: " + itemId;            log.error(msg);            throw new ItemStateException(msg, e);        }    }    /**     * Prefixes the Path <code>p</code> with {@link #pathPrefix}.     *     * @param p the Path to prefix.     * @return the prefixed path or <code>p</code> itself if {@link #pathPrefix}     *         is <code>null</code>.     * @throws RepositoryException if the path cannot be prefixed.     */    private Path prefixPath(Path p) throws RepositoryException {        if (pathPrefix == null) {            return p;        }        Path.PathBuilder builder = new Path.PathBuilder(pathPrefix.getElements());        Path.PathElement[] elements = p.getElements();        for (int i = 0; i < elements.length; i++) {            if (elements[i].denotesRoot()) {                continue;            }            builder.addLast(elements[i]);        }        try {            return builder.getPath();        } catch (MalformedPathException e) {            throw new RepositoryException(e);        }    }}

⌨️ 快捷键说明

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