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.trace.Trace;
019 import org.qedeq.kernel.bo.common.PluginCall;
020 import org.qedeq.kernel.bo.common.QedeqBo;
021 import org.qedeq.kernel.bo.common.QedeqBoSet;
022 import org.qedeq.kernel.bo.common.ServiceProcess;
023 import org.qedeq.kernel.bo.module.InternalServiceProcess;
024 import org.qedeq.kernel.bo.module.KernelQedeqBo;
025 import org.qedeq.kernel.bo.module.KernelQedeqBoSet;
026
027 /**
028 * Process info for a kernel service.
029 *
030 * @author Michael Meyling
031 */
032 public class ServiceProcessImpl implements InternalServiceProcess {
033
034 /** This class. */
035 private static final Class CLASS = ServiceProcessImpl.class;
036
037 /** Counter for each service process. */
038 private static long globalCounter;
039
040 /** The plugin call the process currently works for. */
041 private PluginCall call;
042
043 /** The thread the service is done within. */
044 private final Thread thread;
045
046 /** Start time for process. */
047 private long start;
048
049 /** End time for process. */
050 private long stop;
051
052 /** State for process. 0 = running, 1 = success, -1 failure. */
053 private int state;
054
055 /** Action name. */
056 private final String actionName;
057
058 /** Percentage of currently running plugin execution. */
059 private double executionPercentage = 0;
060
061 /** Percentage of currently running plugin execution. */
062 private String executionActionDescription = "not yet started";
063
064 /** Is this process blocked? */
065 private boolean blocked;
066
067 /** We block these QEDEQ modules for other processes. */
068 private final KernelQedeqBoSet blockedModules;
069
070 /** Process id. */
071 private final long id;
072
073 /**
074 * A new service process within the current thread.
075 *
076 * @param actionName Main process purpose.
077 */
078 public ServiceProcessImpl(final String actionName) {
079 this.id = inc();
080 this.thread = Thread.currentThread();
081 this.call = null;
082 this.blockedModules = new KernelQedeqBoSet();
083 this.actionName = actionName;
084 start();
085 }
086
087 private synchronized long inc() {
088 return globalCounter++;
089 }
090
091 public synchronized void setPluginCall(final PluginCall call) {
092 this.call = call;
093 }
094
095 public synchronized PluginCall getPluginCall() {
096 return call;
097 }
098
099 public synchronized Thread getThread() {
100 return thread;
101 }
102
103 public synchronized QedeqBo getQedeq() {
104 if (call != null) {
105 return call.getQedeq();
106 }
107 return null;
108 }
109
110 public synchronized String getQedeqName() {
111 if (call != null) {
112 return call.getQedeq().getName();
113 }
114 return "";
115 }
116
117 public synchronized String getQedeqUrl() {
118 if (call != null) {
119 return call.getQedeq().getUrl();
120 }
121 return "";
122 }
123
124 public synchronized long getStart() {
125 return start;
126 }
127
128 public synchronized long getStop() {
129 return stop;
130 }
131
132 private synchronized void start() {
133 start = System.currentTimeMillis();
134 executionActionDescription = "started";
135 }
136
137 private synchronized void stop() {
138 stop = System.currentTimeMillis();
139 }
140
141 public synchronized void setSuccessState() {
142 if (isRunning()) {
143 state = 1;
144 stop();
145 executionActionDescription = "finished";
146 executionPercentage = 100;
147 }
148 }
149
150 public synchronized void setFailureState() {
151 if (isRunning()) {
152 state = -1;
153 stop();
154 }
155 }
156
157 public synchronized boolean isRunning() {
158 if (state == 0) {
159 if (!thread.isAlive()) {
160 Trace.fatal(CLASS, this, "isRunning()", "Thread has unexpectly died",
161 new RuntimeException());
162 setFailureState();
163 return false;
164 }
165 return true;
166 }
167 return false;
168 }
169
170 public synchronized boolean isBlocked() {
171 if (isRunning()) {
172 return blocked;
173 }
174 return false;
175 }
176
177 public synchronized void setBlocked(final boolean blocked) {
178 this.blocked = blocked;
179 }
180
181 public synchronized boolean wasSuccess() {
182 return state == 1;
183 }
184
185 public synchronized boolean wasFailure() {
186 return state == -1;
187 }
188
189
190 public synchronized void interrupt() {
191 thread.interrupt();
192 }
193
194 public synchronized double getExecutionPercentage() {
195 if (isRunning() || isBlocked()) {
196 if (call != null) {
197 executionPercentage = call.getExecutionPercentage();
198 }
199 }
200 return executionPercentage;
201 }
202
203 public synchronized String getActionName() {
204 return actionName;
205 }
206
207 public synchronized String getExecutionActionDescription() {
208 if (isRunning() || isBlocked()) {
209 if (call != null) {
210 executionActionDescription = call.getExecutionActionDescription();
211 }
212 }
213 return executionActionDescription;
214 }
215
216 public synchronized QedeqBoSet getBlockedModules() {
217 return new KernelQedeqBoSet(blockedModules);
218 }
219
220 public synchronized void addBlockedModules(final KernelQedeqBoSet set) {
221 blockedModules.add(set);
222 }
223
224 public synchronized void addBlockedModule(final KernelQedeqBo element) {
225 blockedModules.add(element);
226 }
227
228 public synchronized void removeBlockedModules(final KernelQedeqBoSet set) {
229 blockedModules.remove(set);
230 }
231
232 public synchronized void removeBlockedModule(final KernelQedeqBo element) {
233 blockedModules.remove(element);
234 }
235
236 public long getId() {
237 return id;
238 }
239
240 public int hashCode() {
241 return (int) id;
242 }
243
244 public boolean equals(final Object obj) {
245 return 0 == compareTo(obj);
246 }
247
248 public int compareTo(final Object o) {
249 if (!(o instanceof ServiceProcess)) {
250 return -1;
251 }
252 final ServiceProcess s = (ServiceProcess) o;
253 return (getId() < s.getId() ? -1 : (getId() == s.getId() ? 0 : 1));
254 }
255
256
257 }
|