001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, 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 java.util.Iterator;
019 import java.util.Map;
020
021 import org.qedeq.base.trace.Trace;
022 import org.qedeq.kernel.bo.common.PluginExecutor;
023 import org.qedeq.kernel.bo.common.QedeqBo;
024 import org.qedeq.kernel.bo.common.ServiceProcess;
025 import org.qedeq.kernel.bo.module.KernelQedeqBo;
026 import org.qedeq.kernel.se.common.Plugin;
027
028 /**
029 * Process info for a service.
030 *
031 * @author Michael Meyling
032 */
033 public class ServiceProcessImpl implements ServiceProcess {
034
035 /** This class. */
036 private static final Class CLASS = ServiceProcessImpl.class;
037
038 /** The service the thread works for. */
039 private final Plugin service;
040
041 /** The thread the service is done within. */
042 private final Thread thread;
043
044 /** Some important parameters for the service. For example QEDEQ module address. */
045 private final Map parameters;
046
047 /** Start time for process. */
048 private long start;
049
050 /** End time for process. */
051 private long stop;
052
053 /** State for process. 0 = running, 1 = success, -1 failure. */
054 private int state;
055
056 /** Is this process blocked? */
057 private boolean blocked;
058
059 /** QEDEQ module the process is working on. */
060 private KernelQedeqBo qedeq;
061
062 /** Percentage of currently running plugin execution. */
063 private double executionPercentage = 0;
064
065 /** Percentage of currently running plugin execution. */
066 private String executionActionDescription = "not yet started";
067
068 /** Created execution object. Might be <code>null</code>. */
069 private PluginExecutor executor;
070
071 /**
072 * A new service process.
073 *
074 * @param service This service is executed.
075 * @param qedeq This QEDEQ module we work on.
076 * @param thread The process the service is executed within.
077 * @param parameters Interesting process parameters (e.g. QEDEQ module).
078 */
079 public ServiceProcessImpl(final Plugin service, final Thread thread, final KernelQedeqBo qedeq,
080 final Map parameters) {
081 this.service = service;
082 this.thread = thread;
083 this.qedeq = qedeq;
084 this.parameters = parameters;
085 if (!thread.isAlive()) {
086 throw new RuntimeException("thread is already dead");
087 }
088 start();
089 }
090
091 /**
092 * A new service process within the current thread.
093 *
094 * @param service This service is executed.
095 * @param qedeq Module we work on.
096 * @param parameters Interesting process parameters (e.g. QEDEQ module).
097 */
098 public ServiceProcessImpl(final Plugin service, final KernelQedeqBo qedeq, final Map parameters) {
099 this(service, Thread.currentThread(), qedeq, parameters);
100 }
101
102 /* (non-Javadoc)
103 * @see org.qedeq.kernel.bo.ServiceProcess#getService()
104 */
105 public synchronized Plugin getService() {
106 return service;
107 }
108
109 /* (non-Javadoc)
110 * @see org.qedeq.kernel.bo.ServiceProcess#getThread()
111 */
112 public synchronized Thread getThread() {
113 return thread;
114 }
115
116 /* (non-Javadoc)
117 * @see org.qedeq.kernel.bo.ServiceProcess#getQedeq()
118 */
119 public synchronized QedeqBo getQedeq() {
120 return qedeq;
121 }
122
123 /* (non-Javadoc)
124 * @see org.qedeq.kernel.bo.ServiceProcess#getParameters()
125 */
126 public synchronized Map getParameters() {
127 return parameters;
128 }
129
130 /* (non-Javadoc)
131 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutor()
132 */
133 public synchronized PluginExecutor getExecutor() {
134 return executor;
135 }
136
137 /* (non-Javadoc)
138 * @see org.qedeq.kernel.bo.ServiceProcess#setExecutor(org.qedeq.kernel.bo.module.PluginExecutor)
139 */
140 public synchronized void setExecutor(final PluginExecutor executor) {
141 this.executor = executor;
142 }
143
144 /* (non-Javadoc)
145 * @see org.qedeq.kernel.bo.ServiceProcess#getParameterString()
146 */
147 public synchronized String getParameterString() {
148 final StringBuffer buffer = new StringBuffer(30);
149 final int len = service.getPluginId().length() + 1;
150 if (parameters != null) {
151 Iterator e = parameters.entrySet().iterator();
152 boolean notFirst = false;
153 while (e.hasNext()) {
154 final Map.Entry entry = (Map.Entry) e.next();
155 String key = String.valueOf(entry.getKey());
156 if (key.startsWith(service.getPluginId() + "$")) {
157 if (notFirst) {
158 buffer.append(", ");
159 } else {
160 notFirst = true;
161 }
162 key = key.substring(len);
163 buffer.append(key);
164 buffer.append("=");
165 buffer.append(String.valueOf(entry.getValue()));
166 }
167 }
168 }
169 return buffer.toString();
170 }
171
172 /* (non-Javadoc)
173 * @see org.qedeq.kernel.bo.ServiceProcess#getStart()
174 */
175 public synchronized long getStart() {
176 return start;
177 }
178
179 /* (non-Javadoc)
180 * @see org.qedeq.kernel.bo.ServiceProcess#getStop()
181 */
182 public synchronized long getStop() {
183 return stop;
184 }
185
186 private synchronized void start() {
187 start = System.currentTimeMillis();
188 executionActionDescription = "started";
189 }
190
191 private synchronized void stop() {
192 stop = System.currentTimeMillis();
193 }
194
195 /* (non-Javadoc)
196 * @see org.qedeq.kernel.bo.ServiceProcess#setSuccessState()
197 */
198 public synchronized void setSuccessState() {
199 if (isRunning()) {
200 state = 1;
201 stop();
202 executionActionDescription = "finished";
203 executionPercentage = 100;
204 }
205 }
206
207 /* (non-Javadoc)
208 * @see org.qedeq.kernel.bo.ServiceProcess#setFailureState()
209 */
210 public synchronized void setFailureState() {
211 if (isRunning()) {
212 state = -1;
213 stop();
214 }
215 }
216
217 /* (non-Javadoc)
218 * @see org.qedeq.kernel.bo.ServiceProcess#isRunning()
219 */
220 public synchronized boolean isRunning() {
221 if (state == 0) {
222 if (!thread.isAlive()) {
223 Trace.fatal(CLASS, this, "isRunning()", "Thread has unexpectly died",
224 new RuntimeException());
225 setFailureState();
226 return false;
227 }
228 return true;
229 }
230 return false;
231 }
232
233 /* (non-Javadoc)
234 * @see org.qedeq.kernel.bo.ServiceProcess#isBlocked()
235 */
236 public synchronized boolean isBlocked() {
237 if (isRunning()) {
238 return blocked;
239 }
240 return false;
241 }
242
243 /* (non-Javadoc)
244 * @see org.qedeq.kernel.bo.ServiceProcess#setBlocked(boolean)
245 */
246 public synchronized void setBlocked(final boolean blocked) {
247 this.blocked = blocked;
248 }
249
250 /* (non-Javadoc)
251 * @see org.qedeq.kernel.bo.ServiceProcess#wasSuccess()
252 */
253 public synchronized boolean wasSuccess() {
254 return state == 1;
255 }
256
257 /* (non-Javadoc)
258 * @see org.qedeq.kernel.bo.ServiceProcess#wasFailure()
259 */
260 public synchronized boolean wasFailure() {
261 return state == -1;
262 }
263
264 /* (non-Javadoc)
265 * @see org.qedeq.kernel.bo.ServiceProcess#interrupt()
266 */
267 public synchronized void interrupt() {
268 thread.interrupt();
269 setFailureState();
270 }
271
272 /* (non-Javadoc)
273 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutionPercentage()
274 */
275 public synchronized double getExecutionPercentage() {
276 if (isRunning() || isBlocked()) {
277 if (executor != null) {
278 executionPercentage = executor.getExecutionPercentage();
279 }
280 }
281 return executionPercentage;
282 }
283
284 /* (non-Javadoc)
285 * @see org.qedeq.kernel.bo.ServiceProcess#getExecutionActionDescription()
286 */
287 public synchronized String getExecutionActionDescription() {
288 if (isRunning() || isBlocked()) {
289 if (executor != null) {
290 executionActionDescription = executor.getExecutionActionDescription();
291 }
292 }
293 return executionActionDescription;
294 }
295
296 }
|