PluginCallImpl.java
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.kernel.bo.common.PluginCall;
020 import org.qedeq.kernel.bo.common.QedeqBo;
021 import org.qedeq.kernel.bo.common.ServiceProcess;
022 import org.qedeq.kernel.bo.module.KernelQedeqBo;
023 import org.qedeq.kernel.bo.module.PluginExecutor;
024 import org.qedeq.kernel.se.common.Plugin;
025 
026 /**
027  * Single call for a service.
028  *
029  @author  Michael Meyling
030  */
031 public class PluginCallImpl implements PluginCall {
032 
033     /** Counter for each service call. */
034     private static volatile long globalCounter;
035 
036     /** The service the thread works for. */
037     private final Plugin plugin;
038 
039     /** QEDEQ module the process is working on. */
040     private final KernelQedeqBo qedeq;
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 finished by interrupt. */
052     private int state;
053 
054     /** Percentage of currently running plugin execution. */
055     private double executionPercentage = 0;
056 
057     /** Percentage of currently running plugin execution. */
058     private String executionActionDescription = "not yet started";
059 
060     /** Created execution object. Might be <code>null</code>. */
061     private PluginExecutor executor;
062 
063     /** Service process. */
064     private final ServiceProcess process;
065 
066     /** Parent plugin call. Might be <code>null</code>. */
067     private final PluginCall parent;
068 
069     /** Call id. */
070     private final long id;
071 
072     /** Return code. */
073     private int ret;
074 
075     /** Resulting object. */
076     private Object result;
077 
078     /**
079      * A new service process within the current thread.
080      *
081      @param   service     This service is executed.
082      @param   qedeq       Module we work on.
083      @param   parameters  Interesting process parameters (e.g. QEDEQ module).
084      @param   process     Service process we run within.
085      @param   parent      Parent plugin call if any.
086      */
087     public PluginCallImpl(final Plugin service,
088             final KernelQedeqBo qedeq, final Parameters parameters, final ServiceProcess process,
089             final PluginCall parent) {
090         this.id = inc();
091         this.qedeq = qedeq;
092         this.plugin = service;
093         this.parameters = parameters;
094         this.process = process;
095         if (!process.getThread().isAlive()) {
096             throw new RuntimeException("thread is already dead");
097         }
098         this.parent = parent;
099         start();
100     }
101 
102     private synchronized long inc() {
103         return globalCounter++;
104     }
105 
106     public Plugin getPlugin() {
107         return plugin;
108     }
109 
110     public QedeqBo getQedeq() {
111         return qedeq;
112     }
113 
114     public synchronized Parameters getParameters() {
115         return parameters;
116     }
117 
118     /**
119      * Get current executor for this call.
120      *
121      @return  Current executor, might be <code>null</code>.
122      */
123     public synchronized PluginExecutor getExecutor() {
124         return executor;
125     }
126 
127     /**
128      * Set current executor for this call.
129      *
130      @param   executor    Executor, might be <code>null</code>.
131      */
132     public synchronized void setExecutor(final PluginExecutor executor) {
133         this.executor = executor;
134     }
135 
136     public String getParameterString() {
137         return parameters.getParameterString();
138     }
139 
140     public long getStart() {
141         return start;
142     }
143 
144     public synchronized long getStop() {
145         return stop;
146     }
147 
148     private synchronized void start() {
149         start = System.currentTimeMillis();
150         executionActionDescription = "started";
151     }
152 
153     private synchronized void stop() {
154         stop = System.currentTimeMillis();
155     }
156 
157     /**
158      * Set success state for call and stop.
159      */
160     public synchronized void setSuccessState() {
161         if (isRunning()) {
162             state = 1;
163             stop();
164             executionActionDescription = "finished";
165             executionPercentage = 100;
166         }
167     }
168 
169     /**
170      * Set failure state for call and stop.
171      */
172     public synchronized void setFailureState() {
173         if (isRunning()) {
174             state = -1;
175             stop();
176         }
177     }
178 
179     public synchronized boolean isRunning() {
180         return state == 0;
181     }
182 
183     public synchronized boolean isFinished() {
184         return state != 0;
185     }
186 
187     public synchronized boolean wasInterrupted() {
188         return state == -1;
189     }
190 
191     public synchronized boolean hasNormallyFinished() {
192         return state == 1;
193     }
194 
195     public synchronized ServiceProcess getServiceProcess() {
196         return process;
197     }
198 
199     public synchronized double getExecutionPercentage() {
200         if (isRunning()) {
201             if (executor != null) {
202                 executionPercentage = executor.getExecutionPercentage();
203             }
204         }
205         return executionPercentage;
206     }
207 
208     public synchronized String getExecutionActionDescription() {
209         if (isRunning()) {
210             if (executor != null) {
211                 executionActionDescription = executor.getLocationDescription();
212             }
213         }
214         return executionActionDescription;
215     }
216 
217     public long getId() {
218         return id;
219     }
220 
221     public int hashCode() {
222         return (intid;
223     }
224 
225     public boolean equals(final Object obj) {
226         return == compareTo(obj);
227     }
228 
229     public int compareTo(final Object o) {
230         if (!(instanceof PluginCall)) {
231             return -1;
232         }
233         final PluginCall s = (PluginCallo;
234         return (getId() < s.getId() ? -(getId() == s.getId() 1));
235     }
236 
237     public synchronized PluginCall getParentPluginCall() {
238         return parent;
239     }
240 
241     public synchronized Object getExecutionResult() {
242         return result;
243     }
244 
245     public synchronized int getReturnCode() {
246         return ret;
247     }
248 
249 }