]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/AsynchronousTreeTestResultListener.java
Add web service notification of results.
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / AsynchronousTreeTestResultListener.java
1 package org.argeo.slc.core.test.tree;
2
3 import java.util.Vector;
4
5 import org.argeo.slc.core.structure.tree.TreeSPath;
6 import org.argeo.slc.core.test.TestResult;
7 import org.argeo.slc.core.test.TestResultListener;
8 import org.argeo.slc.core.test.TestResultPart;
9
10 /**
11 * Abstract asynchronous implementation of a listener listening to a
12 * <code>TreeTestResult</code>.
13 *
14 * @deprecated listeners should be called synchronously
15 * @see TreeTestResult
16 */
17 public abstract class AsynchronousTreeTestResultListener implements
18 TestResultListener, Runnable {
19 private Vector<PartStruct> partStructs = new Vector<PartStruct>();
20 private Thread thread;
21
22 private Boolean synchronous = true;
23
24 protected AsynchronousTreeTestResultListener() {
25 this(true);
26 }
27
28 protected AsynchronousTreeTestResultListener(Boolean synchronousByDefault) {
29 synchronous = synchronousByDefault;
30 }
31
32 /** Starts the underlying thread. */
33 public void init() {
34 if (!synchronous) {
35 thread = new Thread(this);
36 thread.start();
37 }
38 }
39
40 /** Finish the remaining and destroy */
41 public void close(TestResult testResult) {
42 // FIXME: make behavior more robust when multiple results are
43 // registering this listener.
44 synchronized (partStructs) {
45 // TODO: put a timeout
46 while (partStructs.size() != 0) {
47 try {
48 partStructs.wait(500);
49 } catch (InterruptedException e) {
50 // silent
51 }
52 }
53 thread = null;
54 partStructs.notifyAll();
55 }
56 postClose((TreeTestResult) testResult);
57 }
58
59 public final void resultPartAdded(TestResult testResult,
60 TestResultPart testResultPart) {
61 TreeTestResult result = (TreeTestResult) testResult;
62 PartStruct partStruct = new PartStruct(result.getCurrentPath(), result
63 .getUuid(), testResultPart, result);
64
65 if (!synchronous) {
66 synchronized (partStructs) {
67 partStructs.add(partStruct);
68 partStructs.notifyAll();
69 }
70 } else {
71 resultPartAdded(partStruct);
72 }
73 }
74
75 /** Called when a result part has been added. */
76 protected abstract void resultPartAdded(PartStruct partStruct);
77
78 /**
79 * Called at the end of close. Default implementation is empty. To be
80 * overridden.
81 */
82 protected void postClose(TreeTestResult testResult) {
83
84 }
85
86 public void run() {
87 while (thread != null) {
88 synchronized (partStructs) {
89 for (PartStruct partStruct : partStructs) {
90 resultPartAdded(partStruct);
91 }
92
93 partStructs.clear();
94 partStructs.notifyAll();
95 while (partStructs.size() == 0) {
96 try {
97 partStructs.wait();
98 } catch (InterruptedException e) {
99 // silent
100 }
101 }
102 }
103 }
104 }
105
106 /** Structure used to pass tree specific information to subclasses. */
107 protected static class PartStruct {
108 /** The tree path of this part. */
109 public final TreeSPath path;
110 /** The test result id of the related test result */
111 public final String uuid;
112 /** The part itself */
113 public final TestResultPart part;
114 /** The tree test result itself. */
115 public final TreeTestResult result;
116
117 /** Constructor */
118 public PartStruct(TreeSPath path, String uuid, TestResultPart part,
119 TreeTestResult result) {
120 super();
121 this.path = path;
122 this.uuid = uuid;
123 this.part = part;
124 this.result = result;
125 }
126
127 }
128
129 public Boolean getSynchronous() {
130 return synchronous;
131 }
132
133 public void setSynchronous(Boolean synchronous) {
134 this.synchronous = synchronous;
135 }
136
137 }