Project

General

Profile

Download (6.47 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.common.monitor;
10

    
11
import java.io.Serializable;
12
import java.math.BigDecimal;
13

    
14
import org.apache.logging.log4j.Level;
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.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
 * @since 14.09.2010
23
 */
24
public class DefaultProgressMonitor implements IProgressMonitor {
25

    
26
    private static final long serialVersionUID = 8782649283568146667L;
27
    private static final Logger logger = LogManager.getLogger(DefaultProgressMonitor.class);
28

    
29
    public static final DefaultProgressMonitor NewInstance(){
30
        return new DefaultProgressMonitor();
31
    }
32

    
33
    private boolean isCanceled = false;
34
    protected String taskName = "No task name";
35
    protected int totalWork = 0;
36
    protected double workDone = 0;
37
    protected String subTask = SUBTASK_DEFAULT;
38
    private static final String SUBTASK_DEFAULT = "No subtask name";
39

    
40
    private Serializable feedback;
41
    private transient Object feedbackLock;
42
    private boolean isWaitingForFeedback = false;
43
    private long feedbackWaitStartTime;
44
    private BigDecimal lastPercentage = new BigDecimal(0.0);
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
        workDone = totalWork;
65
    }
66

    
67
    @Override
68
    public boolean isCanceled() {
69
        return isCanceled;
70
    }
71

    
72
    @Override
73
    public void setCanceled(boolean isCanceled) {
74
        this.isCanceled = isCanceled;
75
    }
76

    
77
    @Override
78
    public void setTaskName(String taskName) {
79
        this.taskName = taskName;
80
    }
81

    
82
    @Override
83
    public void subTask(String subTask) {
84
        this.subTask = subTask;
85
        logger.info(/*getPercentage() + "% done." + */  " Next Task: " + subTask);
86
    }
87

    
88
    @Override
89
    public void worked(int work) {
90
        computeWorked(work);
91
//      this.workDone = this.workDone +  work;
92
    }
93

    
94

    
95
    @Override
96
    public void internalWorked(double work) {
97
        computeWorked(work);
98
//      this.workDone = this.workDone +  work;
99
    }
100

    
101
    private void computeWorked(double work){
102
        this.workDone = this.workDone +  work;
103
        if (logger.isInfoEnabled() && getPercentageRounded(2) != lastPercentage){
104
            logger.info(getPercentageRounded(2) + "% done (Completed Task: " + subTask + ")");
105
            lastPercentage = getPercentageRounded(2);
106
        }else if (logger.isDebugEnabled()){
107
            logger.debug(getPercentage() + "% done (Completed Task: " + subTask + ")");
108

    
109
        }
110
    }
111

    
112
    @Override
113
    public void warning(String warning) {
114
        logger.warn(warning);
115
    }
116

    
117
    @Override
118
    public void warning(String warning, Throwable exception) {
119
        logger.warn(warning);
120
        exception.printStackTrace();
121
    }
122

    
123
    /**
124
     * Percentage of work done. With all work done = 100.0d.
125
     * As rounding errors may occur especially when using
126
     * {@link SubProgressMonitor} the result is rounded to 5 digits.
127
     * So do not use the result for additive percentages.
128
     */
129
    public Double getPercentage(){
130
        if(totalWork == 0 ){
131
            return null;
132
        }
133

    
134
        double result = this.workDone * 100 / this.totalWork ;
135
        //as double may have rounding errors especially when using subprogressmonitors
136
        //we do round the result slightly
137
        result = Math.round((result * 100000.0)) / 100000.0;
138
        return result;
139
    }
140

    
141
    public BigDecimal getPercentageRounded(int scale){
142
        if(totalWork == 0 ){
143
            return null;
144
        }
145
        double percentage = this.workDone * 100 / this.totalWork ;
146
        BigDecimal result = new BigDecimal(percentage).setScale( scale, BigDecimal.ROUND_HALF_UP );
147
        return result;
148
    }
149

    
150

    
151
    /**
152
     * {@inheritDoc}
153
     */
154
    @Override
155
    public void waitForFeedback() {
156
        if(feedbackLock == null) {
157
            feedbackLock = new Object();
158
        }
159
        synchronized (feedbackLock) {
160
            feedback = null;
161
            while(feedback == null) {
162
                isWaitingForFeedback = true;
163
                try {
164
                    feedbackWaitStartTime = System.currentTimeMillis();
165
                    feedbackLock.wait();
166
                } catch (InterruptedException ie) {
167
                    throw new IllegalStateException(ie);
168
                }
169
            }
170
        }
171
    }
172

    
173
    /**
174
     * {@inheritDoc}
175
     */
176
    @Override
177
    public void setFeedback(Serializable feedback) {
178
        synchronized (feedbackLock) {
179
            this.feedback = feedback;
180
            this.feedbackLock.notifyAll();
181
            isWaitingForFeedback = false;
182
        }
183
    }
184

    
185

    
186
    /**
187
     * {@inheritDoc}
188
     */
189
    @Override
190
    public Serializable getFeedback() {
191
        return feedback;
192
    }
193

    
194
    /**
195
     * {@inheritDoc}
196
     */
197
    @Override
198
    public boolean getIsWaitingForFeedback() {
199
        return isWaitingForFeedback;
200
    }
201

    
202

    
203
    /**
204
     * {@inheritDoc}
205
     */
206
    @Override
207
    public void waitForFeedback(long feedbackWaitTimeout) {
208
        if(feedbackWaitTimeout <= 0 ) {
209
            throw new IllegalStateException("Feedback wait timeout should be a positive number");
210
        }
211
        this.feedbackWaitTimeout = feedbackWaitTimeout;
212
        waitForFeedback();
213
    }
214

    
215

    
216
    /**
217
     * {@inheritDoc}
218
     */
219
    @Override
220
    public boolean hasFeedbackWaitTimedOut() {
221
       long now = System.currentTimeMillis();
222
       return isWaitingForFeedback && (now - feedbackWaitStartTime > feedbackWaitTimeout);
223
    }
224

    
225

    
226

    
227
    /**
228
     * {@inheritDoc}
229
     */
230
    @Override
231
    public String getOwner() {
232
        return owner;
233
    }
234

    
235

    
236
    /**
237
     * {@inheritDoc}
238
     */
239
    @Override
240
    public void setOwner(String owner) {
241
        this.owner = owner;
242
    }
243

    
244

    
245
    /**
246
     * {@inheritDoc}
247
     */
248
    @Override
249
    public void interrupt() {
250
        // do nothing
251
    }
252

    
253

    
254

    
255
}
(1-1/10)