]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java
Manage with Maven
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / core / test / tree / AsynchronousTreeTestResultListener.java
index 76a8bb05a08071e93b9f7bb3a41c84d1557530e6..c22c1102972cc02be8edd9aa684a90fa783c1345 100644 (file)
@@ -19,13 +19,28 @@ public abstract class AsynchronousTreeTestResultListener implements
        private Vector<PartStruct> partStructs = new Vector<PartStruct>();\r
        private Thread thread;\r
 \r
+       private Boolean synchronous = false;\r
+       \r
+       protected AsynchronousTreeTestResultListener(){\r
+               this(false);\r
+       }\r
+\r
+       protected AsynchronousTreeTestResultListener(Boolean synchronousByDefault){\r
+               synchronous = synchronousByDefault;\r
+       }\r
+\r
+       /** Starts the underlying thread. */\r
        public void init() {\r
-               thread = new Thread(this);\r
-               thread.start();\r
+               if (!synchronous) {\r
+                       thread = new Thread(this);\r
+                       thread.start();\r
+               }\r
        }\r
 \r
        /** Finish the remaining and destroy */\r
-       public void close() {\r
+       public void close(TestResult testResult) {\r
+               // FIXME: make behavior more robust when multiple results are\r
+               // registering this listener.\r
                synchronized (partStructs) {\r
                        // TODO: put a timeout\r
                        while (partStructs.size() != 0) {\r
@@ -38,22 +53,36 @@ public abstract class AsynchronousTreeTestResultListener implements
                        thread = null;\r
                        partStructs.notifyAll();\r
                }\r
+               postClose((TreeTestResult) testResult);\r
        }\r
 \r
        public final void resultPartAdded(TestResult testResult,\r
                        TestResultPart testResultPart) {\r
                TreeTestResult result = (TreeTestResult) testResult;\r
-               synchronized (partStructs) {\r
-                       partStructs.add(new PartStruct(result.getCurrentPath(),\r
-                                       (NumericTRId) result.getTestResultId(), testResultPart,\r
-                                       result));\r
-                       partStructs.notifyAll();\r
+               PartStruct partStruct = new PartStruct(result.getCurrentPath(),\r
+                               (NumericTRId) result.getTestResultId(), testResultPart, result);\r
+\r
+               if (!synchronous) {\r
+                       synchronized (partStructs) {\r
+                               partStructs.add(partStruct);\r
+                               partStructs.notifyAll();\r
+                       }\r
+               } else {\r
+                       resultPartAdded(partStruct);\r
                }\r
        }\r
 \r
        /** Called when a result part has been added. */\r
        protected abstract void resultPartAdded(PartStruct partStruct);\r
 \r
+       /**\r
+        * Called at the end of close. Default implementation is empty. To be\r
+        * overridden.\r
+        */\r
+       protected void postClose(TreeTestResult testResult) {\r
+\r
+       }\r
+\r
        public void run() {\r
                while (thread != null) {\r
                        synchronized (partStructs) {\r
@@ -85,6 +114,7 @@ public abstract class AsynchronousTreeTestResultListener implements
                /** The tree test result itself. */\r
                public final TreeTestResult result;\r
 \r
+               /** Constructor */\r
                public PartStruct(TreeSPath path, NumericTRId resultId,\r
                                TestResultPart part, TreeTestResult result) {\r
                        super();\r
@@ -96,4 +126,12 @@ public abstract class AsynchronousTreeTestResultListener implements
 \r
        }\r
 \r
+       public Boolean getSynchronous() {\r
+               return synchronous;\r
+       }\r
+\r
+       public void setSynchronous(Boolean synchronous) {\r
+               this.synchronous = synchronous;\r
+       }\r
+\r
 }\r