1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.distribution.internal.job

File AbstractDistributionJob.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

22
73
9
1
241
160
28
0.38
8.11
9
3.11

Classes

Class Line # Actions
AbstractDistributionJob 41 73 0% 28 36
0.6538461465.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 992f7bfcfe88d79b0eaf64aaec401a9933d6b5eb $
39    * @since 5.0M1
40    */
 
41    public abstract class AbstractDistributionJob<R extends DistributionRequest, S extends DistributionJobStatus<R>>
42    extends AbstractJob<R, S> implements DistributionJob
43    {
44    /**
45    * The component used to get information about the current distribution.
46    */
47    @Inject
48    protected DistributionManager distributionManager;
49   
50    /**
51    * Condition to wait for ready state.
52    */
53    protected final Condition readyCondition = lock.newCondition();
54   
 
55  15 toggle @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   
 
65  3 toggle @Override
66    protected S createNewStatus(R request)
67    {
68  3 List<DistributionStep> steps = createSteps();
69   
70    // Add Welcome step
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    // Add Report step
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    // Create status
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   
 
115  15 toggle protected S getDistributionJobStatus()
116    {
117  15 return getStatus();
118    }
119   
 
120  6 toggle 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   
 
131  3 toggle @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    // Initialize steps
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    // Enable Welcome step if one of the steps is enabled
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    // Execute steps
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    // Waiting to start
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    // Save the status so that we remember the answer even if the DW is stopped before the end
189  0 this.store.storeAsync(this.status);
190    }
191    }
192    } finally {
193  3 this.progressManager.popLevelProgress(this);
194    }
195    }
196   
197    // DistributionJob
198   
 
199  0 toggle @Override
200    public DistributionStep getCurrentStep()
201    {
202  0 return getStatus().getCurrentStep();
203    }
204   
 
205  3 toggle @Override
206    protected void jobFinished(Throwable exception)
207    {
208  3 super.jobFinished(exception);
209   
210  3 signalReady();
211    }
212   
 
213  3 toggle 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   
 
224  3 toggle @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    }