PromotionManagerImpl.java
13.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* 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.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.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.promotion;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.api.promotion.PromotionManager;
import com.adobe.cq.commerce.api.promotion.Voucher;
import com.adobe.cq.commerce.common.promotion.AbstractJcrVoucher;
import com.adobe.cq.commerce.impl.promotion.JcrPromotionImpl;
import com.adobe.cq.commerce.impl.promotion.JcrVoucherImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
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.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.SlingHttpServletRequest;
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.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=1, label="Adobe CQ Commerce Promotion Manager", description="Manages caching and lookup of promotions and promotion handlers.")
@Service(value={PromotionManager.class})
@Property(name="service.description", value={"Manages caching and lookup of promotions and promotion handlers"})
public class PromotionManagerImpl
implements PromotionManager,
EventListener {
private final Logger log = LoggerFactory.getLogger(PromotionManagerImpl.class);
static final String DEFAULT_PROMOTION_ROOT = "/content/campaigns";
@Property(label="%Promotions Root", description="%Search root for promotions", value={"/content/campaigns"})
private static final String PROMOTION_ROOT_PROP_NAME = "cq.commerce.promotion.root";
@Reference
private ResourceResolverFactory resolverFactory = null;
@Reference(referenceInterface=PromotionHandler.class, bind="bindPromotionHandler", unbind="unbindPromotionHandler", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<String, PromotionHandler> promotionHandlers = new HashMap<String, PromotionHandler>();
private String promotionRoot;
private ResourceResolver serviceResolver;
private ObservationManager observationManager;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private List<Promotion> promotionsCache = new ArrayList<Promotion>();
private boolean cachesDirty = true;
private Map<String, List<Voucher>> vouchersCache = new HashMap<String, List<Voucher>>();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindPromotionHandler(PromotionHandler condition, Map<?, ?> properties) {
this.lock.writeLock().lock();
try {
this.promotionHandlers.put((String)properties.get("commerce.promotion.type"), condition);
}
finally {
this.lock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindPromotionHandler(PromotionHandler condition, Map<?, ?> properties) {
this.lock.writeLock().lock();
try {
this.promotionHandlers.remove((String)properties.get("commerce.promotion.type"));
}
finally {
this.lock.writeLock().unlock();
}
}
@Activate
protected void activate(ComponentContext context) throws Exception {
this.promotionRoot = OsgiUtil.toString(context.getProperties().get("cq.commerce.promotion.root"), (String)"/content/campaigns");
HashMap<String, String> authenticationInfo = new HashMap<String, String>();
authenticationInfo.put("sling.service.subservice", "frontend");
this.serviceResolver = this.resolverFactory.getServiceResourceResolver(authenticationInfo);
Session serviceSession = (Session)this.serviceResolver.adaptTo(Session.class);
this.observationManager = serviceSession.getWorkspace().getObservationManager();
this.observationManager.addEventListener((EventListener)this, 63, this.promotionRoot, true, null, null, true);
}
@Deactivate
protected void deactivate(ComponentContext context) throws RepositoryException {
if (this.serviceResolver != null && this.serviceResolver.isLive()) {
if (this.observationManager != null) {
this.observationManager.removeEventListener((EventListener)this);
}
this.serviceResolver.close();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onEvent(EventIterator events) {
this.lock.writeLock().lock();
try {
this.cachesDirty = true;
for (PromotionHandler handler : this.promotionHandlers.values()) {
try {
handler.invalidateCaches();
}
catch (Exception x) {
this.log.error("Failed to invalidate cache {}", (Throwable)x);
}
}
}
finally {
this.lock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Map<String, String> getPromotionsMap(SlingHttpServletRequest request) {
this.lock.readLock().lock();
try {
this.validateCaches();
HashMap<String, String> map = new HashMap<String, String>();
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
for (Promotion promo : this.promotionsCache) {
try {
if (!promo.isValid() || !session.nodeExists(promo.getPath())) continue;
map.put(promo.getPath(), StringUtils.join(promo.getSegments(), (String)","));
}
catch (RepositoryException e) {}
}
HashMap<String, String> i$ = map;
return i$;
}
finally {
this.lock.readLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public List<Promotion> getAvailablePromotions(ResourceResolver resourceResolver) {
this.lock.readLock().lock();
try {
this.validateCaches();
ArrayList<Promotion> list = new ArrayList<Promotion>();
Session session = (Session)resourceResolver.adaptTo(Session.class);
for (Promotion promo : this.promotionsCache) {
try {
if (!session.nodeExists(promo.getPath())) continue;
list.add(promo);
}
catch (RepositoryException e) {}
}
ArrayList<Promotion> i$ = list;
return i$;
}
finally {
this.lock.readLock().unlock();
}
}
@Deprecated
@Override
public AbstractJcrVoucher getVoucher(SlingHttpServletRequest request, String code) {
Voucher voucher = this.findVoucher(request, code);
if (voucher == null) {
return null;
}
try {
return new AbstractJcrVoucher(request.getResourceResolver().getResource(voucher.getPath()));
}
catch (CommerceException e) {
return null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Voucher findVoucher(SlingHttpServletRequest request, String code) {
List<Voucher> vouchers;
this.lock.readLock().lock();
try {
this.validateCaches();
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
vouchers = this.vouchersCache.get(code);
if (vouchers == null) {
Voucher voucher = null;
return voucher;
}
Voucher first = null;
Voucher firstValid = null;
for (Voucher voucher : vouchers) {
if (voucher == null || !session.nodeExists(voucher.getPath())) continue;
if (first == null) {
first = voucher;
}
if (!voucher.isValid(request)) continue;
if (firstValid == null) {
firstValid = voucher;
continue;
}
this.log.warn("Multiple vouchers using the code '{0}' are currently valid.", (Object)code);
}
Iterator<Voucher> i$ = firstValid != null ? firstValid : first;
return i$;
}
catch (RepositoryException e) {
vouchers = null;
return vouchers;
}
finally {
this.lock.readLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void validateCaches() {
if (this.cachesDirty) {
this.lock.readLock().unlock();
this.lock.writeLock().lock();
try {
if (this.cachesDirty) {
this.promotionsCache.clear();
this.vouchersCache.clear();
this.collector(this.serviceResolver.getResource(this.promotionRoot));
this.cachesDirty = false;
}
}
finally {
this.lock.readLock().lock();
this.lock.writeLock().unlock();
}
}
}
private void collector(Resource resource) {
if (resource == null) {
return;
}
if (resource.isResourceType("commerce/components/voucher")) {
try {
JcrVoucherImpl voucher = new JcrVoucherImpl(resource.getParent());
List<Voucher> list = this.vouchersCache.get(voucher.getCode());
if (list == null) {
list = new ArrayList<Voucher>();
this.vouchersCache.put(voucher.getCode(), list);
}
list.add(voucher);
Collections.sort(list, VoucherPrioritySorter.INSTANCE);
}
catch (CommerceException e) {
this.log.error(e.getMessage());
}
} else if (resource.isResourceType("commerce/components/promotion")) {
try {
JcrPromotionImpl promotion = new JcrPromotionImpl(resource.getParent());
this.promotionsCache.add(promotion);
}
catch (CommerceException e) {
this.log.error(e.getMessage());
}
} else {
Iterator i = resource.listChildren();
while (i.hasNext()) {
this.collector((Resource)i.next());
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public PromotionHandler getHandler(String promotionType) {
this.lock.readLock().lock();
try {
PromotionHandler promotionHandler = this.promotionHandlers.get(promotionType);
return promotionHandler;
}
finally {
this.lock.readLock().unlock();
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
static class VoucherPrioritySorter
implements Comparator<Voucher> {
public static final VoucherPrioritySorter INSTANCE = new VoucherPrioritySorter();
private VoucherPrioritySorter() {
}
@Override
public int compare(Voucher o1, Voucher o2) {
long p2;
long p1 = o1.getPriority();
return p1 < (p2 = o2.getPriority()) ? 1 : (p1 != p2 ? -1 : 0);
}
}
}