2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.util
;
11 import java
.text
.DecimalFormat
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
21 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
22 import eu
.etaxonomy
.taxeditor
.operation
.IFeedbackGenerator
;
23 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
26 * Manages client side progress monitors
33 public class ProgressMonitorClientManager
{
34 private static final Logger logger
= Logger
.getLogger(ProgressMonitorClientManager
.class);
37 * Polls the progress monitor service for the progress status of a monitor
38 * corresponding to the given uuid.
40 * @param label for the operation
41 * @param uuid of the remoting monitor already started on the server
42 * @param pollInterval in milliseconds
43 * @param cancelable flag which determines whether the operation can be cancelled
44 * @param postOp callback for running post operation logic
45 * @param feedbackGenerator feedback generator corresponding to the
46 * 'wait on feedback' request made on the remoting monitor
47 * @param monitor to be updated
48 * @return a final progress monitor after the operation is completed
49 * @throws InterruptedException
51 public IRemotingProgressMonitor
pollMonitor(final String label
,
53 final int pollInterval
,
54 final IPostMoniteredOperationEnabled postOp
,
55 IFeedbackGenerator feedbackGenerator
,
56 IProgressMonitor monitor
) throws InterruptedException
{
57 return pollMonitor(label
, uuid
, pollInterval
, postOp
, Arrays
.asList(feedbackGenerator
), monitor
);
60 * Polls the progress monitor service for the progress status of a monitor
61 * corresponding to the given uuid.
64 * @param label for the operation
65 * @param uuid of the remoting monitor already started on the server
66 * @param pollInterval in milliseconds
67 * @param cancelable flag which determines whether the operation can be cancelled
68 * @param postOp callback for running post operation logic
69 * @param feedbackGenerators list of feedback generators corresponding to the
70 * size and exact order of the 'wait on feedback' requests made on the
72 * @param monitor to be updated
73 * @return a final progress monitor after the operation is completed
74 * @throws InterruptedException
76 public IRemotingProgressMonitor
pollMonitor(final String label
,
78 final int pollInterval
,
79 final IPostMoniteredOperationEnabled postOp
,
80 List
<IFeedbackGenerator
> feedbackGenerators
,
81 IProgressMonitor monitor
) throws InterruptedException
{
82 IProgressMonitorService progressMonitorService
= CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService();
83 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
85 final int START_DELAY
=10;
86 // wait about 10 seconds for the remoting monitor to be initialised
87 // (i.e. for the begin task method to be called ON THE REMOTING MONITOR)
88 for(int i
=0;i
<START_DELAY
;i
++) {
90 logger
.info("Waiting for monitered work to start ..");
91 remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
92 if(remotingMonitor
.getTotalWork() > 0) {
96 // if the total work is still not been set then we assume that the
97 // operation has zero work units
98 if(remotingMonitor
.getTotalWork() == 0) {
99 throw new InterruptedException("Monitor has zero work units");
101 // start the client monitor
102 monitor
.beginTask(label
, remotingMonitor
.getTotalWork());
103 logger
.info("Work to be done: " + remotingMonitor
.getTotalWork());
104 int editorTotalWorkDone
= 0;
105 int serverTotalWorkDone
= 0;
106 // loop until the operation is done
107 int feedbackCount
= 0;
108 while(!(remotingMonitor
.isCanceled() || remotingMonitor
.isFailed() || remotingMonitor
.isDone())) {
109 // wait for pollInterval, then
110 // .... retrieve remoting monitor, then
111 // .... set client monitor info
112 Thread
.sleep(pollInterval
);
113 remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
114 // check if remoting monitor is waiting for feedback
115 if(remotingMonitor
.getIsWaitingForFeedback()) {
116 if(feedbackGenerators
!= null) {
117 // if we have run out of feedback generators while
118 // the remoting monitor is waiting on feedback
119 // then throw exception
120 if(feedbackCount
+ 1 > feedbackGenerators
.size()) {
121 throw new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
123 feedbackGenerators
.get(feedbackCount
).setFeedbackForMonitor(uuid
);
127 serverTotalWorkDone
= (int) remotingMonitor
.getWorkDone();
128 logger
.info("Work done from start: " + serverTotalWorkDone
);
129 String percentage
= new DecimalFormat("#.##").format(remotingMonitor
.getPercentage());
131 monitor
.setTaskName(label
+ " " + percentage
+ "% done ");
132 monitor
.subTask(remotingMonitor
.getSubTask());
133 int worked
= serverTotalWorkDone
- editorTotalWorkDone
;
135 logger
.info("Work done since last check: " + worked
);
136 monitor
.worked(worked
);
138 editorTotalWorkDone
= serverTotalWorkDone
;
140 if(remotingMonitor
.getResult() instanceof Exception
) {
141 throw new IllegalStateException((Exception
)remotingMonitor
.getResult());
143 return remotingMonitor
;
145 if(postOp
!= null && remotingMonitor
.isDone()) {
146 postOp
.postOperation(remotingMonitor
);