1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.common.monitor;
|
11
|
|
12
|
import java.io.Serializable;
|
13
|
import java.math.BigDecimal;
|
14
|
|
15
|
import org.apache.log4j.Level;
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
/**
|
19
|
* This is a console style progress monitor with prints the progress information to configured {@link Logger} with level {@link Level#INFO}
|
20
|
*
|
21
|
* @author a.mueller
|
22
|
* @date 14.09.2010
|
23
|
*
|
24
|
*/
|
25
|
public class DefaultProgressMonitor implements IProgressMonitor {
|
26
|
private static final long serialVersionUID = 8782649283568146667L;
|
27
|
|
28
|
private static final Logger logger = Logger.getLogger(DefaultProgressMonitor.class);
|
29
|
|
30
|
public static final DefaultProgressMonitor NewInstance(){
|
31
|
return new DefaultProgressMonitor();
|
32
|
}
|
33
|
|
34
|
private boolean isCanceled = false;
|
35
|
protected String taskName = "No task name";
|
36
|
protected int totalWork = 0;
|
37
|
protected double workDone = 0;
|
38
|
protected String subTask = "No subtask name";
|
39
|
|
40
|
|
41
|
private Serializable feedback;
|
42
|
private transient Object feedbackLock;
|
43
|
private boolean isWaitingForFeedback = false;
|
44
|
private long feedbackWaitStartTime;
|
45
|
private static final long DEFAULT_FEEDBACK_WAIT_TIMEOUT = 1000 * 60 * 60 * 24; // 24 hours
|
46
|
private long feedbackWaitTimeout = DEFAULT_FEEDBACK_WAIT_TIMEOUT;
|
47
|
|
48
|
private String owner;
|
49
|
|
50
|
protected DefaultProgressMonitor(){
|
51
|
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public void beginTask(String taskName, int totalWork) {
|
56
|
logger.info("Start " + taskName);
|
57
|
this.taskName = taskName;
|
58
|
this.totalWork = totalWork;
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public void done() {
|
63
|
logger.info(taskName + "...Done");
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public boolean isCanceled() {
|
68
|
return isCanceled;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public void setCanceled(boolean isCanceled) {
|
73
|
this.isCanceled = isCanceled;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public void setTaskName(String taskName) {
|
78
|
this.taskName = taskName;
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public void subTask(String subTask) {
|
83
|
this.subTask = subTask;
|
84
|
logger.info(/*getPercentage() + "% done." + */ " Next Task: " + subTask);
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public void worked(int work) {
|
89
|
computeWorked(work);
|
90
|
// this.workDone = this.workDone + work;
|
91
|
}
|
92
|
|
93
|
|
94
|
@Override
|
95
|
public void internalWorked(double work) {
|
96
|
computeWorked(work);
|
97
|
// this.workDone = this.workDone + work;
|
98
|
}
|
99
|
|
100
|
private void computeWorked(double work){
|
101
|
this.workDone = this.workDone + work;
|
102
|
if (logger.isInfoEnabled()){ logger.info(getPercentage() + "% done (Completed Task: " + subTask + ")");}
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public void warning(String warning) {
|
107
|
logger.warn(warning);
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
public void warning(String warning, Throwable exception) {
|
112
|
logger.warn(warning);
|
113
|
exception.printStackTrace();
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Percentage of work done. With all work done = 100.0d.
|
118
|
* As rounding errors may occur especially when using
|
119
|
* {@link SubProgressMonitor} the result is rounded to 5 digits.
|
120
|
* So do not use the result for additive percentages.
|
121
|
*/
|
122
|
public Double getPercentage(){
|
123
|
if(totalWork == 0 ){
|
124
|
return null;
|
125
|
}
|
126
|
|
127
|
double result = this.workDone * 100 / this.totalWork ;
|
128
|
//as double may have rounding errors especially when using subprogressmonitors
|
129
|
//we do round the result slightly
|
130
|
result = Math.round((result * 100000.0)) / 100000.0;
|
131
|
return result;
|
132
|
}
|
133
|
|
134
|
public BigDecimal getPercentageRounded(int scale){
|
135
|
if(totalWork == 0 ){
|
136
|
return null;
|
137
|
}
|
138
|
double percentage = this.workDone * 100 / this.totalWork ;
|
139
|
BigDecimal result = new BigDecimal(percentage).setScale( scale, BigDecimal.ROUND_HALF_UP );
|
140
|
return result;
|
141
|
}
|
142
|
|
143
|
|
144
|
/**
|
145
|
* {@inheritDoc}
|
146
|
*/
|
147
|
@Override
|
148
|
public void waitForFeedback() {
|
149
|
if(feedbackLock == null) {
|
150
|
feedbackLock = new Object();
|
151
|
}
|
152
|
synchronized (feedbackLock) {
|
153
|
feedback = null;
|
154
|
while(feedback == null) {
|
155
|
isWaitingForFeedback = true;
|
156
|
try {
|
157
|
feedbackWaitStartTime = System.currentTimeMillis();
|
158
|
feedbackLock.wait();
|
159
|
} catch (InterruptedException ie) {
|
160
|
throw new IllegalStateException(ie);
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* {@inheritDoc}
|
168
|
*/
|
169
|
@Override
|
170
|
public void setFeedback(Serializable feedback) {
|
171
|
synchronized (feedbackLock) {
|
172
|
this.feedback = feedback;
|
173
|
this.feedbackLock.notifyAll();
|
174
|
isWaitingForFeedback = false;
|
175
|
}
|
176
|
}
|
177
|
|
178
|
|
179
|
/**
|
180
|
* {@inheritDoc}
|
181
|
*/
|
182
|
@Override
|
183
|
public Serializable getFeedback() {
|
184
|
return feedback;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* {@inheritDoc}
|
189
|
*/
|
190
|
@Override
|
191
|
public boolean getIsWaitingForFeedback() {
|
192
|
return isWaitingForFeedback;
|
193
|
}
|
194
|
|
195
|
|
196
|
/**
|
197
|
* {@inheritDoc}
|
198
|
*/
|
199
|
@Override
|
200
|
public void waitForFeedback(long feedbackWaitTimeout) {
|
201
|
if(feedbackWaitTimeout <= 0 ) {
|
202
|
throw new IllegalStateException("Feedback wait timeout should be a positive number");
|
203
|
}
|
204
|
this.feedbackWaitTimeout = feedbackWaitTimeout;
|
205
|
waitForFeedback();
|
206
|
}
|
207
|
|
208
|
|
209
|
/**
|
210
|
* {@inheritDoc}
|
211
|
*/
|
212
|
@Override
|
213
|
public boolean hasFeedbackWaitTimedOut() {
|
214
|
long now = System.currentTimeMillis();
|
215
|
return isWaitingForFeedback && (now - feedbackWaitStartTime > feedbackWaitTimeout);
|
216
|
}
|
217
|
|
218
|
|
219
|
|
220
|
/**
|
221
|
* {@inheritDoc}
|
222
|
*/
|
223
|
@Override
|
224
|
public String getOwner() {
|
225
|
return owner;
|
226
|
}
|
227
|
|
228
|
|
229
|
/**
|
230
|
* {@inheritDoc}
|
231
|
*/
|
232
|
@Override
|
233
|
public void setOwner(String owner) {
|
234
|
this.owner = owner;
|
235
|
}
|
236
|
|
237
|
|
238
|
/**
|
239
|
* {@inheritDoc}
|
240
|
*/
|
241
|
@Override
|
242
|
public void interrupt() {
|
243
|
// do nothing
|
244
|
}
|
245
|
|
246
|
|
247
|
|
248
|
}
|