MSMEventProcessor.java
8.87 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* com.day.cq.wcm.api.PageEvent
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.PageModification$ModificationType
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.impl.BlueprintEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(propertyPrivate=1, name="event.topics", value={"com/day/cq/wcm/core/page", "com/day/cq/replication"}), @Property(name="event.filter", value={"(!(event.application=*))"})})
public class MSMEventProcessor
implements EventHandler {
private static final Logger log = LoggerFactory.getLogger(MSMEventProcessor.class);
@Reference
private LiveRelationshipManager liveRelationshipManager = null;
@Reference
private EventAdmin eventAdmin = null;
private ExecutorService executorService;
@Activate
public void activate(ComponentContext context) {
this.executorService = Executors.newSingleThreadExecutor();
}
@Deactivate
public void deactivate(ComponentContext context) throws InterruptedException {
this.executorService.shutdown();
try {
this.executorService.awaitTermination(15, TimeUnit.SECONDS);
}
catch (InterruptedException ie) {
log.error("Interrupted while waiting to shut-down; Cancel all tasks");
this.executorService.shutdownNow();
this.executorService.awaitTermination(15, TimeUnit.SECONDS);
Thread.currentThread().interrupt();
}
}
public void handleEvent(Event event) {
PageEvent pageEvent = PageEvent.fromEvent((Event)event);
if (pageEvent != null) {
this.executorService.execute(new PageEventBuilder(pageEvent, this.eventAdmin));
} else if (ReplicationAction.fromEvent((Event)event) != null) {
this.executorService.execute(new ReplicationActionBuilder(ReplicationAction.fromEvent((Event)event), this.eventAdmin));
}
}
private boolean filterBlueprint(String path) {
return this.liveRelationshipManager.isSource((Resource)new NonExistingResource(null, path));
}
PageEventBuilder createPageEventBuilder(PageEvent event) {
return new PageEventBuilder(event, this.eventAdmin);
}
ReplicationActionBuilder createReplicationActionBuilder(ReplicationAction action) {
return new ReplicationActionBuilder(action, this.eventAdmin);
}
protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.liveRelationshipManager = liveRelationshipManager;
}
protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.liveRelationshipManager == liveRelationshipManager) {
this.liveRelationshipManager = null;
}
}
protected void bindEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
protected void unbindEventAdmin(EventAdmin eventAdmin) {
if (this.eventAdmin == eventAdmin) {
this.eventAdmin = null;
}
}
class ReplicationActionBuilder
implements Runnable {
private final ReplicationAction replicationAction;
private final EventAdmin eventAdmin;
public ReplicationActionBuilder(ReplicationAction replicationAction, EventAdmin eventAdmin) {
this.replicationAction = replicationAction;
this.eventAdmin = eventAdmin;
}
@Override
public void run() {
if (this.filterType(this.replicationAction)) {
ArrayList<String> filteredPaths = new ArrayList<String>(this.replicationAction.getPaths().length);
for (String path : this.replicationAction.getPaths()) {
if (!MSMEventProcessor.this.filterBlueprint(path)) continue;
filteredPaths.add(path);
}
if (!filteredPaths.isEmpty()) {
BlueprintEvent bpEvent = BlueprintEvent.fromReplicationAction(new ReplicationAction(this.replicationAction.getType(), filteredPaths.toArray(new String[filteredPaths.size()]), this.replicationAction.getTime(), this.replicationAction.getUserId(), this.replicationAction.getRevision()));
this.eventAdmin.postEvent(bpEvent.toEvent(false));
} else {
log.debug("ReplicationAction {} paths not contained in a Blueprint", (Object)this.replicationAction);
}
} else {
log.debug("ReplicationAction {} is not of required Type", (Object)this.replicationAction);
}
}
private boolean filterType(ReplicationAction replicationAction) {
ReplicationActionType type = replicationAction.getType();
return type != ReplicationActionType.INTERNAL_POLL && type != ReplicationActionType.TEST;
}
}
class PageEventBuilder
implements Runnable {
private final PageEvent pageEvent;
private final EventAdmin eventAdmin;
public PageEventBuilder(PageEvent pageEvent, EventAdmin eventAdmin) {
this.pageEvent = pageEvent;
this.eventAdmin = eventAdmin;
}
@Override
public void run() {
ArrayList<PageModification> filteredEvent = new ArrayList<PageModification>();
Iterator itr = this.pageEvent.getModifications();
while (itr.hasNext()) {
PageModification modification = (PageModification)itr.next();
if (this.filterType(modification)) {
if (MSMEventProcessor.this.filterBlueprint(modification.getPath()) || modification.getDestination() != null && MSMEventProcessor.this.filterBlueprint(modification.getDestination())) {
filteredEvent.add(modification);
continue;
}
log.debug("MSM does only deal with Blueprint Resource {}: ignore", (Object)modification.getPath());
continue;
}
log.debug("MSM does not deal with {}: ignore", (Object)modification.getType().name());
}
if (!filteredEvent.isEmpty()) {
BlueprintEvent bpEvent = BlueprintEvent.fromPageEvent(new PageEvent(filteredEvent));
this.eventAdmin.sendEvent(bpEvent.toEvent(true));
this.eventAdmin.postEvent(bpEvent.toEvent(false));
} else {
log.debug("PageEvent {} did not contain any PageModification of required type in a Blueprint: ignore", (Object)this.pageEvent);
}
}
private boolean filterType(PageModification modification) {
PageModification.ModificationType type = modification.getType();
return !type.equals((Object)PageModification.ModificationType.VERSION_CREATED) && !type.equals((Object)PageModification.ModificationType.VALID) && !type.equals((Object)PageModification.ModificationType.INVALID);
}
}
}