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=c22c1102972cc02be8edd9aa684a90fa783c1345;hb=b5c4e0c9c2fcf788a56d6ce72989fe15182e057d;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..c22c11029 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,34 +8,81 @@ 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; + private Boolean synchronous = false; + + protected AsynchronousTreeTestResultListener(){ + this(false); + } + + protected AsynchronousTreeTestResultListener(Boolean synchronousByDefault){ + synchronous = synchronousByDefault; + } + + /** Starts the underlying thread. */ public void init() { - thread = new Thread(this); - thread.start(); + if (!synchronous) { + thread = new Thread(this); + thread.start(); + } } - public void destroy() { - thread = null; + /** Finish the remaining and destroy */ + public void close(TestResult testResult) { + // FIXME: make behavior more robust when multiple results are + // registering this listener. 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, TestResultPart testResultPart) { TreeTestResult result = (TreeTestResult) testResult; - synchronized (partStructs) { - partStructs.add(new PartStruct(result.getCurrentPath(), - (NumericTRId) result.getTestResultId(), testResultPart, - result)); - partStructs.notifyAll(); + PartStruct partStruct = new PartStruct(result.getCurrentPath(), + (NumericTRId) result.getTestResultId(), testResultPart, result); + + if (!synchronous) { + synchronized (partStructs) { + partStructs.add(partStruct); + partStructs.notifyAll(); + } + } else { + resultPartAdded(partStruct); } } + /** 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 +91,7 @@ public abstract class AsynchronousTreeTestResultListener implements } partStructs.clear(); + partStructs.notifyAll(); while (partStructs.size() == 0) { try { partStructs.wait(); @@ -55,14 +103,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(); @@ -74,4 +126,12 @@ public abstract class AsynchronousTreeTestResultListener implements } + public Boolean getSynchronous() { + return synchronous; + } + + public void setSynchronous(Boolean synchronous) { + this.synchronous = synchronous; + } + }