1
|
/**
|
2
|
* Copyright (C) 2015 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.util.concurrent.ConcurrentHashMap;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
/**
|
17
|
* Thread class to be used to run monitored jobs
|
18
|
*
|
19
|
* @author cmathew
|
20
|
* @since 22 Oct 2015
|
21
|
*
|
22
|
*/
|
23
|
public abstract class RemotingProgressMonitorThread extends Thread {
|
24
|
|
25
|
private static ConcurrentHashMap<IRemotingProgressMonitor, RemotingProgressMonitorThread> monitorsInProgress =
|
26
|
new ConcurrentHashMap<IRemotingProgressMonitor, RemotingProgressMonitorThread>();
|
27
|
|
28
|
private IRemotingProgressMonitor monitor;
|
29
|
private static final Logger logger = Logger.getLogger(RemotingProgressMonitorThread.class);
|
30
|
|
31
|
public void setMonitor(IRemotingProgressMonitor monitor) {
|
32
|
if(monitor == null) {
|
33
|
throw new IllegalStateException("Monitor is null");
|
34
|
}
|
35
|
this.monitor = monitor;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* {@inheritDoc}
|
40
|
*/
|
41
|
@Override
|
42
|
public void run() {
|
43
|
try {
|
44
|
monitorsInProgress.put(monitor, this);
|
45
|
monitor.setResult(doRun(monitor));
|
46
|
} catch(Exception ex) {
|
47
|
logger.info("Exception in RemotingProgressMonitorThread ", ex);
|
48
|
monitor.setResult(ex);
|
49
|
monitor.setIsFailed(true);
|
50
|
}
|
51
|
monitor.done();
|
52
|
monitorsInProgress.remove(monitor);
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Executes the monitored job.
|
57
|
*
|
58
|
* @param monitor to be updated by the monitored job
|
59
|
* @return result object
|
60
|
*/
|
61
|
public abstract Serializable doRun(IRemotingProgressMonitor monitor);
|
62
|
|
63
|
/**
|
64
|
* Returns a currently running monitor thread corresponding to the
|
65
|
* given monitor.
|
66
|
*
|
67
|
* @param monitor for which the thread
|
68
|
* @return
|
69
|
*/
|
70
|
protected static RemotingProgressMonitorThread getMonitorThread(IRemotingProgressMonitor monitor) {
|
71
|
return monitorsInProgress.get(monitor);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* {@inheritDoc}
|
76
|
*/
|
77
|
@Override
|
78
|
public void interrupt() {
|
79
|
super.interrupt();
|
80
|
monitor.setCanceled(true);
|
81
|
monitor.done();
|
82
|
monitorsInProgress.remove(monitor);
|
83
|
}
|
84
|
|
85
|
}
|