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
.cdm
.common
.monitor
;
11 import java
.io
.Serializable
;
12 import java
.util
.concurrent
.ConcurrentHashMap
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
18 * Thread class to be used to run monitored jobs
23 public abstract class RemotingProgressMonitorThread
extends Thread
{
25 private static final Logger logger
= LogManager
.getLogger();
27 private static ConcurrentHashMap
<IRemotingProgressMonitor
, RemotingProgressMonitorThread
>
28 monitorsInProgress
= new ConcurrentHashMap
<>();
30 private IRemotingProgressMonitor monitor
;
32 public void setMonitor(IRemotingProgressMonitor monitor
) {
34 throw new IllegalStateException("Monitor is null");
36 this.monitor
= monitor
;
42 monitorsInProgress
.put(monitor
, this);
43 monitor
.setResult(doRun(monitor
));
44 } catch(Exception ex
) {
45 logger
.info("Exception in RemotingProgressMonitorThread ", ex
);
46 monitor
.setResult(ex
);
47 monitor
.setIsFailed(true);
50 monitorsInProgress
.remove(monitor
);
54 * Executes the monitored job.
56 * @param monitor to be updated by the monitored job
57 * @return result object
59 public abstract Serializable
doRun(IRemotingProgressMonitor monitor
);
62 * Returns a currently running monitor thread corresponding to the
65 * @param monitor for which the thread
68 protected static RemotingProgressMonitorThread
getMonitorThread(IRemotingProgressMonitor monitor
) {
69 return monitorsInProgress
.get(monitor
);
73 public void interrupt() {
75 monitor
.setCanceled(true);
77 monitorsInProgress
.remove(monitor
);