OffloadingJobOffloader.java
15.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* 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.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* 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.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.offloading.impl;
import com.adobe.granite.offloading.api.OffloadingException;
import com.adobe.granite.offloading.api.OffloadingJobProperties;
import com.adobe.granite.offloading.impl.OffloadingConfigurator;
import com.adobe.granite.offloading.impl.OffloadingEventProcessor;
import com.adobe.granite.offloading.impl.OffloadingLocations;
import com.adobe.granite.offloading.impl.OffloadingProcessor;
import com.adobe.granite.offloading.impl.OffloadingStatus;
import com.adobe.granite.offloading.impl.OffloadingTransporter;
import com.adobe.granite.offloading.impl.util.OffloadingResourceUtil;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
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.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe Granite Offloading - Job Offloader")
@Service(value={EventHandler.class})
@Properties(value={@Property(name="event.topics", value={"org/apache/sling/api/resource/Resource/ADDED", "org/apache/sling/api/resource/Resource/CHANGED"}, propertyPrivate=1), @Property(name="event.filter", value={"(!(event.application=*))"}, propertyPrivate=1)})
@Reference(referenceInterface=OffloadingTransporter.class, cardinality=ReferenceCardinality.MANDATORY_MULTIPLE, policy=ReferencePolicy.DYNAMIC, name="transporter")
public class OffloadingJobOffloader
implements EventHandler,
OffloadingEventProcessor {
private final Logger log;
private Map<String, OffloadingTransporter> transporterMap;
private static final String SUB_SERVICE_NAME = "offloading";
private static final boolean OFFLOADER_ENABLED_DEFAULT = false;
private boolean offloaderEnabled;
@Property(label="Offloader Enabled", description="Allows to enable/disable the offloader in general.", boolValue={0})
private static final String OFFLOADER_ENABLED = "offloading.offloader.enabled";
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private OffloadingConfigurator configurator;
private OffloadingProcessor offloadingProcessor;
public OffloadingJobOffloader() {
this.log = LoggerFactory.getLogger(this.getClass());
this.transporterMap = new ConcurrentHashMap<String, OffloadingTransporter>();
this.offloaderEnabled = false;
}
@Activate
protected void activate(Map<String, Object> props) {
this.offloadingProcessor = new OffloadingProcessor(this, this.configurator, false, "Adobe Granite Offloading: Job Offloader");
this.modified(props);
}
protected void modified(Map<String, Object> props) {
ValueMapDecorator properties = new ValueMapDecorator(props);
this.offloaderEnabled = (Boolean)properties.get("offloading.offloader.enabled", (Object)false);
if (this.offloaderEnabled) {
this.offloadingProcessor.start();
} else {
this.offloadingProcessor.stop();
}
}
@Deactivate
protected void deactivate() {
this.offloadingProcessor.stop();
this.offloadingProcessor = null;
}
protected void bindTransporter(OffloadingTransporter transporter, Map<String, Object> properties) {
String transporterName = PropertiesUtil.toString((Object)properties.get("offloading.transporter.name"), (String)"");
if (StringUtils.isNotBlank((String)transporterName)) {
this.transporterMap.put(transporterName, transporter);
} else {
this.log.warn("Not able to bind transporter {}, because of missing service property", (Object)transporter.getClass().getCanonicalName());
}
}
protected void unbindTransporter(OffloadingTransporter transporter, Map<String, Object> properties) {
String transporterName = PropertiesUtil.toString((Object)properties.get("offloading.transporter.name"), (String)"");
if (StringUtils.isNotBlank((String)transporterName)) {
this.transporterMap.remove(transporterName);
} else {
this.log.warn("Not able to unbind transporter {}, because of missing service property", (Object)transporter.getClass().getCanonicalName());
}
}
public void handleEvent(Event event) {
if (this.offloaderEnabled) {
this.offloadingProcessor.handleEvent(event);
}
}
private boolean isEventForOffloader(Event event) {
String path = (String)event.getProperty("path");
String resourceType = (String)event.getProperty("resourceType");
return StringUtils.isNotBlank((String)path) && StringUtils.isNotBlank((String)resourceType) && path.startsWith(this.configurator.getOffloadingJobPath()) && resourceType.equals(this.configurator.getJobType());
}
private OffloadingTransporter getTransporter() {
OffloadingTransporter transporter = this.transporterMap.get(this.configurator.getTransporterName());
if (transporter == null) {
this.log.warn("no transporter found for configured transporter {}, will try to fallback to default transporter.", (Object)this.configurator.getTransporterName());
transporter = this.transporterMap.get("offloading.transporter.name");
}
if (transporter == null) {
throw new OffloadingException("no transporter is bound or found for configured transporter " + this.configurator.getTransporterName());
}
this.log.debug("Using transporter: " + transporter.getClass().getName());
return transporter;
}
private void transportToWorker(Resource offloadingJobResource) throws PersistenceException {
if (this.isOffloadingJob(offloadingJobResource)) {
try {
this.markJobOffloadedToWorker(offloadingJobResource);
this.getTransporter().transportToWorker(offloadingJobResource);
}
catch (OffloadingException e) {
this.log.error(e.getMessage(), (Throwable)e);
this.markJobToWorkerFailed(offloadingJobResource, e.getMessage());
}
}
}
private void transportToMaster(Resource offloadingJobResource) throws PersistenceException {
if (this.isOffloadingJob(offloadingJobResource)) {
try {
this.markJobBackOnMaster(offloadingJobResource);
this.getTransporter().transportToMaster(offloadingJobResource);
OffloadingResourceUtil.deleteJob(offloadingJobResource, this.configurator.getSlingJobPath(), this.configurator.getOffloadingJobPath());
}
catch (OffloadingException e) {
this.log.error(e.getMessage(), (Throwable)e);
this.markJobToMasterFailed(offloadingJobResource, e.getMessage());
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void processEvent(Event event) {
this.log.debug("process resource event {}", (Object)event.getTopic());
ResourceResolver resolver = null;
try {
resolver = this.getServiceResolver();
Resource jobResource = OffloadingResourceUtil.getJobResourceFromEvent(resolver, event);
if (jobResource != null) {
if (this.isNewJobForWorker(jobResource)) {
this.transportToWorker(jobResource);
} else if (this.isNewJobForMaster(jobResource)) {
this.transportToMaster(jobResource);
}
}
if (resolver.hasChanges()) {
resolver.commit();
}
}
catch (Exception e) {
this.log.error(e.getMessage(), (Throwable)e);
}
finally {
if (resolver != null) {
resolver.close();
}
}
}
@Override
public boolean doHandleEvent(Event event) {
return this.isEventForOffloader(event);
}
private boolean isNewJobForWorker(Resource jobResource) {
boolean newJobForWorker = false;
if (jobResource != null) {
ValueMap props = ResourceUtil.getValueMap((Resource)jobResource);
String location = (String)props.get(OffloadingJobProperties.LOCATION.propertyName(), (Object)"");
String status = (String)props.get(OffloadingJobProperties.STATUS.propertyName(), (Object)"");
String transporterError = (String)props.get(OffloadingJobProperties.TRANSPORT_ERROR.propertyName(), (Object)"");
newJobForWorker = location.equals(OffloadingLocations.MASTER.getPropertyValue()) && status.equals(OffloadingStatus.NEW.getStatusValue()) && StringUtils.isBlank((String)transporterError);
}
return newJobForWorker;
}
private boolean isNewJobForMaster(Resource jobResource) {
boolean newJobForMaster = false;
if (jobResource != null) {
ValueMap props = ResourceUtil.getValueMap((Resource)jobResource);
String location = (String)props.get(OffloadingJobProperties.LOCATION.propertyName(), (Object)"");
String status = (String)props.get(OffloadingJobProperties.STATUS.propertyName(), (Object)"");
String transporterError = (String)props.get(OffloadingJobProperties.TRANSPORT_ERROR.propertyName(), (Object)"");
newJobForMaster = location.equals(OffloadingLocations.WORKER.getPropertyValue()) && !status.equals(OffloadingStatus.NEW.getStatusValue()) && !status.equals(OffloadingStatus.STARTED.getStatusValue()) && !status.equals(OffloadingStatus.FAILED.getStatusValue()) && StringUtils.isBlank((String)transporterError);
}
return newJobForMaster;
}
private void markJobOffloadedToWorker(Resource jobResource) throws PersistenceException {
ModifiableValueMap jobProperties = (ModifiableValueMap)jobResource.adaptTo(ModifiableValueMap.class);
jobProperties.put((Object)OffloadingJobProperties.TIME_START.propertyName(), (Object)Calendar.getInstance());
jobProperties.put((Object)OffloadingJobProperties.LOCATION.propertyName(), (Object)OffloadingLocations.WORKER.getPropertyValue());
jobResource.getResourceResolver().commit();
}
private void markJobToWorkerFailed(Resource jobResource, String reason) throws PersistenceException {
ModifiableValueMap jobProperties = (ModifiableValueMap)jobResource.adaptTo(ModifiableValueMap.class);
jobProperties.remove((Object)OffloadingJobProperties.TIME_START.propertyName());
jobProperties.put((Object)OffloadingJobProperties.LOCATION.propertyName(), (Object)OffloadingLocations.MASTER.getPropertyValue());
if (StringUtils.isBlank((String)reason)) {
reason = "Transport error, but no reason given.";
}
jobProperties.put((Object)OffloadingJobProperties.TRANSPORT_ERROR.propertyName(), (Object)reason);
jobResource.getResourceResolver().commit();
}
private void markJobBackOnMaster(Resource jobResource) throws PersistenceException {
ModifiableValueMap jobProperties = (ModifiableValueMap)jobResource.adaptTo(ModifiableValueMap.class);
jobProperties.put((Object)OffloadingJobProperties.TIME_END.propertyName(), (Object)Calendar.getInstance());
jobProperties.put((Object)OffloadingJobProperties.LOCATION.propertyName(), (Object)OffloadingLocations.MASTER.getPropertyValue());
jobResource.getResourceResolver().commit();
}
private void markJobToMasterFailed(Resource jobResource, String reason) throws PersistenceException {
ModifiableValueMap jobProperties = (ModifiableValueMap)jobResource.adaptTo(ModifiableValueMap.class);
jobProperties.remove((Object)OffloadingJobProperties.TIME_END.propertyName());
jobProperties.put((Object)OffloadingJobProperties.LOCATION.propertyName(), (Object)OffloadingLocations.WORKER.getPropertyValue());
if (StringUtils.isBlank((String)reason)) {
reason = "Transport error, but no reason given.";
}
jobProperties.put((Object)OffloadingJobProperties.TRANSPORT_ERROR.propertyName(), (Object)reason);
jobResource.getResourceResolver().commit();
}
private boolean isOffloadingJob(Resource jobResource) {
return jobResource != null && jobResource.getPath().startsWith(this.configurator.getOffloadingJobPath());
}
private ResourceResolver getServiceResolver() throws LoginException {
HashMap<String, String> authenticationInfo = new HashMap<String, String>();
authenticationInfo.put("sling.service.subservice", "offloading");
return this.resolverFactory.getServiceResourceResolver(authenticationInfo);
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindConfigurator(OffloadingConfigurator offloadingConfigurator) {
this.configurator = offloadingConfigurator;
}
protected void unbindConfigurator(OffloadingConfigurator offloadingConfigurator) {
if (this.configurator == offloadingConfigurator) {
this.configurator = null;
}
}
}