CanvasPageActivationHandler.java
8.57 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
/*
* 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.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.Event
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* 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.api.resource.ValueMap
* org.osgi.service.component.ComponentContext
* 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.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.designimporter.impl.common.PathSchemeHelper;
import com.day.cq.wcm.designimporter.util.ImporterUtil;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
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.Reference;
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.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class CanvasPageActivationHandler
implements EventListener {
@Reference
ResourceResolverFactory resourceResolverFactory;
@Reference
private Replicator replicator;
@Reference
private ResourceResolverFactory resolverFactory;
protected final Logger log;
public static final String SERVICE_USER = "canvaspage-activate-service";
private ResourceResolver resolver;
private static final String LAST_REPLICATION_ACTION = "cq:lastReplicationAction";
public CanvasPageActivationHandler() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Unable to fully structure code
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
public void onEvent(EventIterator events) {
lbl1: // 11 sources:
while (events.hasNext() != false) {
evt = events.nextEvent();
if (evt.getType() != 4 && evt.getType() != 16) continue;
resolver = null;
try {
path = evt.getPath();
if (!path.endsWith("cq:lastReplicationAction")) continue;
path = path.substring(0, path.lastIndexOf("jcr:content"));
resolver = this.getServiceResolver();
pageManager = (PageManager)resolver.adaptTo(PageManager.class);
page = pageManager.getPage(path);
if (page == null) continue;
props = page.getProperties();
replicationAction = (String)props.get((Object)"cq:lastReplicationAction");
replicationActionType = ReplicationActionType.fromName((String)replicationAction);
switch (.$SwitchMap$com$day$cq$replication$ReplicationActionType[replicationActionType.ordinal()]) {
case 1: {
this.handlePageActivation(page);
** break;
}
case 2: {
this.handlePageDeactivation(page);
}
}
continue;
}
catch (RepositoryException e) {
this.log.error("Repository exception encountered", (Throwable)e);
continue;
}
catch (ReplicationException e) {
this.log.error("Replication exception encountered", (Throwable)e);
continue;
}
catch (LoginException e) {
this.log.error("Unable to get service resource resolver", (Throwable)e);
continue;
}
finally {
if (resolver == null) continue;
resolver.close();
continue;
}
}
}
private void handlePageActivation(Page page) throws RepositoryException, ReplicationException {
this.deactivatePendingArtifacts(page);
}
private void handlePageDeactivation(Page page) throws RepositoryException, ReplicationException {
List<Resource> importers = ImporterUtil.findImporters((Resource)page.adaptTo(Resource.class));
for (Resource importer : importers) {
Resource canvas = ImporterUtil.getCanvas(importer);
String canvasCompPath = PathSchemeHelper.getCanvasComponentPath(canvas);
String canvasDesPAth = PathSchemeHelper.getCanvasDesignPath(canvas);
Session session = ((Node)page.adaptTo(Node.class)).getSession();
this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, canvasDesPAth);
this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, canvasCompPath);
}
this.deactivatePendingArtifacts(page);
}
private void deactivatePendingArtifacts(Page page) throws RepositoryException, ReplicationException {
Session session = ((Node)page.adaptTo(Node.class)).getSession();
String[] pendingPaths = (String[])page.getProperties().get((Object)"cq:canvasArtifactsPendingDeactivate");
if (pendingPaths != null) {
for (String path : pendingPaths) {
this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
}
}
}
@Activate
protected void activate(ComponentContext context) throws Exception {
this.resolver = this.getServiceResolver();
Session session = (Session)this.resolver.adaptTo(Session.class);
session.getWorkspace().getObservationManager().addEventListener((EventListener)this, 20, "/content/campaigns", true, null, new String[]{"cq:PageContent"}, true);
}
@Deactivate
protected void deactivate(ComponentContext context) throws Exception {
Session session = (Session)this.resolver.adaptTo(Session.class);
session.getWorkspace().getObservationManager().removeEventListener((EventListener)this);
session.logout();
}
private ResourceResolver getServiceResolver() throws LoginException {
Map<String, String> info = Collections.singletonMap("sling.service.subservice", "canvaspage-activate-service");
return this.resolverFactory.getServiceResourceResolver(info);
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
}