001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.bo.service;
017
018 import org.qedeq.base.io.Parameters;
019 import org.qedeq.base.trace.Trace;
020 import org.qedeq.kernel.bo.common.PluginExecutor;
021 import org.qedeq.kernel.bo.common.QedeqBo;
022 import org.qedeq.kernel.bo.common.ServiceProcess;
023 import org.qedeq.kernel.bo.module.KernelQedeqBo;
024 import org.qedeq.kernel.se.common.Plugin;
025
026 /**
027 * Process info for a service.
028 *
029 * @author Michael Meyling
030 */
031 public class ServiceProcessImpl implements ServiceProcess {
032
033 /** This class. */
034 private static final Class CLASS = ServiceProcessImpl.class;
035
036 /** The service the thread works for. */
037 private final Plugin service;
038
039 /** The thread the service is done within. */
040 private final Thread thread;
041
042 /** Some important parameters for the service. For example QEDEQ module address. */
043 private final Parameters parameters;
044
045 /** Start time for process. */
046 private long start;
047
048 /** End time for process. */
049 private long stop;
050
051 /** State for process. 0 = running, 1 = success, -1 failure. */
052 private int state;
053
054 /** Is this process blocked? */
055 private boolean blocked;
056
057 /** QEDEQ module the process is working on. */
058 private KernelQedeqBo qedeq;
059
060 /** Percentage of currently running plugin execution. */
061 private double executionPercentage = 0;
062
063 /** Percentage of currently running plugin execution. */
064 private String executionActionDescription = "not yet started";
065
066 /** Created execution object. Might be <code>null</code>. */
067 private PluginExecutor executor;
068
069 /**
070 * A new service process.
071 *
072 * @param service This service is executed.
073 * @param qedeq This QEDEQ module we work on.
074 * @param thread The process the service is executed within.
075 * @param parameters Interesting process parameters (e.g. QEDEQ module).
076 */
077 public ServiceProcessImpl(final Plugin service, final Thread thread, final KernelQedeqBo qedeq,
078 final Parameters parameters) {
079 this.service = service;
080 this.thread = thread;
081 this.qedeq = qedeq;
082 this.parameters = parameters;
083 if (!thread.isAlive()) {
084 throw new RuntimeException("thread is already dead");
085 }
086 start();
087 }
088
089 /**
090 * A new service process within the current thread.
091 *
092 * @param service This service is executed.
093 * @param qedeq Module we work on.
094 * @param parameters Interesting process parameters (e.g. QEDEQ module).
095 */
096 public ServiceProcessImpl(final Plugin service, final KernelQedeqBo qedeq, final Parameters parameters) {
097 this(service, Thread.currentThread(), qedeq, parameters);
098 }
099
100 /* (non-Javadoc)
101 * @see org.qedeq.kernel.bo.ServiceProcess#getService()
102 */
103 public synchronized Plugin getService() {
104 return service;
105 }
106
107 /* (non-Javadoc)
108 * @see org.qedeq.kernel.bo.ServiceProcess#getThread()
109 */
110 public synchronized Thread getThread() {
111 return thread;
112 }
113
114 /* (non-Javadoc)
115 * @see org.qedeq.kernel.bo.ServiceProcess#getQedeq()
116 */
117 public synchronized QedeqBo getQedeq() {
118 return qedeq;
119 }
120
121 /* (non-Javadoc)
122 * @see org.qedeq.kernel.bo.ServiceProcess#getParameters()
123 */
124 public synchronized Parameters getParameters() {
125 return parameters;
126 }
127
128 /* (non-Javadoc)
129 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutor()
130 */
131 public synchronized PluginExecutor getExecutor() {
132 return executor;
133 }
134
135 /* (non-Javadoc)
136 * @see org.qedeq.kernel.bo.ServiceProcess#setExecutor(org.qedeq.kernel.bo.module.PluginExecutor)
137 */
138 public synchronized void setExecutor(final PluginExecutor executor) {
139 this.executor = executor;
140 }
141
142 /* (non-Javadoc)
143 * @see org.qedeq.kernel.bo.ServiceProcess#getParameterString()
144 */
145 public synchronized String getParameterString() {
146 return parameters.getParameterString();
147 }
148
149 /* (non-Javadoc)
150 * @see org.qedeq.kernel.bo.ServiceProcess#getStart()
151 */
152 public synchronized long getStart() {
153 return start;
154 }
155
156 /* (non-Javadoc)
157 * @see org.qedeq.kernel.bo.ServiceProcess#getStop()
158 */
159 public synchronized long getStop() {
160 return stop;
161 }
162
163 private synchronized void start() {
164 start = System.currentTimeMillis();
165 executionActionDescription = "started";
166 }
167
168 private synchronized void stop() {
169 stop = System.currentTimeMillis();
170 }
171
172 /* (non-Javadoc)
173 * @see org.qedeq.kernel.bo.ServiceProcess#setSuccessState()
174 */
175 public synchronized void setSuccessState() {
176 if (isRunning()) {
177 state = 1;
178 stop();
179 executionActionDescription = "finished";
180 executionPercentage = 100;
181 }
182 }
183
184 /* (non-Javadoc)
185 * @see org.qedeq.kernel.bo.ServiceProcess#setFailureState()
186 */
187 public synchronized void setFailureState() {
188 if (isRunning()) {
189 state = -1;
190 stop();
191 }
192 }
193
194 /* (non-Javadoc)
195 * @see org.qedeq.kernel.bo.ServiceProcess#isRunning()
196 */
197 public synchronized boolean isRunning() {
198 if (state == 0) {
199 if (!thread.isAlive()) {
200 Trace.fatal(CLASS, this, "isRunning()", "Thread has unexpectly died",
201 new RuntimeException());
202 setFailureState();
203 return false;
204 }
205 return true;
206 }
207 return false;
208 }
209
210 /* (non-Javadoc)
211 * @see org.qedeq.kernel.bo.ServiceProcess#isBlocked()
212 */
213 public synchronized boolean isBlocked() {
214 if (isRunning()) {
215 return blocked;
216 }
217 return false;
218 }
219
220 /* (non-Javadoc)
221 * @see org.qedeq.kernel.bo.ServiceProcess#setBlocked(boolean)
222 */
223 public synchronized void setBlocked(final boolean blocked) {
224 this.blocked = blocked;
225 }
226
227 /* (non-Javadoc)
228 * @see org.qedeq.kernel.bo.ServiceProcess#wasSuccess()
229 */
230 public synchronized boolean wasSuccess() {
231 return state == 1;
232 }
233
234 /* (non-Javadoc)
235 * @see org.qedeq.kernel.bo.ServiceProcess#wasFailure()
236 */
237 public synchronized boolean wasFailure() {
238 return state == -1;
239 }
240
241 /* (non-Javadoc)
242 * @see org.qedeq.kernel.bo.ServiceProcess#interrupt()
243 */
244 public synchronized void interrupt() {
245 thread.interrupt();
246 setFailureState();
247 }
248
249 /* (non-Javadoc)
250 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutionPercentage()
251 */
252 public synchronized double getExecutionPercentage() {
253 if (isRunning() || isBlocked()) {
254 if (executor != null) {
255 executionPercentage = executor.getExecutionPercentage();
256 }
257 }
258 return executionPercentage;
259 }
260
261 /* (non-Javadoc)
262 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutionActionDescription()
263 */
264 public synchronized String getExecutionActionDescription() {
265 if (isRunning() || isBlocked()) {
266 if (executor != null) {
267 executionActionDescription = executor.getLocationDescription();
268 }
269 }
270 return executionActionDescription;
271 }
272
273 }
|