ReplicateAssetOnModify.java
14.3 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.Agent
* com.day.cq.replication.AgentConfig
* com.day.cq.replication.AgentFilter
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationOptions
* com.day.cq.replication.Replicator
* com.scene7.is.util.collections.CollectionUtil
* 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.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.jackrabbit.api.observation.JackrabbitEvent
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.JobManager
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
* org.apache.sling.jcr.api.SlingRepository
* org.jetbrains.annotations.NotNull
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.aod.replication;
import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentFilter;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.Replicator;
import com.scene7.is.util.collections.CollectionUtil;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.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.jackrabbit.api.observation.JackrabbitEvent;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.jcr.api.SlingRepository;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1, label="Adobe CQ Dynamic Media Replicate On Modify", description="Replicates modified assets through replication agents that have the On Modification trigger enabled")
@Service(value={JobConsumer.class})
@Properties(value={@Property(name="job.topics", value={"com/adobe/cq/dam/dmassetreplicateonmodify"}, propertyPrivate=1)})
public class ReplicateAssetOnModify
implements JobConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(ReplicateAssetOnModify.class);
private static final String[] LISTENER_PATHS = new String[]{"/content/dam", "/etc/dam"};
private static final String SUB_SERVICE_NAME = "dmreplicateonmodify";
private static final Pattern JCR_CONTENT = Pattern.compile("/jcr:content(/.*)?$");
private static final String LAST_INVALIDATE_CACHE = "dam:lastInvalidateCache";
private static final Set<String> IGNORED_PROPS = CollectionUtil.setOf((Object[])new String[]{"cq:lastPublished", "cq:lastPublishedBy", "cq:lastReplicated", "cq:lastReplicatedBy", "cq:lastReplicationAction", "dam:lastInvalidateCache"});
public static final String JOB_TOPIC = "com/adobe/cq/dam/dmassetreplicateonmodify";
public static final String JOB_PROP_LIST = "replication.list";
@Property(boolValue={0}, label="Enabled", description="Automatically replicate assets on modification")
public static final String PROP_ENABLED = "dmreplicateonmodify.enabled";
@Reference
private JobManager jobManager = null;
@Reference
private SlingRepository repository = null;
@Reference
private Replicator replicator = null;
private Session observationSession;
private ObservationManager observationManager;
private EventListener[] listeners;
private static final AgentFilter MODIFY_FILTER = new AgentFilter(){
public boolean isIncluded(Agent agent) {
return agent.getConfiguration().isTriggeredOnModification();
}
};
@Activate
protected void activate(ComponentContext ctx) {
try {
Dictionary props = ctx.getProperties();
boolean enabled = OsgiUtil.toBoolean(props.get("dmreplicateonmodify.enabled"), (boolean)true);
if (enabled) {
LOGGER.info("Activate Dynamic Media Replicate On Modify");
this.observationSession = this.repository.loginService("dmreplicateonmodify", null);
this.observationManager = this.observationSession.getWorkspace().getObservationManager();
this.listeners = this.registerEventListeners(LISTENER_PATHS);
}
}
catch (RepositoryException e) {
LOGGER.error("Unable to activate Dynamic Media replicate on modify", (Throwable)e);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deactivate
protected void deactivate() {
try {
if (this.observationManager != null) {
LOGGER.info("Deactivate Dynamic Media Replicate On Modify");
this.unregisterEventListeners(this.listeners);
}
}
catch (RepositoryException e) {
LOGGER.error("Error removing event listener", (Throwable)e);
}
finally {
if (this.observationSession != null) {
this.observationSession.logout();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public JobConsumer.JobResult process(Job job) {
try {
Session session = null;
try {
session = this.repository.loginService("dmreplicateonmodify", null);
List actions = (List)job.getProperty("replication.list");
for (ReplicationAction action : actions) {
try {
LOGGER.debug("Replicate {} {}", (Object)action.getType(), (Object)action.getPath());
ReplicationOptions options = new ReplicationOptions();
options.setFilter(MODIFY_FILTER);
this.replicator.replicate(session, action.getType(), action.getPath(), options);
continue;
}
catch (ReplicationException e) {
LOGGER.error("Failed to replicate " + action.getPath(), (Throwable)e);
JobConsumer.JobResult jobResult = JobConsumer.JobResult.FAILED;
if (session == null) return jobResult;
{
session.logout();
}
return jobResult;
}
}
return JobConsumer.JobResult.OK;
}
finally {
if (session != null) {
session.logout();
}
}
}
catch (RepositoryException e) {
LOGGER.error("Failed to login as a service", (Throwable)e);
return JobConsumer.JobResult.FAILED;
}
}
@NotNull
private EventListener[] registerEventListeners(String[] listenerPaths) throws RepositoryException {
EventListener[] listeners = new ReplicateOnModifyListener[listenerPaths.length];
for (int i = 0; i != listenerPaths.length; ++i) {
listeners[i] = new ReplicateOnModifyListener(this.jobManager);
this.observationManager.addEventListener(listeners[i], 31, listenerPaths[i], true, null, null, true);
}
return listeners;
}
private void unregisterEventListeners(EventListener[] listeners) throws RepositoryException {
for (EventListener listener : listeners) {
this.observationManager.removeEventListener(listener);
}
}
protected void bindJobManager(JobManager jobManager) {
this.jobManager = jobManager;
}
protected void unbindJobManager(JobManager jobManager) {
if (this.jobManager == jobManager) {
this.jobManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
private static class ReplicateOnModifyListener
implements EventListener {
private final JobManager jobManager;
public ReplicateOnModifyListener(JobManager jobManager) {
this.jobManager = jobManager;
}
public void onEvent(EventIterator eventIterator) {
try {
ArrayList<ReplicationAction> actions = new ArrayList<ReplicationAction>();
while (eventIterator.hasNext()) {
Event event = eventIterator.nextEvent();
String item = event.getPath();
LOGGER.trace("Trigger: {} {}", (Object)ReplicateOnModifyListener.getEventTypeStr(event.getType()), (Object)item);
if (ReplicateOnModifyListener.skipEvent(event)) {
LOGGER.trace("Skipped: {} {}", (Object)ReplicateOnModifyListener.getEventTypeStr(event.getType()), (Object)item);
continue;
}
String path = ReplicateOnModifyListener.getAssetPath(event);
boolean isAsset = path.equals(item);
ReplicationActionType type = ReplicateOnModifyListener.getReplicationActionType(event, isAsset);
ReplicationAction action = new ReplicationAction(type, path, event.getDate(), event.getUserID(), null);
int last = actions.size() - 1;
if (last >= 0 && ((ReplicationAction)actions.get(last)).getPath().equals(path)) {
if (!isAsset) continue;
actions.set(last, action);
continue;
}
actions.add(action);
}
if (!actions.isEmpty()) {
HashMap<String, ArrayList<ReplicationAction>> props = new HashMap<String, ArrayList<ReplicationAction>>();
props.put("replication.list", actions);
this.jobManager.addJob("com/adobe/cq/dam/dmassetreplicateonmodify", props);
}
}
catch (RepositoryException e) {
LOGGER.error("Error processing JCR event", (Throwable)e);
}
}
private static boolean skipEvent(Event event) throws RepositoryException {
JackrabbitEvent je;
String item = event.getPath();
if (item == null) {
return true;
}
if (event instanceof JackrabbitEvent && (je = (JackrabbitEvent)event).isExternal()) {
return true;
}
int i = item.lastIndexOf(47);
if (ReplicateOnModifyListener.isProperty(event.getType()) && i >= 0) {
String property = item.substring(i + 1);
return IGNORED_PROPS.contains(property);
}
return false;
}
private static String getEventTypeStr(int event) {
switch (event) {
case 1: {
return "NODE_ADDED";
}
case 2: {
return "NODE_REMOVED";
}
case 32: {
return "NODE_MOVED";
}
case 4: {
return "PROPERTY_ADDED";
}
case 8: {
return "PROPERTY_REMOVED";
}
case 16: {
return "PROPERTY_CHANGED";
}
case 64: {
return "PERSIST";
}
}
return "(UNKNOWN)";
}
private static ReplicationActionType getReplicationActionType(Event event, boolean isAsset) {
if (isAsset && event.getType() == 2) {
return ReplicationActionType.DELETE;
}
return ReplicationActionType.ACTIVATE;
}
private static String getAssetPath(Event event) throws RepositoryException {
int i;
String item = event.getPath();
boolean isProperty = ReplicateOnModifyListener.isProperty(event.getType());
if (isProperty && (i = item.lastIndexOf(47)) >= 0) {
item = item.substring(0, i);
}
if (item.endsWith("imageserver/configuration/jcr:content/settings")) {
return item;
}
return JCR_CONTENT.matcher(item).replaceFirst("");
}
private static boolean isProperty(int i) {
return i == 4 || i == 16 || i == 8;
}
}
}