LiveCopyIndexImpl.java
12.5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.WCMException
* 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
* javax.jcr.query.QueryManager
* org.apache.commons.lang.StringUtils
* org.apache.jackrabbit.api.observation.JackrabbitEventFilter
* org.apache.jackrabbit.api.observation.JackrabbitObservationManager
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.SourceTargetFilter;
import com.day.cq.wcm.msm.impl.SourceTargetMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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 javax.jcr.query.QueryManager;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.api.observation.JackrabbitEventFilter;
import org.apache.jackrabbit.api.observation.JackrabbitObservationManager;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LiveCopyIndexImpl {
private static final Logger log = LoggerFactory.getLogger(LiveCopyIndexImpl.class);
private final Session session;
private final String rootPath;
private final String[] excludedPaths;
private final SourceTargetMap index;
private boolean indexedFully = false;
private EventListener nodeListener;
private EventListener propertyListener;
private boolean stopped;
/* varargs */ LiveCopyIndexImpl(Session session, String rootPath, String ... excludedPaths) throws RepositoryException {
Workspace wsp = session.getWorkspace();
ObservationManager om = wsp.getObservationManager();
QueryManager qm = wsp.getQueryManager();
if (qm == null) {
throw new IllegalArgumentException("Need QueryManager for Workspace");
}
if (om == null) {
throw new IllegalArgumentException("Need ObservationManager");
}
this.session = session;
this.rootPath = rootPath;
this.index = new SourceTargetMap();
this.excludedPaths = excludedPaths;
}
synchronized SourceTargetFilter get(String targetPath) throws RepositoryException, WCMException {
return this.update(targetPath);
}
synchronized SourceTargetFilter update(String path) throws WCMException, RepositoryException {
if (this.index.filterAt(path)) {
this.index.remove(path);
}
return this.read(path);
}
private SourceTargetFilter read(String targetPath) throws RepositoryException, WCMException {
SourceTargetFilter sourceTargetFilter = null;
this.session.refresh(false);
if (this.session.nodeExists(targetPath)) {
Node contentNode;
Node lcNode = this.session.getNode(targetPath);
if (lcNode.isNodeType("nt:hierarchyNode") && lcNode.hasNode("jcr:content")) {
contentNode = lcNode.getNode("jcr:content");
} else if (!lcNode.isNodeType("nt:hierarchyNode")) {
contentNode = lcNode;
} else {
return null;
}
if (LiveCopyManagerImpl.hasLiveCopy(contentNode)) {
Node configNode = contentNode.getNode("cq:LiveSyncConfig");
sourceTargetFilter = SourceTargetFilter.read(configNode);
this.index.addSourTargetFilter(sourceTargetFilter);
}
}
return sourceTargetFilter;
}
synchronized Set<String> getTargetLiveCopies(String sourcePath) throws RepositoryException, WCMException {
log.debug("Get target LiveCopies for {}", (Object)sourcePath);
this.buildIndex();
return this.index.getTargets(sourcePath);
}
synchronized SourceTargetFilter getClosestLiveCopy(String path) throws RepositoryException, WCMException {
if (this.indexedFully) {
return this.index.getContainingFilter(Text.getRelativeParent((String)path, (int)1));
}
String check = path;
while (!(check = Text.getRelativeParent((String)check, (int)1)).equals("/")) {
SourceTargetFilter parent = this.get(check);
if (parent == null) continue;
if (parent.contains(path)) {
log.debug("Found a LiveCopy for {} at {}", (Object)path, (Object)check);
return parent;
}
log.debug("Found a shallow LiveCopy for {} at {}: not contained in a LiveCopy", (Object)path, (Object)check);
return null;
}
return null;
}
synchronized List<String> getTargetBranch(String targetPath) throws WCMException, RepositoryException {
this.buildIndex();
return this.index.getTargetBranch(targetPath);
}
void setDirty() {
this.indexedFully = false;
}
void startListening() throws RepositoryException {
this.nodeListener = new EventListener(){
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onEvent(EventIterator events) {
HashSet<String> processed = new HashSet<String>();
String path = "";
block10 : do {
try {
Event event;
boolean isConfig;
do {
if (!events.hasNext()) break block10;
event = events.nextEvent();
path = event.getPath();
isConfig = false;
if (!path.endsWith("cq:LiveSyncConfig")) continue;
isConfig = true;
if (!StringUtils.isEmpty((String)(path = Text.getRelativeParent((String)path, (int)2)))) continue;
path = "/";
} while (processed.contains(path));
LiveCopyIndexImpl liveCopyIndexImpl = LiveCopyIndexImpl.this;
synchronized (liveCopyIndexImpl) {
processed.add(path);
switch (event.getType()) {
case 32: {
if (LiveCopyIndexImpl.this.index.descendantHasFilter(path)) {
LiveCopyIndexImpl.this.flushAll();
return;
}
processed.remove(path);
break;
}
case 2: {
if (!LiveCopyIndexImpl.this.index.descendantHasFilter(path)) break;
LiveCopyIndexImpl.this.updatePath(path);
break;
}
case 1: {
if (!LiveCopyIndexImpl.this.indexedFully) break;
if (!isConfig) {
String contentPath = event.getPath() + "/" + "jcr:content";
boolean bl = isConfig = LiveCopyIndexImpl.this.session.nodeExists(contentPath) && LiveCopyManagerImpl.hasLiveCopy(LiveCopyIndexImpl.this.session.getNode(contentPath));
}
if (!isConfig) break;
LiveCopyIndexImpl.this.get(path);
}
}
continue;
}
}
catch (Exception e) {
if (LiveCopyIndexImpl.this.stopped) {
log.debug("Failed to process Event at {}: {}: Event dispatched while unregistered", (Object)path, (Object)e);
break;
}
LiveCopyIndexImpl.this.flushAll();
log.error("Failed to process Event at {}: {}: clear Index", (Object)path, (Object)e);
break;
}
} while (true);
}
};
this.propertyListener = new EventListener(){
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onEvent(EventIterator events) {
HashSet<String> processed = new HashSet<String>();
String path = "";
try {
while (events.hasNext()) {
Event event = events.nextEvent();
path = Text.getRelativeParent((String)event.getPath(), (int)3);
if (StringUtils.isEmpty((String)path) || processed.contains(path)) continue;
LiveCopyIndexImpl liveCopyIndexImpl = LiveCopyIndexImpl.this;
synchronized (liveCopyIndexImpl) {
LiveCopyIndexImpl.this.update(path);
}
processed.add(path);
}
}
catch (Exception e) {
if (LiveCopyIndexImpl.this.stopped) {
log.debug("Failed to process Event at {}: {}: Event dispatched while unregistered", (Object)path, (Object)e);
}
LiveCopyIndexImpl.this.flushAll();
log.error("Failed to process Event at {}: {}: clear Index", (Object)path, (Object)e);
}
}
};
JackrabbitObservationManager om = (JackrabbitObservationManager)this.session.getWorkspace().getObservationManager();
JackrabbitEventFilter nodeListenerFilter = new JackrabbitEventFilter().setAbsPath(this.rootPath).setEventTypes(35).setIsDeep(true).setNoLocal(true);
if (this.excludedPaths != null && this.excludedPaths.length > 0) {
nodeListenerFilter.setExcludedPaths(this.excludedPaths);
}
om.addEventListener(this.nodeListener, nodeListenerFilter);
JackrabbitEventFilter propertyListenerFilter = new JackrabbitEventFilter().setAbsPath(this.rootPath).setEventTypes(28).setIsDeep(true).setNodeTypes(new String[]{"cq:LiveSyncConfig"}).setNoLocal(false);
if (this.excludedPaths != null && this.excludedPaths.length > 0) {
propertyListenerFilter.setExcludedPaths(this.excludedPaths);
}
om.addEventListener(this.propertyListener, propertyListenerFilter);
}
void endListening() throws RepositoryException {
ObservationManager om;
this.stopped = true;
if (this.session.isLive() && (om = this.session.getWorkspace().getObservationManager()) != null) {
if (this.nodeListener != null) {
om.removeEventListener(this.nodeListener);
}
if (this.propertyListener != null) {
om.removeEventListener(this.propertyListener);
}
}
}
private synchronized void flushAll() {
this.setDirty();
this.index.clear();
}
private synchronized void buildIndex() throws RepositoryException, WCMException {
if (!this.indexedFully) {
this.session.refresh(false);
for (SourceTargetFilter f : SourceTargetFilter.getAll(this.rootPath, this.session, new HashSet<SourceTargetFilter>())) {
this.index.addSourTargetFilter(f);
}
this.indexedFully = true;
}
}
private void updatePath(String path) throws RepositoryException, WCMException {
for (String des : this.index.getTargetBranch(path)) {
this.update(des);
}
}
void events(EventIterator events, int type) {
switch (type) {
case 1:
case 2:
case 32: {
this.nodeListener.onEvent(events);
break;
}
default: {
this.propertyListener.onEvent(events);
}
}
}
}