Project

General

Profile

« Previous | Next » 

Revision 1c7b9181

Added by Cherian Mathew almost 9 years ago

#5297 Add cancel and waitForFeedback functionality

Correct work units

View differences:

cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/DefaultProgressMonitor.java
136 136
    }
137 137

  
138 138

  
139
    /**
140
     * {@inheritDoc}
141
     */
142
    @Override
143
    public void waitForFeedback() {
144

  
145
    }
146

  
147

  
139 148

  
140 149
}
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/IProgressMonitor.java
140 140
	 */
141 141
	public void warning(String message, Throwable throwable);
142 142

  
143

  
144
	/**
145
	 * Waits for external feedback during the lifetime of the
146
	 * operation
147
	 */
148
	public void waitForFeedback();
149

  
143 150
}
144 151

  
145 152

  
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/IRemotingProgressMonitor.java
57 57
     */
58 58
    public void setOwner(String owner);
59 59

  
60
    public void setFeedback(Object feedback);
61

  
62
    public boolean isWaitingForFeedback();
63

  
60 64
}
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/NullProgressMonitor.java
1 1
/**
2
 * 
2
 *
3 3
 */
4 4
package eu.etaxonomy.cdm.common.monitor;
5 5

  
6 6
/**
7 7
 * Empty default implementation
8
 * 
8
 *
9 9
 * @author n.hoffmann
10 10
 *
11 11
 */
......
59 59

  
60 60
	@Override
61 61
	public void internalWorked(double work) {
62
		//  do nothing	
62
		//  do nothing
63 63
	}
64 64

  
65
    /**
66
     * {@inheritDoc}
67
     */
68
    @Override
69
    public void waitForFeedback() {
70
    //  do nothing
71
    }
72

  
65 73
}
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/RemotingProgressMonitor.java
23 23
    private Object result;
24 24
    private List<String> reports = new ArrayList<String>();
25 25
    private String owner;
26
    private Object feedback;
27
    private boolean isWaitingForFeedback = false;
26 28

  
27 29

  
28 30
    /**
......
79 81
        this.owner = owner;
80 82
    }
81 83

  
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public void waitForFeedback() {
89
        synchronized (feedback) {
90
            feedback = null;
91
            while(feedback == null) {
92
                isWaitingForFeedback = true;
93
                try {
94
                    feedback.wait();
95
                } catch (InterruptedException ie) {
96
                    throw new IllegalStateException(ie);
97
                }
98
            }
99
        }
100
    }
101

  
102

  
103

  
104
    /**
105
     * {@inheritDoc}
106
     */
107
    @Override
108
    public void setFeedback(Object feedback) {
109
        synchronized (feedback) {
110
            this.feedback = feedback;
111
            this.feedback.notifyAll();
112
            isWaitingForFeedback = false;
113
          }
114

  
115
    }
116

  
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    public boolean isWaitingForFeedback() {
122
        return isWaitingForFeedback;
123
    }
124

  
125

  
82 126
}
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/monitor/SubProgressMonitor.java
5 5
         * are made available under the terms of the Eclipse Public License v1.0
6 6
         * which accompanies this distribution, and is available at
7 7
         * http://www.eclipse.org/legal/epl-v10.html
8
         * 
8
         *
9 9
         * Contributors:
10 10
         *     IBM Corporation - initial API and implementation
11 11
         *******************************************************************************/
......
14 14

  
15 15
/**
16 16
 * For new implementations consider using {@link SubMonitor}.
17
 * 
17
 *
18 18
 * A progress monitor that uses a given amount of work ticks
19 19
 * from a parent monitor. It can be used as follows:
20 20
 * <pre>
......
38 38
 * </p><p>
39 39
 * This class may be instantiated or subclassed by clients.
40 40
 * </p>
41
 * 
41
 *
42 42
 * @see SubMonitor
43 43
 */
44 44
public class SubProgressMonitor extends ProgressMonitorWrapper {
......
51 51
     */
52 52
    public static final int SUPPRESS_SUBTASK_LABEL = 1 << 1;
53 53
    /**
54
     * Style constant indicating that the main task label 
54
     * Style constant indicating that the main task label
55 55
     * should be prepended to the subtask label.
56 56
     *
57 57
     * @see #SubProgressMonitor(IProgressMonitor,int,int)
......
68 68
    private String mainTaskLabel;
69 69

  
70 70
    /**
71
     * Creates a new sub-progress monitor for the given monitor. The sub 
72
     * progress monitor uses the given number of work ticks from its 
71
     * Creates a new sub-progress monitor for the given monitor. The sub
72
     * progress monitor uses the given number of work ticks from its
73 73
     * parent monitor.
74 74
     *
75 75
     * @param monitor the parent progress monitor
......
81 81
    }
82 82

  
83 83
    /**
84
     * Creates a new sub-progress monitor for the given monitor. The sub 
85
     * progress monitor uses the given number of work ticks from its 
84
     * Creates a new sub-progress monitor for the given monitor. The sub
85
     * progress monitor uses the given number of work ticks from its
86 86
     * parent monitor.
87 87
     *
88 88
     * @param monitor the parent progress monitor
......
108 108
     *
109 109
     * Starts a new main task. Since this progress monitor is a sub
110 110
     * progress monitor, the given name will NOT be used to update
111
     * the progress bar's main task label. That means the given 
111
     * the progress bar's main task label. That means the given
112 112
     * string will be ignored. If style <code>PREPEND_MAIN_LABEL_TO_SUBTASK
113 113
     * <code> is specified, then the given string will be prepended to
114 114
     * every string passed to <code>subTask(String)</code>.
115 115
     */
116
    @Override
116 117
    public void beginTask(String name, int totalWork) {
117 118
        nestedBeginTasks++;
118 119
        // Ignore nested begin task calls.
119 120
        if (nestedBeginTasks > 1) {
120 121
            return;
121 122
        }
122
        // be safe:  if the argument would cause math errors (zero or 
123
        // be safe:  if the argument would cause math errors (zero or
123 124
        // negative), just use 0 as the scale.  This disables progress for
124
        // this submonitor. 
125
        // this submonitor.
125 126
        scale = totalWork <= 0 ? 0 : (double) parentTicks
126 127
                / (double) totalWork;
127 128
        if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0) {
......
132 133
    /* (Intentionally not javadoc'd)
133 134
     * Implements the method <code>IProgressMonitor.done</code>.
134 135
     */
136
    @Override
135 137
    public void done() {
136 138
        // Ignore if more done calls than beginTask calls or if we are still
137 139
        // in some nested beginTasks
138
        if (nestedBeginTasks == 0 || --nestedBeginTasks > 0)
140
        if (nestedBeginTasks == 0 || --nestedBeginTasks > 0) {
139 141
            return;
142
        }
140 143
        // Send any remaining ticks and clear out the subtask text
141 144
        double remaining = parentTicks - sentToParent;
142
        if (remaining > 0)
145
        if (remaining > 0) {
143 146
            super.internalWorked(remaining);
147
        }
144 148
        //clear the sub task if there was one
145 149
        if (hasSubTask)
150
         {
146 151
            subTask(""); //$NON-NLS-1$
152
        }
147 153
        sentToParent = 0;
148 154
    }
149 155

  
......
168 174
    /* (Intentionally not javadoc'd)
169 175
     * Implements the method <code>IProgressMonitor.subTask</code>.
170 176
     */
177
    @Override
171 178
    public void subTask(String name) {
172 179
        if ((style & SUPPRESS_SUBTASK_LABEL) != 0) {
173 180
            return;
......
184 191
    /* (Intentionally not javadoc'd)
185 192
     * Implements the method <code>IProgressMonitor.worked</code>.
186 193
     */
194
    @Override
187 195
    public void worked(int work) {
188 196
        internalWorked(work);
189 197
    }
......
191 199
	@Override
192 200
	public void warning(String message) {
193 201
		// TODO Auto-generated method stub
194
		
202

  
195 203
	}
196 204

  
197 205
	@Override
198 206
	public void warning(String message, Throwable throwable) {
199 207
		// TODO Auto-generated method stub
200
		
208

  
201 209
	}
210

  
211
    /**
212
     * {@inheritDoc}
213
     */
214
    @Override
215
    public void waitForFeedback() {
216

  
217
    }
202 218
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206Import.java
224 224
            if (unitsList != null) {
225 225
                String message = "nb units to insert: " + unitsList.getLength();
226 226
                logger.info(message);
227
                state.getConfig().getProgressMonitor().beginTask("Importing ABCD file", unitsList.getLength() + 2);
227
                state.getConfig().getProgressMonitor().beginTask("Importing ABCD file", unitsList.getLength() + 3);
228 228
                updateProgress(state, message);
229 229

  
230 230
                state.setDataHolder(new Abcd206DataHolder());
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IProgressMonitorService.java
38 38
     */
39 39
    public IRemotingProgressMonitor getRemotingMonitor(UUID uuid);
40 40

  
41
    /**
42
     * Sets the cancel flag to true for the monitor corresponding to the
43
     * given uuid
44
     *
45
     * @param uuid of remoting monitor
46
     */
47
    public void cancel(UUID uuid);
48

  
41 49
    /**
42 50
     * Interrupt thread corresponding to remoting monitor with
43 51
     * given uuid
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ProgressMonitorServiceImpl.java
78 78
        if(monitorThread != null) {
79 79
            monitorThread.interrupt();
80 80
        }
81

  
82 81
    }
83 82

  
84 83
    /**
......
89 88
        return RemotingProgressMonitorThread.getMonitorThread(getRemotingMonitor(uuid)) != null;
90 89
    }
91 90

  
91
    /**
92
     * {@inheritDoc}
93
     */
94
    @Override
95
    public void cancel(UUID uuid) {
96
        IRestServiceProgressMonitor monitor = progressMonitorManager.getMonitor(uuid);
97
        if(monitor != null) {
98
            monitor.setCanceled(true);
99
            monitor.done();
100
        }
101
    }
102

  
92 103
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TestServiceImpl.java
85 85
            @Override
86 86
            public Object doRun(IRemotingProgressMonitor monitor)  {
87 87
                Object result = longRunningMethod(monitor, ex);
88
                monitor.addReport("Report");
88
                if(!monitor.isCanceled()) {
89
                    monitor.addReport("Report");
90
                }
89 91
                return result;
90 92
            }
91 93
        };
......
107 109
                if(i == stepToThrowException && ex != null) {
108 110
                    throw ex;
109 111
                }
112
                if(monitor.isCanceled()) {
113
                    return "Cancelled";
114
                }
110 115
            } catch (InterruptedException e) {
111 116
                throw ex;
112 117
            }

Also available in: Unified diff