OffloadingResourceUtil.java
4.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.service.event.Event
*/
package com.adobe.granite.offloading.impl.util;
import com.adobe.granite.offloading.api.OffloadingJobProperties;
import com.adobe.granite.offloading.impl.OffloadingStatus;
import com.adobe.granite.offloading.impl.util.OffloadingUtil;
import java.util.Calendar;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.event.Event;
public class OffloadingResourceUtil {
public static void markOffloadingJobFinished(Resource jobResource) {
OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.FINISHED.getStatusValue());
}
public static void markOffloadingJobFailed(Resource jobResource) {
OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.FAILED.getStatusValue());
}
public static void markOffloadingJobCancelled(Resource jobResource) {
OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.CANCELLED.getStatusValue());
}
public static void markOffloadingJobStarted(Resource jobResource) {
OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.STARTED.getStatusValue());
}
public static void markJobStarted(Resource jobResource) {
OffloadingResourceUtil.setProperty(jobResource, "event.job.started.time", Calendar.getInstance());
}
public static void markJobNotStarted(Resource jobResource) {
OffloadingResourceUtil.unsetProperty(jobResource, "event.job.started.time");
}
public static void setProperty(Resource resource, String property, Object value) {
ModifiableValueMap jobProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
jobProperties.put((Object)property, value);
}
public static void unsetProperty(Resource resource, String property) {
ModifiableValueMap jobProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
if (jobProperties.containsKey((Object)property)) {
jobProperties.remove((Object)property);
}
}
public static Resource getJobResourceFromEvent(ResourceResolver resolver, Event event) {
String path = (String)event.getProperty("path");
if (StringUtils.isNotBlank((String)path)) {
return resolver.getResource(path);
}
return null;
}
public static Resource getJobResourceFromJobNotification(ResourceResolver resolver, Event event, String offloadingJobsBasePath) {
String jobId = (String)event.getProperty("slingevent:eventId");
String targetInstance = (String)event.getProperty("event.job.application");
String jobTopic = (String)event.getProperty("event.job.topic");
if (StringUtils.isNotBlank((String)jobId) && StringUtils.isNotBlank((String)targetInstance) && StringUtils.isNotBlank((String)jobTopic)) {
String offloadingJobPath = OffloadingUtil.getJobPathFromJobId(offloadingJobsBasePath, targetInstance, jobTopic, jobId);
return resolver.getResource(offloadingJobPath);
}
return null;
}
public static void deleteJob(Resource offloadingJobResource, String slingJobPathPrefix, String offloadingJobPathPrefix) throws PersistenceException {
String offloadingJobPath = offloadingJobResource.getPath();
String jobPath = offloadingJobPath.replace(offloadingJobPathPrefix, slingJobPathPrefix);
OffloadingResourceUtil.deleteJob(offloadingJobResource.getResourceResolver(), jobPath);
OffloadingResourceUtil.deleteJob(offloadingJobResource.getResourceResolver(), offloadingJobPath);
}
public static void deleteJob(ResourceResolver resolver, String jobPath) throws PersistenceException {
Resource jobResource = resolver.getResource(jobPath);
if (jobResource != null) {
resolver.delete(jobResource);
}
}
}