⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subscriptionimpl.java

📁 jsr170接口的java实现。是个apache的开源项目。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // clear list        eventBundles.clear();        return ed;    }    /**     * Creates a new transaction listener for the scope of a transaction     * commit (save call).     * @return a transaction listener for this subscription.     */    TransactionListener createTransactionListener() {        if (info.isNoLocal()) {            // a subscription which is not interested in local changes does            // not need the transaction id            return new TransactionEvent() {                public void onEvent(EventIterator events) {                    // ignore                }                public void beforeCommit(TransactionResource resource, String lockToken) {                    // ignore                }                public void afterCommit(TransactionResource resource,                                        String lockToken,                                        boolean success) {                    // ignore                }            };        } else {            return new TransactionEvent();        }    }    /**     * Suspend this subscription. This call will remove this subscription as     * event listener from the observation manager.     */    void suspend() throws DavException {        try {            obsMgr.removeEventListener(this);        } catch (RepositoryException e) {            throw new JcrDavException(e);        }    }    /**     * Resumes this subscription. This call will register this subscription     * again as event listener to the observation manager.     */    void resume() throws DavException {        try {            obsMgr.addEventListener(this, getJcrEventTypes(),                    getLocator().getRepositoryPath(), isDeep(), getUuidFilters(),                    getNodetypeNameFilters(), isNoLocal());        } catch (RepositoryException e) {            throw new JcrDavException(e);        }    }    //--------------------------------------------< EventListener interface >---    /**     * Records the events passed as a new event bundle in order to make them     * available with the next {@link #discoverEvents(long)} request. If this     * subscription is expired it will remove itself as listener from the     * observation manager.     *     * @param events to be recorded.     * @see EventListener#onEvent(EventIterator)     * @see #discoverEvents(long)     */    public synchronized void onEvent(EventIterator events) {        if (!isExpired()) {            eventBundles.add(new EventBundleImpl(events));        } else {            // expired -> unsubscribe            try {                obsMgr.removeEventListener(this);            } catch (RepositoryException e) {                log.warn("Exception while unsubscribing: " + e);            }        }        notifyAll();    }    //--------------------------------------------------------------------------    /**     * Static utility method to convert the type defined by a     * {@link javax.jcr.observation.Event JCR event} into an <code>EventType</code>     * object.     *     * @param jcrEventType     * @return <code>EventType</code> representation of the given JCR event type.     * @throws IllegalArgumentException if the given int does not represent a     * valid type constants as defined by {@link Event}.<br>     * Valid values are     * <ul>     * <li>{@link Event#NODE_ADDED}</li>     * <li>{@link Event#NODE_REMOVED}</li>     * <li>{@link Event#PROPERTY_ADDED}</li>     * <li>{@link Event#PROPERTY_REMOVED}</li>     * <li>{@link Event#PROPERTY_CHANGED}</li>     * </ul>     */    public static EventType getEventType(int jcrEventType) {        String localName;        switch (jcrEventType) {            case Event.NODE_ADDED:                localName = EVENT_NODEADDED;                break;            case Event.NODE_REMOVED:                localName = EVENT_NODEREMOVED;                break;            case Event.PROPERTY_ADDED:                localName = EVENT_PROPERTYADDED;                break;            case Event.PROPERTY_CHANGED:                localName = EVENT_PROPERTYCHANGED;                break;            case Event.PROPERTY_REMOVED:                localName = EVENT_PROPERTYREMOVED;                break;            default: // no default                throw new IllegalArgumentException("Invalid JCR event type: " + jcrEventType);        }        return DefaultEventType.create(localName, NAMESPACE);    }    /**     * Static utility method to convert an <code>EventType</code> as present in     * the Xml body into the corresponding JCR event constant defined by     * {@link javax.jcr.observation.Event}.     *     * @param eventType     * @return Any of the event types defined by {@link Event}.<br>     * Possible values are     * <ul>     * <li>{@link Event#NODE_ADDED}</li>     * <li>{@link Event#NODE_REMOVED}</li>     * <li>{@link Event#PROPERTY_ADDED}</li>     * <li>{@link Event#PROPERTY_REMOVED}</li>     * <li>{@link Event#PROPERTY_CHANGED}</li>     * </ul>     * @throws DavException if the given event type does not define a valid     * JCR event type, such as returned by {@link #getEventType(int)}.     */    public static int getJcrEventType(EventType eventType) throws DavException {        if (eventType == null || !NAMESPACE.equals(eventType.getNamespace())) {            throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, "Invalid JCR event type: "+ eventType + ": Namespace mismatch.");        }        int eType;        String eventName = eventType.getName();        if (EVENT_NODEADDED.equals(eventName)) {            eType = Event.NODE_ADDED;        } else if (EVENT_NODEREMOVED.equals(eventName)) {            eType = Event.NODE_REMOVED;        } else if (EVENT_PROPERTYADDED.equals(eventName)) {            eType = Event.PROPERTY_ADDED;        } else if (EVENT_PROPERTYCHANGED.equals(eventName)) {            eType = Event.PROPERTY_CHANGED;        } else if (EVENT_PROPERTYREMOVED.equals(eventName)) {            eType = Event.PROPERTY_REMOVED;        } else {            throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, "Invalid event type: "+eventName);        }        return eType;    }    /**     * Inner class <code>EventBundle</code> encapsulats an event bundle as     * recorded {@link SubscriptionImpl#onEvent(EventIterator) on event} and     * provides the possibility to retrieve the Xml representation of the     * bundle and the events included in order to respond to a POLL request.     *     * @see SubscriptionImpl#discoverEvents(long)     */    private class EventBundleImpl implements EventBundle {        private final EventIterator events;        private final String transactionId;        private EventBundleImpl(EventIterator events) {            this(events, null);        }        private EventBundleImpl(EventIterator events, String transactionId) {            this.events = events;            this.transactionId = transactionId;        }        public Element toXml(Document document) {            Element bundle = DomUtil.createElement(document, XML_EVENTBUNDLE, NAMESPACE);            if (transactionId != null) {                DomUtil.setAttribute(bundle, XML_EVENT_TRANSACTION_ID, NAMESPACE, transactionId);            }            while (events.hasNext()) {                Event event = events.nextEvent();                Element eventElem = DomUtil.addChildElement(bundle, XML_EVENT, NAMESPACE);                // href                String eHref = "";                try {                    boolean isCollection = (event.getType() == Event.NODE_ADDED || event.getType() == Event.NODE_REMOVED);                    eHref = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), event.getPath(), false).getHref(isCollection);                } catch (RepositoryException e) {                    // should not occur....                    log.error(e.getMessage());                }                eventElem.appendChild(DomUtil.hrefToXml(eHref, document));                // eventtype                Element eType = DomUtil.addChildElement(eventElem, XML_EVENTTYPE, NAMESPACE);                eType.appendChild(getEventType(event.getType()).toXml(document));                // user id                DomUtil.addChildElement(eventElem, XML_EVENTUSERID, NAMESPACE, event.getUserID());            }            return bundle;        }    }    //----------------------------< TransactionEvent >------------------------    /**     * Implements a transaction event which listenes for events during a save     * call on the repository.     */    private class TransactionEvent implements EventListener, TransactionListener {        private String transactionId;        /**         * {@inheritDoc}         */        public void onEvent(EventIterator events) {            String tId = transactionId;            if (tId == null) {                tId = UUID.randomUUID().toString();            }            synchronized (SubscriptionImpl.this) {                eventBundles.add(new EventBundleImpl(events, tId));                SubscriptionImpl.this.notifyAll();            }        }        //-----------------------------< TransactionListener >------------------        /**         * {@inheritDoc}         */        public void beforeCommit(TransactionResource resource, String lockToken) {            try {                transactionId = lockToken;                obsMgr.addEventListener(this, getJcrEventTypes(),                        getLocator().getRepositoryPath(), isDeep(), getUuidFilters(),                        getNodetypeNameFilters(), isNoLocal());                // suspend the subscription                suspend();            } catch (RepositoryException e) {                log.warn("Unable to register TransactionListener: " + e);            } catch (DavException e) {                log.warn("Unable to register TransactionListener: " + e);            }        }        /**         * {@inheritDoc}         */        public void afterCommit(TransactionResource resource,                                String lockToken,                                boolean success) {            try {                // resume the subscription                resume();                // remove this transaction event                obsMgr.removeEventListener(this);            } catch (RepositoryException e) {                log.warn("Unable to remove listener: " + e);            } catch (DavException e) {                log.warn("Unable to resume Subscription: " + e);            }        }    }}

⌨️ 快捷键说明

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