StaleAssetIndicatorServiceImpl.java
8.26 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.forms.common.service.StaleAssetIndicatorService
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.api.policies.ContentPolicy
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aem.formsndocuments.service.impl;
import com.adobe.aem.formsndocuments.service.FormsRelationService;
import com.adobe.aem.formsndocuments.transferobjects.AssetInfo;
import com.adobe.aem.formsndocuments.util.FMUtils;
import com.adobe.forms.common.service.StaleAssetIndicatorService;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.policies.ContentPolicy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
@Service(value={StaleAssetIndicatorService.class})
public class StaleAssetIndicatorServiceImpl
implements StaleAssetIndicatorService {
@Reference(referenceInterface=FormsRelationService.class)
private FormsRelationService formsRelationService;
private final Logger logger = LoggerFactory.getLogger(StaleAssetIndicatorServiceImpl.class);
private Map<String, AssetTreeLMTEntry> assetTreeLMTTable = new ConcurrentHashMap<String, AssetTreeLMTEntry>();
public long getAssetTreeLMT(Resource resource) {
AssetTreeLMTEntry assetTreeLMTEntry;
block9 : {
ResourceResolver resourceResolver = resource.getResourceResolver();
Session session = (Session)resourceResolver.adaptTo(Session.class);
String assetPath = resource.getPath();
if (null == session) {
this.logger.error("[StaleAssetIndicator] : Failed to get valid Session for " + assetPath);
return Long.MAX_VALUE;
}
if (null == assetPath || "".equals(assetPath)) {
this.logger.error("[StaleAssetIndicator] : Empty asset path");
return Long.MAX_VALUE;
}
assetTreeLMTEntry = this.assetTreeLMTTable.get(assetPath);
if (null == assetTreeLMTEntry) {
this.logger.debug("[StaleAssetIndicator] : Cache Miss " + assetPath);
assetTreeLMTEntry = this.reCacheAssetTreeLMTTableEntry(resourceResolver, session, assetPath);
} else {
try {
long assetCurrentLMT = FMUtils.getLastModifiedOrCreated(session, assetPath);
if (assetCurrentLMT < 1 || assetTreeLMTEntry.assetLMT == Long.MAX_VALUE || assetCurrentLMT > assetTreeLMTEntry.assetLMT) {
this.logger.debug("[StaleAssetIndicator] : Cache Stale " + assetPath);
assetTreeLMTEntry = this.reCacheAssetTreeLMTTableEntry(resourceResolver, session, assetPath);
break block9;
}
for (DependantEntry dependant : assetTreeLMTEntry.dependants) {
long dependantCurrentLMT = FMUtils.getLastModifiedOrCreated(session, dependant.path);
if (dependantCurrentLMT >= 1 && dependant.lmt >= 1 && dependantCurrentLMT <= dependant.lmt) continue;
this.logger.debug("[StaleAssetIndicator] : Cache Stale " + assetPath);
assetTreeLMTEntry = this.reCacheAssetTreeLMTTableEntry(resourceResolver, session, assetPath);
break;
}
}
catch (Exception e) {
this.logger.error("[StaleAssetIndicator] : Failed to get all related Child Assets for " + assetPath, (Throwable)e);
}
}
}
if (null != assetTreeLMTEntry) {
return Math.max(assetTreeLMTEntry.assetLMT, assetTreeLMTEntry.assetTreeLMT);
}
return Long.MAX_VALUE;
}
private AssetTreeLMTEntry reCacheAssetTreeLMTTableEntry(ResourceResolver resourceResolver, Session session, String assetPath) {
AssetTreeLMTEntry assetTreeLMTEntry = null;
HashSet<AssetInfo> relatedChildAssets = new HashSet<AssetInfo>();
long assetTreeLMT = -1;
ArrayList<DependantEntry> dependants = null;
try {
this.assetTreeLMTTable.remove(assetPath);
long assetLMT = FMUtils.getLastModifiedOrCreated(session, assetPath);
this.formsRelationService.getAllRelatedChildAssets(resourceResolver, assetPath, relatedChildAssets);
if (null != relatedChildAssets) {
dependants = new ArrayList<DependantEntry>();
for (AssetInfo asset : relatedChildAssets) {
if (!this.isSupportedDependant(asset)) continue;
long assetCurrentLMT = asset.getLastModifiedOrCreatedTime(session);
dependants.add(new DependantEntry(asset.getResource().getPath(), assetCurrentLMT));
assetTreeLMT = assetTreeLMT > assetCurrentLMT ? assetTreeLMT : assetCurrentLMT;
}
assetLMT = assetLMT > 0 ? assetLMT : Long.MAX_VALUE;
assetTreeLMT = assetTreeLMT > 0 ? assetTreeLMT : Long.MAX_VALUE;
assetTreeLMTEntry = new AssetTreeLMTEntry(assetLMT, assetTreeLMT, dependants);
this.assetTreeLMTTable.put(assetPath, assetTreeLMTEntry);
this.logger.debug("[StaleAssetIndicator] : Cache put : " + assetPath + " : " + assetTreeLMTEntry.assetLMT + " : " + assetTreeLMTEntry.assetTreeLMT);
}
}
catch (Exception e) {
this.logger.error("[StaleAssetIndicator] : Failed to get all related Child Assets for " + assetPath, (Throwable)e);
}
return assetTreeLMTEntry;
}
private boolean isSupportedDependant(AssetInfo assetInfo) {
try {
Resource assetResource = assetInfo.getResource();
Template afTemplate = (Template)assetResource.adaptTo(Template.class);
if (afTemplate != null) {
return false;
}
ContentPolicy contentPolicy = (ContentPolicy)assetResource.adaptTo(ContentPolicy.class);
if (contentPolicy != null) {
return false;
}
Node assetNode = (Node)assetResource.adaptTo(Node.class);
if (assetNode.isNodeType("cq:ClientLibraryFolder")) {
return false;
}
}
catch (RepositoryException e) {
this.logger.warn("[StaleAssetIndicator] : Failed to deduce if {0} is a supported asset.", (Object)assetInfo.getResource().getPath());
}
return true;
}
protected void bindFormsRelationService(FormsRelationService formsRelationService) {
this.formsRelationService = formsRelationService;
}
protected void unbindFormsRelationService(FormsRelationService formsRelationService) {
if (this.formsRelationService == formsRelationService) {
this.formsRelationService = null;
}
}
private class AssetTreeLMTEntry {
public final long assetLMT;
public final long assetTreeLMT;
public final List<DependantEntry> dependants;
public AssetTreeLMTEntry(long assetLMT, long assetTreeLMT, List<DependantEntry> dependants) {
this.assetLMT = assetLMT;
this.assetTreeLMT = assetTreeLMT;
this.dependants = new ArrayList<DependantEntry>(dependants);
}
}
private class DependantEntry {
public final String path;
public final long lmt;
public DependantEntry(String path, long lmt) {
this.path = path;
this.lmt = lmt;
}
}
}