LaunchAutoPromoteJob.java
8.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchException
* com.adobe.cq.launches.api.LaunchManager
* com.adobe.cq.launches.api.LaunchPromotionParameters
* com.adobe.cq.launches.api.LaunchPromotionScope
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationStatus
* com.day.cq.replication.Replicator
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.reference.Reference
* com.day.cq.wcm.api.reference.ReferenceProvider
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* 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.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.launches.impl;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchPromotionParameters;
import com.adobe.cq.launches.api.LaunchPromotionScope;
import com.adobe.cq.wcm.launches.impl.AutomaticLaunchPromotionParameters;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.jcr.Node;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
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.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={JobConsumer.class})
@Property(name="job.topics", value={"com/adobe/cq/wcm/launches/autopromote"})
public class LaunchAutoPromoteJob
implements JobConsumer {
private static final Logger log = LoggerFactory.getLogger(LaunchAutoPromoteJob.class);
static final String JOB_TOPIC = "com/adobe/cq/wcm/launches/autopromote";
static final String PN_PATH = "launchPath";
static final String PN_LIVE_DATE = "liveDate";
static final String PN_USER_ID = "userId";
private static final String LAUNCH_PROMOTE_SERVICE_USER = "launch-promote-service";
@org.apache.felix.scr.annotations.Reference
private ResourceResolverFactory resolverFactory;
@org.apache.felix.scr.annotations.Reference
private Replicator replicator;
@org.apache.felix.scr.annotations.Reference(referenceInterface=ReferenceProvider.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private final List<ReferenceProvider> referenceProviders = new CopyOnWriteArrayList<ReferenceProvider>();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public JobConsumer.JobResult process(Job job) {
String launchPath = (String)job.getProperty("launchPath", String.class);
Calendar liveDate = (Calendar)job.getProperty("liveDate", Calendar.class);
String userId = (String)job.getProperty("userId", String.class);
log.info("Executing auto-promotion of launch [{}] at date: {}, triggered by {}", new Object[]{launchPath, liveDate.getTime(), userId});
ResourceResolver resolver = null;
try {
Map<String, String> info = Collections.singletonMap("sling.service.subservice", "launch-promote-service");
resolver = this.resolverFactory.getServiceResourceResolver(info);
LaunchManager launchManager = (LaunchManager)resolver.adaptTo(LaunchManager.class);
Launch launch = launchManager.getLaunch(launchPath);
AutomaticLaunchPromotionParameters launchPromotionParameters = new AutomaticLaunchPromotionParameters(launch, LaunchPromotionScope.FULL);
launchManager.promoteLaunch(launch, (LaunchPromotionParameters)launchPromotionParameters);
try {
Node contentNode = ((Node)launch.getResource().adaptTo(Node.class)).getNode("jcr:content");
contentNode.setProperty("lastPromotedBy", userId);
}
catch (Exception e) {
log.warn("Unable to set lastPromotedBy", (Throwable)e);
}
Resource productionRes = LaunchUtils.getTargetResource(launch.getRootResource(), null);
this.publish((Session)resolver.adaptTo(Session.class), (Page)productionRes.adaptTo(Page.class), launch.isDeep());
JobConsumer.JobResult jobResult = JobConsumer.JobResult.OK;
return jobResult;
}
catch (LoginException e) {
log.error("Unable to get a resource resolver while auto-promoting launch [{}]", (Object)launchPath, (Object)e);
}
catch (LaunchException e) {
log.error("Unable to auto-promote launch [{}]", (Object)launchPath, (Object)e);
}
catch (ReplicationException e) {
log.error("Unable to activate auto-promote launch [{}]", (Object)launchPath, (Object)e);
}
finally {
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
return JobConsumer.JobResult.FAILED;
}
private void publish(Session session, Page page, boolean isDeep) throws ReplicationException {
this.replicator.replicate(session, ReplicationActionType.ACTIVATE, page.getPath());
for (ReferenceProvider referenceProvider : this.referenceProviders) {
for (Reference reference : referenceProvider.findReferences((Resource)page.adaptTo(Resource.class))) {
Resource resource = reference.getResource();
boolean published = false;
boolean outdated = false;
ReplicationStatus replicationStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
if (replicationStatus != null) {
boolean bl = published = replicationStatus.isDelivered() || replicationStatus.isActivated();
if (published) {
long lastPublished = replicationStatus.getLastPublished().getTimeInMillis();
boolean bl2 = outdated = lastPublished < reference.getLastModified();
}
}
if (published && !outdated) continue;
this.replicator.replicate(session, ReplicationActionType.ACTIVATE, reference.getResource().getPath());
}
}
if (isDeep) {
Iterator children = page.listChildren();
while (children.hasNext()) {
this.publish(session, (Page)children.next(), true);
}
}
}
protected void bindReferenceProviders(ReferenceProvider referenceProvider) {
this.referenceProviders.add(referenceProvider);
}
protected void unbindReferenceProviders(ReferenceProvider referenceProvider) {
this.referenceProviders.remove((Object)referenceProvider);
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
}