Project

General

Profile

Download (6.1 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();
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
    @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() && getPercentageRounded(2) != lastPercentage){
103
            logger.info(getPercentageRounded(2) + "% done (Completed Task: " + subTask + ")");
104
            lastPercentage = getPercentageRounded(2);
105
        }else if (logger.isDebugEnabled()){
106
            logger.debug(getPercentage() + "% done (Completed Task: " + subTask + ")");
107

    
108
        }
109
    }
110

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

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

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

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

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

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

    
168
    @Override
169
    public void setFeedback(Serializable feedback) {
170
        synchronized (feedbackLock) {
171
            this.feedback = feedback;
172
            this.feedbackLock.notifyAll();
173
            isWaitingForFeedback = false;
174
        }
175
    }
176

    
177
    @Override
178
    public Serializable getFeedback() {
179
        return feedback;
180
    }
181

    
182
    @Override
183
    public boolean getIsWaitingForFeedback() {
184
        return isWaitingForFeedback;
185
    }
186

    
187
    @Override
188
    public void waitForFeedback(long feedbackWaitTimeout) {
189
        if(feedbackWaitTimeout <= 0 ) {
190
            throw new IllegalStateException("Feedback wait timeout should be a positive number");
191
        }
192
        this.feedbackWaitTimeout = feedbackWaitTimeout;
193
        waitForFeedback();
194
    }
195

    
196
    @Override
197
    public boolean hasFeedbackWaitTimedOut() {
198
       long now = System.currentTimeMillis();
199
       return isWaitingForFeedback && (now - feedbackWaitStartTime > feedbackWaitTimeout);
200
    }
201

    
202
    @Override
203
    public String getOwner() {
204
        return owner;
205
    }
206

    
207
    @Override
208
    public void setOwner(String owner) {
209
        this.owner = owner;
210
    }
211

    
212
    @Override
213
    public void interrupt() {
214
        // do nothing
215
    }
216
}
(1-1/10)