CanvasPageDeleteHandler.java
11.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.Replicator
* com.day.cq.search.PredicateGroup
* com.day.cq.search.Query
* com.day.cq.search.QueryBuilder
* com.day.cq.search.result.Hit
* com.day.cq.search.result.SearchResult
* com.day.cq.wcm.api.PageEvent
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.PageModification$ModificationType
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.Service
* org.apache.sling.api.resource.LoginException
* 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.apache.sling.commons.threads.ModifiableThreadPoolConfig
* org.apache.sling.commons.threads.ThreadPool
* org.apache.sling.commons.threads.ThreadPoolConfig
* org.apache.sling.commons.threads.ThreadPoolConfig$ThreadPriority
* org.apache.sling.commons.threads.ThreadPoolManager
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.designimporter.impl;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.designimporter.util.ImporterUtil;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Service;
import org.apache.sling.api.resource.LoginException;
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.apache.sling.commons.threads.ModifiableThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPool;
import org.apache.sling.commons.threads.ThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1)
@Service
@Properties(value={@Property(name="event.topics", value={"com/day/cq/wcm/core/page"}, propertyPrivate=1), @Property(name="event.filter", value={"(!(event.application=*))"}, propertyPrivate=1)})
public class CanvasPageDeleteHandler
implements EventHandler {
private static final Logger logger = LoggerFactory.getLogger(CanvasPageDeleteHandler.class);
private static final String SERVICE_USER = "canvaspage-delete-service";
@Reference
Replicator replicator;
@Reference
ResourceResolverFactory resourceResolverFactory;
@Reference
private ThreadPoolManager threadPoolManager;
private ThreadPool threadPool;
@Property(label="minThreadPoolSize", description="Minimum Thread pool size", intValue={2})
private static final String MINIMUM_THREAD_POOL_SIZE = "minThreadPoolSize";
private int minThreadPoolSize;
@Property(label="maxThreadPoolSize", description="Maximum Thread pool size", intValue={10})
private static final String MAXIMUM_THREAD_POOL_SIZE = "maxThreadPoolSize";
private int maxThreadPoolSize;
@Reference
QueryBuilder queryBuilder;
@Activate
protected final void activate(Map<Object, Object> config) {
this.minThreadPoolSize = OsgiUtil.toInteger((Object)config.get("minThreadPoolSize"), (int)5);
this.maxThreadPoolSize = OsgiUtil.toInteger((Object)config.get("maxThreadPoolSize"), (int)10);
ModifiableThreadPoolConfig threadPoolConfig = new ModifiableThreadPoolConfig();
if (threadPoolConfig.getMinPoolSize() < this.minThreadPoolSize) {
threadPoolConfig.setMinPoolSize(this.minThreadPoolSize);
}
if (threadPoolConfig.getMaxPoolSize() < this.maxThreadPoolSize) {
threadPoolConfig.setMaxPoolSize(this.maxThreadPoolSize);
}
threadPoolConfig.setPriority(ThreadPoolConfig.ThreadPriority.NORM);
this.threadPool = this.threadPoolManager.create((ThreadPoolConfig)threadPoolConfig);
if (this.threadPool == null) {
throw new IllegalStateException("Could not get a ThreadPool");
}
}
@Deactivate
protected void deactivate(ComponentContext ctx) throws Exception {
if (this.threadPool != null) {
this.threadPoolManager.release(this.threadPool);
this.threadPoolManager = null;
this.threadPool = null;
}
}
public void handleEvent(final Event event) {
PageEvent pageEvent = PageEvent.fromEvent((Event)event);
if (pageEvent != null) {
Iterator iter = pageEvent.getModifications();
while (iter.hasNext()) {
PageModification mod = (PageModification)iter.next();
if (!mod.getType().equals((Object)PageModification.ModificationType.DELETED)) continue;
this.threadPool.execute(new Runnable(){
@Override
public void run() {
CanvasPageDeleteHandler.this.process(event);
}
});
break;
}
}
}
private boolean process(Event event) {
PageEvent pageEvent = PageEvent.fromEvent((Event)event);
if (pageEvent != null) {
Iterator iter = pageEvent.getModifications();
while (iter.hasNext()) {
PageModification mod = (PageModification)iter.next();
switch (mod.getType()) {
case DELETED: {
this.handlePageDelete(mod.getPath());
}
}
}
}
return true;
}
private void handlePageDelete(String pagePath) {
try {
ResourceResolver resolver = this.getServiceResolver("canvaspage-activate-service");
Session session = (Session)resolver.adaptTo(Session.class);
LinkedList<String> deactivationPaths = new LinkedList<String>();
this.addDesignDeactivationPath(pagePath, session, deactivationPaths);
this.addComponentDeactivationPath(pagePath, session, deactivationPaths);
int i = pagePath.lastIndexOf("/");
if (i >= 0 && i < pagePath.length() - 1) {
String name = pagePath.substring(i + 1);
this.addDesignDeactivationPath(pagePath + "/" + name, session, deactivationPaths);
this.addComponentDeactivationPath(pagePath + "/" + name, session, deactivationPaths);
}
ResourceResolver deleteResolver = this.getServiceResolver("canvaspage-delete-service");
for (String path : deactivationPaths) {
this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
Resource resource = deleteResolver.resolve(path);
ImporterUtil.deleteCanvasArtifact(resource);
}
((Session)deleteResolver.adaptTo(Session.class)).save();
}
catch (LoginException e) {
logger.error("Login error occurred while handling canvas page deletion", (Throwable)e);
}
catch (RepositoryException e) {
logger.error("Error occurred while handling canvas page deletion", (Throwable)e);
}
catch (ReplicationException e) {
logger.error("Replication error occurred while handling canvas page deletion", (Throwable)e);
}
}
private void addDesignDeactivationPath(String canvasPagePath, Session session, List<String> deactivationPaths) throws RepositoryException {
HashMap<String, String> map = new HashMap<String, String>();
map.put("path", "/etc/designs");
map.put("property", "cq:ownerCanvasPage");
map.put("property.value", canvasPagePath);
Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
for (Hit hit : result.getHits()) {
String path = hit.getPath();
path = path.replaceAll("/jcr:content$", "");
deactivationPaths.add(path);
}
}
private void addComponentDeactivationPath(String canvasPagePath, Session session, List<String> deactivationPaths) throws RepositoryException {
HashMap<String, String> map = new HashMap<String, String>();
map.put("path", "/apps");
map.put("property", "cq:ownerCanvasPage");
map.put("property.value", canvasPagePath);
Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
for (Hit hit : result.getHits()) {
String path = hit.getPath();
deactivationPaths.add(path);
}
}
private ResourceResolver getServiceResolver(String userId) throws LoginException {
Map<String, String> info = Collections.singletonMap("sling.service.subservice", userId);
return this.resourceResolverFactory.getServiceResourceResolver(info);
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
this.threadPoolManager = threadPoolManager;
}
protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
if (this.threadPoolManager == threadPoolManager) {
this.threadPoolManager = null;
}
}
protected void bindQueryBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
}
protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
if (this.queryBuilder == queryBuilder) {
this.queryBuilder = null;
}
}
}