X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Ftest%2Ftree%2FAsynchronousTreeTestResultListener.java;h=751b0e9b150427448eb5070535f71ee7970da83f;hb=ed93fb9db5f9008a78d9ab892806ae398ed65b85;hp=13b3b25dfabe9d0fcab365df83ddbf7f9ed0b099;hpb=62ef12a06742ee09256c18e8db90fe83a108c116;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java b/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java index 13b3b25df..751b0e9b1 100644 --- a/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java +++ b/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java @@ -8,21 +8,38 @@ import org.argeo.slc.core.test.TestResult; import org.argeo.slc.core.test.TestResultListener; import org.argeo.slc.core.test.TestResultPart; +/** + * Abstract asynchronous implementation of a listener listening to a + * TreeTestResult. + * + * @see TreeTestResult + */ public abstract class AsynchronousTreeTestResultListener implements TestResultListener, Runnable { private Vector partStructs = new Vector(); private Thread thread; + /** Starts the underlying thread. */ public void init() { thread = new Thread(this); thread.start(); } - public void destroy() { - thread = null; + /** Finish the remaining and destroy */ + public void close(TestResult testResult) { synchronized (partStructs) { + // TODO: put a timeout + while (partStructs.size() != 0) { + try { + partStructs.wait(500); + } catch (InterruptedException e) { + // silent + } + } + thread = null; partStructs.notifyAll(); } + postClose((TreeTestResult) testResult); } public final void resultPartAdded(TestResult testResult, @@ -36,6 +53,17 @@ public abstract class AsynchronousTreeTestResultListener implements } } + /** Called when a result part has been added. */ + protected abstract void resultPartAdded(PartStruct partStruct); + + /** + * Called at the end of close. Default implementation is empty. To be + * overridden. + */ + protected void postClose(TreeTestResult testResult) { + + } + public void run() { while (thread != null) { synchronized (partStructs) { @@ -44,6 +72,7 @@ public abstract class AsynchronousTreeTestResultListener implements } partStructs.clear(); + partStructs.notifyAll(); while (partStructs.size() == 0) { try { partStructs.wait(); @@ -55,14 +84,18 @@ public abstract class AsynchronousTreeTestResultListener implements } } - protected abstract void resultPartAdded(PartStruct partStruct); - + /** Structure used to pass tree specific information to subclasses. */ protected static class PartStruct { + /** The tree path of this part. */ public final TreeSPath path; + /** The test result id of the related test result */ public final NumericTRId resultId; + /** The part itself */ public final TestResultPart part; + /** The tree test result itself. */ public final TreeTestResult result; + /** Constructor */ public PartStruct(TreeSPath path, NumericTRId resultId, TestResultPart part, TreeTestResult result) { super();