WorkflowPageMove.java
10.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowService
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.model.WorkflowModel
* javax.jcr.Credentials
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.SimpleCredentials
* 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.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.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
* org.apache.sling.jcr.api.SlingRepository
* 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.workflow.impl;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Credentials;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
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.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.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.jcr.api.SlingRepository;
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
@Service(value={EventHandler.class})
@Properties(value={@Property(name="event.topics", value={"com/day/cq/wcm/workflow/req/for/move"}), @Property(name="event.filter", value={"(!(event.application=*))"})})
public class WorkflowPageMove
implements EventHandler,
JobConsumer {
@Property(label="minThreadPoolSize", description="Minimum Thread pool size", intValue={5})
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 MAXUIMUM_THREAD_POOL_SIZE = "maxThreadPoolSize";
private int maxThreadPoolSize;
@Reference
private ThreadPoolManager threadPoolManager;
private ThreadPool threadPool;
public static final String REQUEST_FOR_MOVING_MODEL = "/etc/workflow/models/request_to_complete_move_operation/jcr:content/model";
public static final String EVENT_TOPIC = "com/day/cq/wcm/workflow/req/for/move";
@Reference
private SlingRepository repository;
@Reference
private WorkflowService workflowService;
private static final String TYPE_JCR_PATH = "JCR_PATH";
private static final Logger log = LoggerFactory.getLogger(WorkflowPageMove.class);
@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 context) {
if (this.threadPool != null) {
this.threadPoolManager.release(this.threadPool);
this.threadPoolManager = null;
this.threadPool = null;
}
}
public void handleEvent(final Event event) {
this.threadPool.execute(new Runnable(){
@Override
public void run() {
String topic = event.getTopic();
String userId = (String)event.getProperty("userId");
String path = (String)event.getProperty("path");
String srcPath = (String)event.getProperty("srcPath");
Object repType = event.getProperty("replicationType");
Object publishReferences = event.getProperty("publishReferences");
WorkflowPageMove.this.processPageMove(topic, userId, path, srcPath, repType, publishReferences);
}
});
}
public JobConsumer.JobResult process(Job job) {
String topic = job.getTopic();
String userId = (String)job.getProperty("userId");
String path = (String)job.getProperty("path");
String srcPath = (String)job.getProperty("srcPath");
Object repType = job.getProperty("replicationType");
Object publishReferences = job.getProperty("publishReferences");
this.processPageMove(topic, userId, path, srcPath, repType, publishReferences);
return JobConsumer.JobResult.OK;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void processPageMove(String topic, String userId, String path, String srcPath, Object repType, Object publishReferences) {
block9 : {
WorkflowSession adminWfSession = null;
WorkflowSession userWfSession = null;
try {
if (!topic.equals("com/day/cq/wcm/workflow/req/for/move")) break block9;
adminWfSession = this.getWorkflowSession();
userWfSession = this.getWorkflowSession(userId, adminWfSession);
WorkflowModel model = adminWfSession.getModel("/etc/workflow/models/request_to_complete_move_operation/jcr:content/model");
WorkflowData wfData = userWfSession.newWorkflowData("JCR_PATH", (Object)path);
HashMap<String, Object> wfMetaData = new HashMap<String, Object>();
wfMetaData.put("srcPath", srcPath);
wfMetaData.put("replicationType", repType);
wfMetaData.put("publishReferences", publishReferences);
try {
userWfSession.startWorkflow(model, wfData, wfMetaData);
}
catch (WorkflowException e) {
log.warn("Unable to start workflow '{}' as user '{}'. Starting as admin. (reason: {})", new Object[]{model.getId(), userId, String.valueOf(e.getCause())});
adminWfSession.startWorkflow(model, wfData, wfMetaData);
}
}
catch (WorkflowException we) {
log.error("Cannot start request for deletion workflow /etc/workflow/models/request_to_complete_move_operation/jcr:content/model " + we.getMessage(), (Throwable)we);
}
finally {
if (userWfSession != null) {
userWfSession.logout();
}
if (adminWfSession != null) {
adminWfSession.logout();
}
}
}
}
private WorkflowSession getWorkflowSession() {
try {
return this.workflowService.getWorkflowSession(this.repository.loginAdministrative(null));
}
catch (RepositoryException re) {
log.error("Cannot login into repository", (Throwable)re);
return null;
}
}
private WorkflowSession getWorkflowSession(String userId, WorkflowSession adminSession) {
try {
Session userSession = adminSession.getSession().impersonate((Credentials)new SimpleCredentials(userId, new char[0]));
return this.workflowService.getWorkflowSession(userSession);
}
catch (RepositoryException re) {
log.error("Cannot login into repository", (Throwable)re);
return null;
}
}
protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
this.threadPoolManager = threadPoolManager;
}
protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
if (this.threadPoolManager == threadPoolManager) {
this.threadPoolManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindWorkflowService(WorkflowService workflowService) {
this.workflowService = workflowService;
}
protected void unbindWorkflowService(WorkflowService workflowService) {
if (this.workflowService == workflowService) {
this.workflowService = null;
}
}
}