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