JcrObservationThrottle.java
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* 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;
}
}