LiveRelationshipEventHandler.java
6.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.PageModification$ModificationType
* com.day.cq.wcm.api.WCMException
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.impl.BlueprintEvent;
import com.day.cq.wcm.msm.impl.LiveCopyIndexImpl;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.SourceTargetFilter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="event.topics", propertyPrivate=1, value={"com/day/cq/wcm/msm/blueprint/pageEvent/synchronous"})})
public class LiveRelationshipEventHandler
implements EventHandler {
private static final Logger log = LoggerFactory.getLogger(LiveRelationshipEventHandler.class);
@Reference
private LiveCopyServiceImpl liveCopyService = null;
@Reference
private ResourceResolverFactory resolverFactory = null;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void handleEvent(Event event) {
BlueprintEvent bpEvent = BlueprintEvent.fromEvent(event);
Iterator<PageModification> iterator = bpEvent.getModifications(PageModification.ModificationType.MOVED);
if (iterator.hasNext()) {
try {
ResourceResolver serviceResourceResolver = this.resolverFactory.getServiceResourceResolver(null);
LiveCopyManagerImpl liveCopyManager = this.liveCopyService.createLiveCopyManager(serviceResourceResolver);
TreeSet<String> done = new TreeSet<String>();
try {
Set<String> changed = null;
while (iterator.hasNext()) {
PageModification modification = iterator.next();
if (!this.filterMove(modification)) continue;
String sourcePath = modification.getPath();
boolean parentDone = false;
Iterator<String> ancestors = done.headSet(sourcePath).iterator();
while (!parentDone && ancestors.hasNext()) {
parentDone = Text.isDescendantOrEqual((String)ancestors.next(), (String)sourcePath);
}
if (parentDone) continue;
changed = liveCopyManager.moveDependentLiveCopies(sourcePath, modification.getDestination());
done.add(sourcePath);
}
if (serviceResourceResolver.adaptTo(Session.class) != null) {
Session session = (Session)serviceResourceResolver.adaptTo(Session.class);
if (changed != null) {
session.save();
LiveCopyIndexImpl index = this.liveCopyService.getLiveCopyIndex();
for (String path : changed) {
index.update(path);
}
}
}
}
catch (WCMException we) {
log.error("Error while processing Event {}", (Throwable)we);
}
catch (RepositoryException e) {
log.error("Error while processing Event {}", (Throwable)e);
}
finally {
if (serviceResourceResolver != null) {
serviceResourceResolver.close();
}
}
}
catch (LoginException e) {
log.error("Failed to access Administrative Resolver {}", (Throwable)e);
}
}
}
private boolean filterMove(PageModification modification) {
return modification.getType().equals((Object)PageModification.ModificationType.MOVED) && !modification.getPath().equals(modification.getDestination());
}
protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
this.liveCopyService = liveCopyServiceImpl;
}
protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
if (this.liveCopyService == liveCopyServiceImpl) {
this.liveCopyService = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
}