1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.distribution.internal.job; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
import java.util.concurrent.locks.Condition; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.ObjectUtils; |
28 |
|
import org.xwiki.component.manager.ComponentLookupException; |
29 |
|
import org.xwiki.extension.distribution.internal.DistributionManager; |
30 |
|
import org.xwiki.extension.distribution.internal.job.step.DistributionStep; |
31 |
|
import org.xwiki.extension.distribution.internal.job.step.DistributionStep.State; |
32 |
|
import org.xwiki.extension.distribution.internal.job.step.ReportDistributionStep; |
33 |
|
import org.xwiki.extension.distribution.internal.job.step.WelcomeDistributionStep; |
34 |
|
import org.xwiki.job.AbstractJob; |
35 |
|
import org.xwiki.job.event.status.JobStatus; |
36 |
|
|
37 |
|
|
38 |
|
@version |
39 |
|
@since |
40 |
|
|
|
|
| 65.4% |
Uncovered Elements: 36 (104) |
Complexity: 28 |
Complexity Density: 0.38 |
|
41 |
|
public abstract class AbstractDistributionJob<R extends DistributionRequest, S extends DistributionJobStatus<R>> |
42 |
|
extends AbstractJob<R, S> implements DistributionJob |
43 |
|
{ |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@Inject |
48 |
|
protected DistributionManager distributionManager; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
protected final Condition readyCondition = lock.newCondition(); |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
15 |
@Override... |
56 |
|
public String getType() |
57 |
|
{ |
58 |
15 |
return "distribution"; |
59 |
|
} |
60 |
|
|
61 |
|
protected abstract S createNewDistributionStatus(R request, List<DistributionStep> steps); |
62 |
|
|
63 |
|
protected abstract List<DistributionStep> createSteps(); |
64 |
|
|
|
|
| 87.5% |
Uncovered Elements: 3 (24) |
Complexity: 7 |
Complexity Density: 0.35 |
|
65 |
3 |
@Override... |
66 |
|
protected S createNewStatus(R request) |
67 |
|
{ |
68 |
3 |
List<DistributionStep> steps = createSteps(); |
69 |
|
|
70 |
|
|
71 |
3 |
try { |
72 |
3 |
DistributionStep welcomeStep = |
73 |
|
this.componentManager |
74 |
|
.<DistributionStep> getInstance(DistributionStep.class, WelcomeDistributionStep.ID); |
75 |
3 |
welcomeStep.setState(State.COMPLETED); |
76 |
|
|
77 |
3 |
steps.add(0, welcomeStep); |
78 |
|
} catch (ComponentLookupException e1) { |
79 |
0 |
this.logger.error("Failed to get step instance for id [{}]", WelcomeDistributionStep.ID); |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
3 |
try { |
84 |
3 |
DistributionStep welcomeStep = |
85 |
|
this.componentManager.<DistributionStep> getInstance(DistributionStep.class, ReportDistributionStep.ID); |
86 |
3 |
welcomeStep.setState(State.COMPLETED); |
87 |
|
|
88 |
3 |
steps.add(welcomeStep); |
89 |
|
} catch (ComponentLookupException e1) { |
90 |
0 |
this.logger.error("Failed to get step instance for id [{}]", ReportDistributionStep.ID); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
3 |
S status = createNewDistributionStatus(request, steps); |
96 |
|
|
97 |
3 |
if (this.distributionManager.getDistributionExtension() != null) { |
98 |
3 |
DistributionJobStatus< ? > previousStatus = getPreviousStatus(); |
99 |
|
|
100 |
3 |
if (previousStatus != null |
101 |
|
&& previousStatus.getDistributionExtension() != null |
102 |
|
&& !ObjectUtils.equals(previousStatus.getDistributionExtension(), |
103 |
|
this.distributionManager.getDistributionExtension())) { |
104 |
2 |
status.setPreviousDistributionExtension(previousStatus.getDistributionExtension()); |
105 |
2 |
status.setPreviousDistributionExtensionUI(previousStatus.getDistributionExtensionUI()); |
106 |
|
} |
107 |
|
|
108 |
3 |
status.setDistributionExtension(this.distributionManager.getDistributionExtension().getId()); |
109 |
3 |
status.setDistributionExtensionUI(getUIExtensionId()); |
110 |
|
} |
111 |
|
|
112 |
3 |
return status; |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
15 |
protected S getDistributionJobStatus()... |
116 |
|
{ |
117 |
15 |
return getStatus(); |
118 |
|
} |
119 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
120 |
6 |
protected DistributionStep getStep(List<DistributionStep> steps, String stepId)... |
121 |
|
{ |
122 |
6 |
for (DistributionStep step : steps) { |
123 |
15 |
if (step.getId().equals(stepId)) { |
124 |
6 |
return step; |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
0 |
return null; |
129 |
|
} |
130 |
|
|
|
|
| 40.4% |
Uncovered Elements: 28 (47) |
Complexity: 10 |
Complexity Density: 0.3 |
|
131 |
3 |
@Override... |
132 |
|
protected void runInternal() throws Exception |
133 |
|
{ |
134 |
3 |
List<DistributionStep> steps = getDistributionJobStatus().getSteps(); |
135 |
|
|
136 |
3 |
this.progressManager.pushLevelProgress(steps.size(), this); |
137 |
|
|
138 |
|
|
139 |
3 |
WelcomeDistributionStep welcomeStep = (WelcomeDistributionStep) getStep(steps, WelcomeDistributionStep.ID); |
140 |
3 |
ReportDistributionStep reportStep = (ReportDistributionStep) getStep(steps, ReportDistributionStep.ID); |
141 |
|
|
142 |
3 |
for (DistributionStep step : steps) { |
143 |
12 |
step.initialize(this); |
144 |
|
|
145 |
|
|
146 |
12 |
if (step.getState() == null) { |
147 |
0 |
if (welcomeStep != null) { |
148 |
0 |
welcomeStep.setState(null); |
149 |
|
} |
150 |
0 |
if (reportStep != null) { |
151 |
0 |
reportStep.setState(null); |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
3 |
try { |
158 |
15 |
for (int index = 0; index < steps.size(); ++index) { |
159 |
12 |
this.progressManager.startStep(this); |
160 |
|
|
161 |
12 |
getDistributionJobStatus().setCurrentStateIndex(index); |
162 |
|
|
163 |
12 |
DistributionStep step = steps.get(index); |
164 |
|
|
165 |
12 |
step.prepare(); |
166 |
|
|
167 |
12 |
if (step.getState() == null) { |
168 |
0 |
DistributionQuestion question = new DistributionQuestion(step); |
169 |
|
|
170 |
|
|
171 |
0 |
signalReady(); |
172 |
0 |
getStatus().ask(question); |
173 |
|
|
174 |
0 |
if (question.getAction() != null) { |
175 |
0 |
switch (question.getAction()) { |
176 |
0 |
case CANCEL: |
177 |
0 |
for (; index < steps.size(); ++index) { |
178 |
0 |
steps.get(index).setState(DistributionStep.State.CANCELED); |
179 |
|
} |
180 |
0 |
case SKIP: |
181 |
0 |
index = steps.size() - 1; |
182 |
0 |
break; |
183 |
0 |
default: |
184 |
0 |
break; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
0 |
this.store.storeAsync(this.status); |
190 |
|
} |
191 |
|
} |
192 |
|
} finally { |
193 |
3 |
this.progressManager.popLevelProgress(this); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0 |
@Override... |
200 |
|
public DistributionStep getCurrentStep() |
201 |
|
{ |
202 |
0 |
return getStatus().getCurrentStep(); |
203 |
|
} |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
205 |
3 |
@Override... |
206 |
|
protected void jobFinished(Throwable exception) |
207 |
|
{ |
208 |
3 |
super.jobFinished(exception); |
209 |
|
|
210 |
3 |
signalReady(); |
211 |
|
} |
212 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
213 |
3 |
private void signalReady()... |
214 |
|
{ |
215 |
3 |
this.lock.lock(); |
216 |
|
|
217 |
3 |
try { |
218 |
3 |
this.readyCondition.signalAll(); |
219 |
|
} finally { |
220 |
3 |
this.lock.unlock(); |
221 |
|
} |
222 |
|
} |
223 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0.57 |
|
224 |
3 |
@Override... |
225 |
|
public void awaitReady() |
226 |
|
{ |
227 |
3 |
if (getStatus() == null || getStatus().getState() == JobStatus.State.RUNNING) { |
228 |
3 |
try { |
229 |
3 |
this.lock.lockInterruptibly(); |
230 |
|
|
231 |
3 |
try { |
232 |
3 |
this.readyCondition.await(); |
233 |
|
} finally { |
234 |
3 |
this.lock.unlock(); |
235 |
|
} |
236 |
|
} catch (InterruptedException e) { |
237 |
0 |
this.logger.warn("The distribution job has been interrupted"); |
238 |
|
} |
239 |
|
} |
240 |
|
} |
241 |
|
} |