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

File DefaultJobProgress.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

36
59
16
1
263
154
36
0.61
3.69
16
2.25

Classes

Class Line # Actions
DefaultJobProgress 41 59 0% 36 12
0.891891989.2%
 

Contributing tests

This file is covered by 184 tests. .

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.job.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27    import org.xwiki.job.event.status.EndStepProgressEvent;
28    import org.xwiki.job.event.status.JobProgress;
29    import org.xwiki.job.event.status.PopLevelProgressEvent;
30    import org.xwiki.job.event.status.PushLevelProgressEvent;
31    import org.xwiki.job.event.status.StartStepProgressEvent;
32    import org.xwiki.job.event.status.StepProgressEvent;
33    import org.xwiki.logging.Message;
34    import org.xwiki.observation.EventListener;
35    import org.xwiki.observation.event.Event;
36   
37    /**
38    * @version $Id: a8dfc55907b0443ac833b9bc6741ef9714c791aa $
39    * @since 4.0M1
40    */
 
41    public class DefaultJobProgress implements EventListener, JobProgress
42    {
43    /**
44    * The object used to log messages.
45    */
46    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJobProgress.class);
47   
48    /**
49    * Listened events.
50    */
51    private static final List<Event> EVENTS = Arrays.<Event>asList(new PushLevelProgressEvent(),
52    PopLevelProgressEvent.INSTANCE, StepProgressEvent.INSTANCE, StartStepProgressEvent.INSTANCE,
53    EndStepProgressEvent.INSTANCE);
54   
55    private final String listenerName;
56   
57    private final DefaultJobProgressStep rootStep;
58   
59    private transient DefaultJobProgressStep currentStep;
60   
61    /**
62    * Default constructor.
63    */
 
64  668 toggle public DefaultJobProgress()
65    {
66  668 this(null);
67    }
68   
69    /**
70    * @param name the name associated to the job progress
71    */
 
72  668 toggle public DefaultJobProgress(String name)
73    {
74  668 this.listenerName = name != null ? name : getClass().getName() + '_' + System.identityHashCode(this);
75   
76  668 this.rootStep =
77    new DefaultJobProgressStep(new Message("job.progress", "Progress with name [{}]", name), null, null);
78  668 this.currentStep = this.rootStep;
79    }
80   
81    // EventListener
82   
 
83  5430 toggle @Override
84    public String getName()
85    {
86  5430 return this.listenerName;
87    }
88   
 
89  553 toggle @Override
90    public List<Event> getEvents()
91    {
92  553 return EVENTS;
93    }
94   
 
95  14236 toggle @Override
96    public void onEvent(Event event, Object source, Object message)
97    {
98  14236 if (event instanceof PushLevelProgressEvent) {
99  2636 onPushLevelProgress(((PushLevelProgressEvent) event).getSteps(), source);
100  11600 } else if (event instanceof PopLevelProgressEvent) {
101  2628 onPopLevelProgress(source);
102  8972 } else if (event instanceof StartStepProgressEvent) {
103  7806 onStartStepProgress((Message) message, source);
104  1166 } else if (event instanceof EndStepProgressEvent) {
105  1161 onEndStepProgress(source);
106  5 } else if (event instanceof StepProgressEvent) {
107  5 onStepProgress(source);
108    }
109    }
110   
111    /**
112    * Adds a new level to the progress stack.
113    *
114    * @param event the event that was fired
115    */
 
116  3042 toggle private void onPushLevelProgress(int steps, Object source)
117    {
118  3042 if (this.currentStep.isLevelFinished()) {
119    // If current step is done move to next one
120  79 this.currentStep = this.currentStep.getParent().nextStep(null, source);
121    }
122   
123    // Add level
124  3042 this.currentStep = this.currentStep.addLevel(steps, source);
125    }
126   
127    /**
128    * Close current step.
129    */
 
130  1161 toggle private void onEndStepProgress(Object source)
131    {
132    // Try to find the right step based on the source
133  1161 DefaultJobProgressStep step = findStep(this.currentStep, source);
134   
135  1161 if (step == null) {
136  0 LOGGER.warn("Could not find any matching step for source [{}]. Ignoring EndStepProgress.",
137    source.toString());
138   
139  0 return;
140    }
141   
142  1161 this.currentStep = step;
143   
144  1161 this.currentStep.finish();
145    }
146   
 
147  7811 toggle private void onStartStepProgress(Message message, Object source)
148    {
149  7811 if (this.currentStep.getParent() == null) {
150    // If we are still on root node, create a level
151  5 this.currentStep = this.currentStep.addLevel(source);
152  7806 } else if (!this.currentStep.isLevelFinished() && this.currentStep.source != source) {
153    // If current step is from a different source add a level
154  406 onPushLevelProgress(0, source);
155    }
156   
157    // Start a new step
158  7811 this.currentStep = this.currentStep.getParent().nextStep(message, source);
159    }
160   
161    /**
162    * Move progress to next step.
163    *
164    * @deprecated since 7.1M2, use {@link #onStartStepProgress(Message)} instead
165    */
 
166  5 toggle @Deprecated
167    private void onStepProgress(Object source)
168    {
169  5 onStartStepProgress(null, source);
170   
171    // if there is only one step close it and move to the next one
172  5 if (this.currentStep.getParent().getChildren().size() == 1) {
173  3 this.currentStep = this.currentStep.getParent().nextStep(null, source);
174    }
175    }
176   
177    /**
178    * Called when a {@link PopLevelProgressEvent} is fired.
179    */
 
180  2628 toggle private void onPopLevelProgress(Object source)
181    {
182  2628 DefaultJobProgressStep parent = this.currentStep.getParent();
183   
184  2628 if (parent == null) {
185  0 LOGGER.warn("PopLevelProgressEvent was fired too many times. Don't forget "
186    + "to match each PopLevelProgressEvent with a PushLevelProgressEvent.");
187   
188  0 return;
189    }
190   
191    // Try to find the right level based on the source
192  2628 DefaultJobProgressStep level = findLevel(this.currentStep.getParent(), source);
193   
194  2628 if (level == null) {
195  1 LOGGER.warn("Could not find any matching step level for source [{}]. Ignoring PopLevelProgressEvent.",
196    source.toString());
197   
198  1 return;
199    }
200   
201    // Move to parent step
202  2627 this.currentStep = level;
203   
204    // Close the level
205  2627 this.currentStep.finishLevel();
206    }
207   
 
208  1161 toggle private DefaultJobProgressStep findStep(DefaultJobProgressStep step, Object source)
209    {
210  1161 DefaultJobProgressStep matchingStep = step;
211   
212  1161 do {
213  1285 if (matchingStep.source == source) {
214  1161 return matchingStep;
215    }
216   
217  124 matchingStep = matchingStep.getParent();
218  124 } while (matchingStep != null);
219   
220  0 return null;
221    }
222   
 
223  2628 toggle private DefaultJobProgressStep findLevel(DefaultJobProgressStep step, Object levelSource)
224    {
225  2628 DefaultJobProgressStep matchingStep = step;
226   
227  2628 do {
228  2912 if (levelSource == null || matchingStep.levelSource == levelSource) {
229  2627 return matchingStep;
230    }
231   
232  285 matchingStep = matchingStep.getParent();
233  285 } while (matchingStep != null);
234   
235  1 return null;
236    }
237   
238    // JobProgress
239   
 
240  207 toggle @Override
241    public double getOffset()
242    {
243  207 return this.rootStep.getOffset();
244    }
245   
 
246  69 toggle @Override
247    public double getCurrentLevelOffset()
248    {
249  69 return this.currentStep.getParent() != null ? this.currentStep.getParent().getOffset() : getOffset();
250    }
251   
 
252  597 toggle @Override
253    public DefaultJobProgressStep getRootStep()
254    {
255  597 return this.rootStep;
256    }
257   
 
258  0 toggle @Override
259    public DefaultJobProgressStep getCurrentStep()
260    {
261  0 return this.currentStep;
262    }
263    }