1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.threads;
18
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.TagSupport;
21 import org.apache.commons.jelly.XMLOutput;
22
23 import java.util.List;
24
25 /***
26 * This tag creates a dependency on another thread. If onlyWait is set
27 * a {@link TimeoutException} can be thrown. If status is set a {@link RequirementException}
28 * can be thrown.
29 *
30 * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
31 */
32
33 public class WaitForTag extends TagSupport {
34 private int status = RunnableStatus.NONE;
35 private JellyThread thread = null;
36 private List group = null;
37 private long onlyWait = -1;
38
39 /***
40 * Wait for a specific status. "SUCCESS", "FAILURE", "TIMED_OUT", or "AVOIDED". If
41 * waiting on a thread group each thread in the group will have to have this status
42 * set.
43 */
44 public void setStatus(String status) {
45 this.status = RunnableStatus.getStatusCode(status);
46 }
47
48 /***
49 * Which thread will this tag check the status of
50 */
51 public void setThread(JellyThread thread) {
52 this.thread = thread;
53 }
54
55 /***
56 * Set the group of threads to wait on
57 */
58 public void setGroup(List group) {
59 this.group = group;
60 }
61
62 /***
63 * Set how long to wait for the thread to finish. If waiting for a group
64 * this will be the time to wait for each thread in the group to finish.
65 */
66 public void setOnlyWait(long onlyWait) {
67 this.onlyWait = onlyWait;
68 }
69
70 /***
71 * Check the requirements
72 * @throws TimeoutException If the call to waitUntilDone(onlyWait) times out
73 * @throws RequirementException If a threads status doesn't match the setStatus() value
74 */
75 public void doTag(XMLOutput output) throws TimeoutException, RequirementException, JellyTagException {
76 if (thread == null && group == null) {
77 throw new JellyTagException("This tag requires that you set the thread or group attribute");
78 }
79
80
81 if (thread != null) {
82 thread.waitUntilDone(onlyWait);
83 if (status != RunnableStatus.NONE) {
84 if (!thread.getStatus().equals(status)) {
85 throw new RequirementException("Requirement on thread \"" + thread.getName() + "\" not met");
86 }
87 }
88 }
89
90
91 if (group != null) {
92 for (int i = 0; i < group.size(); i++) {
93 JellyThread gthread = (JellyThread) group.get(i);
94 gthread.waitUntilDone(onlyWait);
95 if (status != RunnableStatus.NONE) {
96 if (!gthread.getStatus().equals(status)) {
97 throw new RequirementException("Requirement on thread \"" + gthread.getName() + "\" not met");
98 }
99 }
100 }
101 }
102 }
103 }