Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 07:28:57 UTC
../../../../../img/srcFileCovDistChart4.png 94% of files have more coverage
73   267   53   2.35
34   178   0.73   31
31     1.71  
1    
 
  InternalServiceJobImpl       Line # 35 73 53 37.7% 0.3768116
 
  (111)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program 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
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.bo.job;
17   
18    import java.util.ArrayList;
19    import java.util.List;
20   
21    import org.qedeq.base.trace.Trace;
22    import org.qedeq.kernel.bo.common.ModuleServiceCall;
23    import org.qedeq.kernel.bo.common.QedeqBo;
24    import org.qedeq.kernel.bo.common.QedeqBoSet;
25    import org.qedeq.kernel.bo.common.ServiceJob;
26    import org.qedeq.kernel.bo.module.InternalModuleServiceCall;
27    import org.qedeq.kernel.bo.module.InternalServiceJob;
28    import org.qedeq.kernel.bo.module.ModuleArbiter;
29   
30    /**
31    * Process info for a kernel service.
32    *
33    * @author Michael Meyling
34    */
 
35    public class InternalServiceJobImpl implements InternalServiceJob {
36   
37    /** This class. */
38    private static final Class CLASS = InternalServiceJobImpl.class;
39   
40    /** Counter for each service process. */
41    private static long globalCounter;
42   
43    /** The service call the process currently works for. */
44    private InternalModuleServiceCall call;
45   
46    /** The thread the service is done within. */
47    private final Thread thread;
48   
49    /** Start time for process. */
50    private long start;
51   
52    /** End time for process. */
53    private long stop;
54   
55    /** State for process. -1 = interrupted, 0 = running, 1 = success, 2 failure. */
56    private int state;
57   
58    /** Action name. */
59    private final String actionName;
60   
61    /** Percentage of currently running plugin execution. */
62    private double executionPercentage;
63   
64    /** Percentage of currently running plugin execution. */
65    private String executionActionDescription = "not yet started";
66   
67    /** Is this process blocked? */
68    private boolean blocked;
69   
70    /** Process id. */
71    private final long id;
72   
73    /** This arbiter can lock and unlock modules. */
74    private ModuleArbiter arbiter;
75   
76    /**
77    * A new service process within the current thread.
78    *
79    * @param arbiter Remember module arbiter.
80    * @param actionName Main process purpose.
81    */
 
82  891 toggle public InternalServiceJobImpl(final ModuleArbiter arbiter, final String actionName) {
83  891 this.id = inc();
84  891 this.thread = Thread.currentThread();
85  891 this.call = null;
86  891 this.arbiter = arbiter;
87  891 this.actionName = actionName;
88  891 start();
89    }
90   
 
91  891 toggle private synchronized long inc() {
92  891 return globalCounter++;
93    }
94   
 
95  2715 toggle public synchronized void setInternalServiceCall(final InternalModuleServiceCall call) {
96  2715 this.call = call;
97    }
98   
 
99  1595 toggle public synchronized ModuleServiceCall getModuleServiceCall() {
100  1595 return call;
101    }
102   
 
103  1761 toggle public synchronized InternalModuleServiceCall getInternalServiceCall() {
104  1761 return call;
105    }
106   
 
107  1589 toggle public synchronized Thread getThread() {
108  1589 return thread;
109    }
110   
 
111  0 toggle public synchronized String getQedeqName() {
112  0 if (call != null) {
113  0 return call.getQedeq().getName();
114    }
115  0 return "";
116    }
117   
 
118  0 toggle public synchronized String getQedeqUrl() {
119  0 if (call != null) {
120  0 return call.getQedeq().getUrl();
121    }
122  0 return "";
123    }
124   
 
125  0 toggle public synchronized long getStart() {
126  0 return start;
127    }
128   
 
129  0 toggle public synchronized long getStop() {
130  0 return stop;
131    }
132   
 
133  891 toggle private synchronized void start() {
134  891 start = System.currentTimeMillis();
135  891 executionActionDescription = "started";
136    }
137   
 
138  578 toggle private synchronized void stop() {
139  578 stop = System.currentTimeMillis();
140    }
141   
 
142  578 toggle public synchronized void setSuccessState() {
143  578 if (isRunning()) {
144  578 state = 1;
145  578 stop();
146  578 executionActionDescription = "finished";
147  578 executionPercentage = 100;
148    }
149    }
150   
 
151  0 toggle public synchronized void setInterruptedState() {
152  0 if (isRunning()) {
153  0 state = -1;
154  0 stop();
155    }
156    }
157   
 
158  0 toggle public synchronized void setFailureState() {
159  0 if (isRunning()) {
160  0 state = 2;
161  0 stop();
162    }
163    }
164   
 
165  3293 toggle public synchronized boolean isRunning() {
166  3293 if (state == 0) {
167  3293 if (!thread.isAlive()) {
168  0 Trace.fatal(CLASS, this, "isRunning()", "Thread has unexpectly died",
169    new RuntimeException());
170  0 state = -1;
171  0 stop();
172  0 return false;
173    }
174  3293 return true;
175    }
176  0 return false;
177    }
178   
 
179  0 toggle public synchronized boolean isBlocked() {
180  0 if (isRunning()) {
181  0 return blocked;
182    }
183  0 return false;
184    }
185   
 
186  5362 toggle public synchronized void setBlocked(final boolean blocked) {
187  5362 this.blocked = blocked;
188    }
189   
 
190  0 toggle public synchronized boolean wasSuccess() {
191  0 return state == 1;
192    }
193   
 
194  0 toggle public synchronized boolean wasFailure() {
195  0 return state == -1 || state == 2;
196    }
197   
 
198  0 toggle public synchronized boolean wasInterrupted() {
199  0 return state == -1;
200    }
201   
202   
 
203  851 toggle public synchronized void interrupt() {
204  851 thread.interrupt();
205    }
206   
 
207  0 toggle public synchronized double getExecutionPercentage() {
208  0 if (isRunning() || isBlocked()) {
209  0 if (call != null) {
210  0 executionPercentage = call.getExecutionPercentage();
211    }
212    }
213  0 return executionPercentage;
214    }
215   
 
216  0 toggle public synchronized String getActionName() {
217  0 return actionName;
218    }
219   
 
220  0 toggle public synchronized String getExecutionActionDescription() {
221  0 if (isRunning() || isBlocked()) {
222  0 if (call != null) {
223  0 executionActionDescription = call.getLocation();
224    }
225    }
226  0 return executionActionDescription;
227    }
228   
 
229  0 toggle public synchronized QedeqBoSet getBlockedModules() {
230  0 return arbiter.getBlockedModules(this);
231    }
232   
233   
 
234  6356 toggle public long getId() {
235  6356 return id;
236    }
237   
 
238  0 toggle public int hashCode() {
239  0 return (int) id;
240    }
241   
 
242  2681 toggle public boolean equals(final Object obj) {
243  2681 return 0 == compareTo(obj);
244    }
245   
 
246  2681 toggle public int compareTo(final Object o) {
247  2681 if (!(o instanceof ServiceJob)) {
248  1092 return -1;
249    }
250  1589 final ServiceJob s = (ServiceJob) o;
251  1589 return (getId() < s.getId() ? -1 : (getId() == s.getId() ? 0 : 1));
252    }
253   
 
254  0 toggle public synchronized QedeqBo[] getCurrentlyProcessedModules() {
255  0 final List result = new ArrayList();
256  0 ModuleServiceCall parent = call;
257  0 while (parent != null) {
258  0 if (parent.getQedeq() != null && (result.size() == 0
259    || (result.size() > 0 && !parent.getQedeq().equals(result.get(0))))) {
260  0 result.add(0, parent.getQedeq());
261    }
262  0 parent = parent.getParentServiceCall();
263    }
264  0 return (QedeqBo[]) result.toArray(new QedeqBo[]{});
265    }
266   
267    }