JcrObservationThrottle.java 4.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.observation.Event
 *  javax.jcr.observation.EventIterator
 *  javax.jcr.observation.EventListener
 *  javax.jcr.observation.ObservationManager
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.commons.jcr;

import com.day.cq.commons.jcr.JcrUtil;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JcrObservationThrottle
implements EventListener {
    private final Node tempFolder;
    private Node tempNode;
    private final Logger log;
    private int nodeCounter;
    private int lastCounterSeen;

    public JcrObservationThrottle(Node tempNodeFolder) {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.nodeCounter = 0;
        this.tempFolder = tempNodeFolder;
    }

    public void open() throws RepositoryException {
        this.tempNode = JcrUtil.createUniqueNode(this.tempFolder, this.getClass().getSimpleName(), "nt:unstructured", this.tempFolder.getSession());
        boolean eventTypes = true;
        boolean isDeep = true;
        boolean noLocal = false;
        this.tempFolder.getSession().getWorkspace().getObservationManager().addEventListener((EventListener)this, 1, this.tempNode.getPath(), true, null, null, false);
        this.log.debug("Temporary node {} created, observation setup", (Object)this.tempNode.getPath());
    }

    public void close() {
        try {
            this.tempFolder.getSession().getWorkspace().getObservationManager().removeEventListener((EventListener)this);
            this.tempFolder.remove();
            this.tempFolder.getSession().save();
        }
        catch (RepositoryException re) {
            this.log.warn("RepositoryException in close()", (Throwable)re);
        }
        this.tempNode = null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void onEvent(EventIterator events) {
        while (events.hasNext()) {
            Event e = events.nextEvent();
            try {
                String[] path = e.getPath().split("/");
                String last = path[path.length - 1];
                try {
                    int i = Integer.valueOf(last);
                    JcrObservationThrottle jcrObservationThrottle = this;
                    synchronized (jcrObservationThrottle) {
                        this.lastCounterSeen = i;
                        this.notify();
                    }
                    this.log.debug("Got event {}, notified", (Object)this.lastCounterSeen);
                    continue;
                }
                catch (NumberFormatException ignore) {
                }
            }
            catch (Exception ex) {
                this.log.warn("Exception in onEvent", (Throwable)ex);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public long waitForEvents() throws RepositoryException {
        long start = System.currentTimeMillis();
        int targetCounter = ++this.nodeCounter;
        Node added = this.tempNode.addNode(String.valueOf(targetCounter), "nt:unstructured");
        this.tempNode.getSession().save();
        this.log.debug("Waiting for observation events on {}", (Object)added.getPath());
        try {
            while (this.lastCounterSeen < targetCounter) {
                JcrObservationThrottle jcrObservationThrottle = this;
                synchronized (jcrObservationThrottle) {
                    this.wait();
                    continue;
                }
            }
            this.log.debug("Got observation event {}", (Object)this.lastCounterSeen);
        }
        catch (InterruptedException e) {
            this.log.warn("InterruptedException in waitForEvents", (Throwable)e);
        }
        return System.currentTimeMillis() - start;
    }
}