WatchFolderConfigRepositoryObserver.java
20.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.PropertyType
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.nodetype.NodeType
* 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.Reference
* org.apache.jackrabbit.api.observation.JackrabbitEvent
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemfd.watchfolder.activation;
import com.adobe.aemfd.watchfolder.config.WatchFolderConfigCache;
import com.adobe.aemfd.watchfolder.config.WatchFolderConfiguration;
import com.adobe.aemfd.watchfolder.factory.ActivationCallbackFactory;
import com.adobe.aemfd.watchfolder.factory.ConfigurationHandlerFactory;
import com.adobe.aemfd.watchfolder.factory.InputProcessorFactory;
import com.adobe.aemfd.watchfolder.job.ScanScheduler;
import com.adobe.aemfd.watchfolder.util.JcrUtil;
import com.adobe.aemfd.watchfolder.util.SessionUtil;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
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.Reference;
import org.apache.jackrabbit.api.observation.JackrabbitEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component(immediate=1)
public class WatchFolderConfigRepositoryObserver
implements EventListener {
private static final Logger logger = LoggerFactory.getLogger(WatchFolderConfigRepositoryObserver.class);
private static final String CONFIG_SUB_DIR = "config";
private static final String WF_CONFIG_ROOT = "/etc/fd/watchfolder/config";
private static final Map<String, String> MANDATORY_PROP_MAP = new HashMap<String, String>();
private static final String PARAM_PREFIX = "param.";
@Reference
private ActivationCallbackFactory activationCallbackFactory;
@Reference
private InputProcessorFactory inputProcessorFactory;
@Reference
private ConfigurationHandlerFactory configurationHandlerFactory;
@Reference
private WatchFolderConfigCache watchFolderConfigCache;
@Reference
private ScanScheduler scanScheduler;
@Reference
private SessionUtil sessionUtil;
private Session theSession;
private Object getValue(String wfId, Property p) throws Exception {
try {
return JcrUtil.getPropertyValue(p, false, true);
}
catch (Exception e) {
logger.warn("Error getting singleton value of property " + p.getName() + " for watch-folder " + wfId + " [error = '" + e + "'], skipping...");
return null;
}
}
private void setProperty(String wfId, WatchFolderConfiguration config, Property p) throws Exception {
String pName = p.getName();
if ("jcr:primaryType".equals(pName)) {
logger.info("Skipping primary-type property for watch-folder " + wfId);
return;
}
Object value = this.getValue(wfId, p);
if (value == null) {
logger.warn("Empty/non-singleton value or value of unsupported type for property " + pName + " of watch-folder " + wfId + ", skipping...");
return;
}
try {
String setter = "set" + Character.toUpperCase(pName.charAt(0));
if (pName.length() > 1) {
setter = setter + pName.substring(1);
}
Method m = config.getClass().getMethod(setter, value.getClass());
m.invoke(config, value);
logger.info("Successfully set property " + pName + " on watch-folder config " + wfId + " to " + JcrUtil.toString(value));
}
catch (Exception e) {
logger.warn("Error setting property " + pName + " for watch-folder config " + wfId + " to value " + JcrUtil.toString(value) + " [error = '" + e + "'], skipping...");
}
}
private void setConfigParam(String wfId, WatchFolderConfiguration config, Property p) throws Exception {
String paramName = p.getName().substring("param.".length());
try {
Object paramVal = JcrUtil.getPropertyValue(p, null, false);
config.setParam(paramName, paramVal);
logger.info("Successfully set config param " + paramName + " for watch-folder " + wfId + " to " + JcrUtil.toString(paramVal));
}
catch (Exception e) {
logger.warn("Error setting config param " + paramName + " for watch-folder " + wfId + " [error = '" + e + "'], skipping...");
}
}
private WatchFolderConfiguration toConfig(Node n) throws Exception {
String wfId = n.getName();
String nt = n.getPrimaryNodeType().getName();
if (!"nt:unstructured".equals(nt)) {
logger.warn("Unknown config node-type " + nt + " for watch-folder " + wfId + ", ignoring...");
return null;
}
for (Map.Entry<String, String> mpe : MANDATORY_PROP_MAP.entrySet()) {
String propName = mpe.getKey();
if (!n.hasProperty(propName)) {
logger.warn("Watch-folder config for " + wfId + " does not have mandatory property " + propName + ", ignoring...");
return null;
}
Property p = n.getProperty(propName);
String pType = PropertyType.nameFromValue((int)p.getType());
if (!mpe.getValue().equals(pType)) {
logger.warn("Watch-folder config for " + wfId + " has mandatory property " + propName + " of the wrong type " + pType + ", ignoring...");
return null;
}
Object val = this.getValue(wfId, p);
if (val != null) continue;
logger.warn("Watch-folder config for " + wfId + " has empty/non-singleton value for mandatory property " + propName + ", ignoring...");
return null;
}
WatchFolderConfiguration config = new WatchFolderConfiguration();
config.setId(wfId);
PropertyIterator pi = n.getProperties();
if (pi != null) {
while (pi.hasNext()) {
Property p = pi.nextProperty();
if (this.configurationHandlerFactory.enhanceConfiguration(config, p)) continue;
String pName = p.getName();
if (pName.startsWith("param.")) {
this.setConfigParam(wfId, config, p);
continue;
}
this.setProperty(wfId, config, p);
}
}
if (!this.inputProcessorFactory.getValidProcessorTypes().contains(config.getInputProcessorType())) {
logger.warn("Unknown input processor type " + config.getInputProcessorType() + " for watch folder " + wfId + ", ignoring...");
return null;
}
if (!config.isEnabled()) {
logger.warn("Watch-folder " + wfId + " is disabled, ignoring...");
return null;
}
if (config.getPollInterval() <= 0) {
logger.warn("Non-positive poll interval specified for watch folder " + wfId + ", reverting to default " + 5);
config.setPollInterval(5);
}
return config;
}
private void doAdd(Node configNode, boolean sf) throws Exception {
WatchFolderConfiguration config;
if (sf) {
this.activationCallbackFactory.onLocalConfigNodeAdd(configNode.getName(), configNode);
}
if ((config = this.toConfig(configNode)) != null) {
this.watchFolderConfigCache.addConfiguration(config);
this.scanScheduler.scheduleScan(config);
logger.info("Initialized scan-job and cache entry for watch-folder " + config.getId());
}
}
@Activate
public void activate() throws Exception {
this.theSession = this.sessionUtil.getServiceSession(null);
JcrUtil.makeSubDirIfRequired(this.theSession, "config", "sling:Folder");
NodeIterator ni = this.theSession.getNode("/etc/fd/watchfolder/config").getNodes();
if (ni != null) {
while (ni.hasNext()) {
Node n = ni.nextNode();
this.doAdd(n, false);
}
}
this.theSession.getWorkspace().getObservationManager().addEventListener((EventListener)this, 31, "/etc/fd/watchfolder/config", true, null, null, false);
logger.info("Registered listener for observing changes to watch-folder config repository...");
}
@Deactivate
public void deactivate() {
if (this.theSession != null) {
try {
this.theSession.getWorkspace().getObservationManager().removeEventListener((EventListener)this);
logger.info("De-registered watch-folder config repository listener.");
}
catch (RepositoryException e) {
logger.warn("Unexpected error de-registering watch-folder config repository listener.", (Throwable)e);
}
}
Collection<WatchFolderConfiguration> configs = this.watchFolderConfigCache.getAllConfigurations();
for (WatchFolderConfiguration config : configs) {
this.scanScheduler.tearDown(config.getId());
logger.info("Shut down scan job for watch-folder " + config.getId());
}
this.watchFolderConfigCache.clearAllConfigurations();
logger.info("Cleared watch-folder config cache.");
if (this.theSession != null) {
this.theSession.logout();
this.theSession = null;
}
}
private void doDelete(String wfId, boolean sf) throws Exception {
if (this.watchFolderConfigCache.containsConfiguration(wfId)) {
this.scanScheduler.tearDown(wfId);
this.watchFolderConfigCache.deleteConfiguration(wfId);
logger.info("Tore down scan job and cache entry for watch-folder " + wfId);
} else {
logger.warn("Watch-folder " + wfId + " not registered, no cache entry or scan job to tear down.");
}
if (sf) {
this.activationCallbackFactory.onLocalConfigNodeDelete(this.theSession, wfId);
}
}
private void doUpdate(String wfId) throws Exception {
Node configNode = this.getConfigNode(wfId);
if (this.watchFolderConfigCache.containsConfiguration(wfId)) {
WatchFolderConfiguration config = this.toConfig(configNode);
if (config == null) {
this.doDelete(wfId, false);
} else {
this.scanScheduler.tearDown(wfId);
this.watchFolderConfigCache.updateConfiguration(config);
this.scanScheduler.scheduleScan(config);
logger.info("Updated scan-job and cache for watch-folder " + wfId);
}
} else {
this.doAdd(configNode, false);
}
}
private boolean isLocal(Event e) {
if (e instanceof JackrabbitEvent) {
return !((JackrabbitEvent)e).isExternal();
}
return true;
}
private String getWatchFolderFromNodeOrPropertyPath(String path, boolean node) {
if (path == null) {
return null;
}
String pre = "/etc/fd/watchfolder/config/";
if (path.startsWith(pre)) {
path = path.substring(pre.length());
String[] comps = path.split("/");
if (node && comps.length == 1) {
return comps[0];
}
if (!node && comps.length == 2) {
return comps[0];
}
}
return null;
}
private void processEvents(EventIterator ei, Map<String, Boolean> adds, Map<String, Boolean> dels, Map<String, Boolean> updates) throws Exception {
if (ei != null) {
block5 : while (ei.hasNext()) {
Event e = ei.nextEvent();
boolean local = this.isLocal(e);
int eType = e.getType();
String ePath = e.getPath();
switch (eType) {
String wfId;
case 1: {
wfId = this.getWatchFolderFromNodeOrPropertyPath(ePath, true);
if (wfId == null) {
logger.warn("Received add event for node at path " + ePath + " which cannot be a watch-folder config, ignoring...");
continue block5;
}
logger.info("Received add event for watch-folder " + wfId);
adds.put(wfId, local);
continue block5;
}
case 2: {
wfId = this.getWatchFolderFromNodeOrPropertyPath(ePath, true);
if (wfId == null) {
logger.warn("Received delete event for node at path " + ePath + " which cannot be a watch-folder config, ignoring...");
continue block5;
}
logger.info("Received delete event for watch-folder " + wfId);
dels.put(wfId, local);
continue block5;
}
case 4:
case 8:
case 16: {
wfId = this.getWatchFolderFromNodeOrPropertyPath(ePath, false);
if (wfId == null) {
logger.warn("Received event for property at path " + ePath + " which cannot be a watch-folder config property, ignoring...");
continue block5;
}
logger.info("Received potential update event for watch-folder " + wfId + " due to operation of type " + eType + " on property " + ePath + ", flagging for further processing.");
updates.put(wfId, local);
continue block5;
}
}
logger.warn("Received event " + (Object)e + " of type " + eType + " for which we have not registered, ignoring...");
}
}
HashMap<String, Boolean> finalUpdates = new HashMap<String, Boolean>(updates.size());
for (Map.Entry<String, Boolean> me : updates.entrySet()) {
String wfId = me.getKey();
if (adds.containsKey(wfId)) {
logger.info("Watch-folder " + wfId + " already marked for addition, ignoring for update...");
continue;
}
if (dels.containsKey(wfId)) {
logger.info("Watch-folder " + wfId + " already marked for deletion, ignoring for update...");
continue;
}
logger.info("Watch-folder " + wfId + " confirmed for update.");
finalUpdates.put(wfId, me.getValue());
}
updates.clear();
updates.putAll(finalUpdates);
}
private Node getConfigNode(String wfId) throws Exception {
String path = "/etc/fd/watchfolder/config/" + wfId;
return this.theSession.getNode(path);
}
public void onEvent(EventIterator eventIterator) {
logger.info("Received modification event for watch-folder config repository, processing...");
HashMap<String, Boolean> adds = new HashMap<String, Boolean>();
HashMap<String, Boolean> dels = new HashMap<String, Boolean>();
HashMap<String, Boolean> updates = new HashMap<String, Boolean>();
try {
this.processEvents(eventIterator, adds, dels, updates);
}
catch (Exception e) {
logger.error("Unexpected error processing batch of watch-folder events, ignoring this batch...", (Throwable)e);
return;
}
for (Map.Entry<String, Boolean> ae22 : adds.entrySet()) {
try {
boolean local = ae22.getValue();
if (!local) {
logger.info("Will not attempt to create staging folder for watch-folder " + ae22.getKey() + " since config node was not created locally.");
}
this.doAdd(this.getConfigNode(ae22.getKey()), local);
}
catch (Exception e) {
logger.error("Error handling add for watch-folder " + ae22.getKey() + ", skipping...", (Throwable)e);
}
}
for (Map.Entry<String, Boolean> ae22 : updates.entrySet()) {
try {
this.doUpdate(ae22.getKey());
}
catch (Exception e) {
logger.error("Error handling update for watch-folder " + ae22.getKey() + ", skipping...", (Throwable)e);
}
}
for (Map.Entry<String, Boolean> ae22 : dels.entrySet()) {
try {
boolean local = ae22.getValue();
if (!local) {
logger.info("Will not attempt to delete staging folder for watch-folder " + ae22.getKey() + " since config node was not deleted locally.");
}
this.doDelete(ae22.getKey(), local);
}
catch (Exception e) {
logger.error("Error handling delete for watch-folder " + ae22.getKey() + ", skipping...", (Throwable)e);
}
}
}
static {
MANDATORY_PROP_MAP.put("folderPath", "String");
MANDATORY_PROP_MAP.put("inputProcessorType", "String");
MANDATORY_PROP_MAP.put("inputProcessorId", "String");
MANDATORY_PROP_MAP.put("outputFilePattern", "String");
}
protected void bindActivationCallbackFactory(ActivationCallbackFactory activationCallbackFactory) {
this.activationCallbackFactory = activationCallbackFactory;
}
protected void unbindActivationCallbackFactory(ActivationCallbackFactory activationCallbackFactory) {
if (this.activationCallbackFactory == activationCallbackFactory) {
this.activationCallbackFactory = null;
}
}
protected void bindInputProcessorFactory(InputProcessorFactory inputProcessorFactory) {
this.inputProcessorFactory = inputProcessorFactory;
}
protected void unbindInputProcessorFactory(InputProcessorFactory inputProcessorFactory) {
if (this.inputProcessorFactory == inputProcessorFactory) {
this.inputProcessorFactory = null;
}
}
protected void bindConfigurationHandlerFactory(ConfigurationHandlerFactory configurationHandlerFactory) {
this.configurationHandlerFactory = configurationHandlerFactory;
}
protected void unbindConfigurationHandlerFactory(ConfigurationHandlerFactory configurationHandlerFactory) {
if (this.configurationHandlerFactory == configurationHandlerFactory) {
this.configurationHandlerFactory = null;
}
}
protected void bindWatchFolderConfigCache(WatchFolderConfigCache watchFolderConfigCache) {
this.watchFolderConfigCache = watchFolderConfigCache;
}
protected void unbindWatchFolderConfigCache(WatchFolderConfigCache watchFolderConfigCache) {
if (this.watchFolderConfigCache == watchFolderConfigCache) {
this.watchFolderConfigCache = null;
}
}
protected void bindScanScheduler(ScanScheduler scanScheduler) {
this.scanScheduler = scanScheduler;
}
protected void unbindScanScheduler(ScanScheduler scanScheduler) {
if (this.scanScheduler == scanScheduler) {
this.scanScheduler = null;
}
}
protected void bindSessionUtil(SessionUtil sessionUtil) {
this.sessionUtil = sessionUtil;
}
protected void unbindSessionUtil(SessionUtil sessionUtil) {
if (this.sessionUtil == sessionUtil) {
this.sessionUtil = null;
}
}
}