%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.threads.WaitForTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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 | 32 | public class WaitForTag extends TagSupport { |
34 | 16 | private int status = RunnableStatus.NONE; |
35 | 16 | private JellyThread thread = null; |
36 | 16 | private List group = null; |
37 | 16 | 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 | 6 | this.status = RunnableStatus.getStatusCode(status); |
46 | 6 | } |
47 | ||
48 | /** |
|
49 | * Which thread will this tag check the status of |
|
50 | */ |
|
51 | public void setThread(JellyThread thread) { |
|
52 | 14 | this.thread = thread; |
53 | 14 | } |
54 | ||
55 | /** |
|
56 | * Set the group of threads to wait on |
|
57 | */ |
|
58 | public void setGroup(List group) { |
|
59 | 2 | this.group = group; |
60 | 2 | } |
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 | 2 | this.onlyWait = onlyWait; |
68 | 2 | } |
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 | 16 | if (thread == null && group == class="keyword">null) { |
77 | 0 | throw new JellyTagException("This tag requires that you set the thread or group attribute"); |
78 | } |
|
79 | ||
80 | // wait on the thread |
|
81 | 16 | if (thread != null) { |
82 | 14 | thread.waitUntilDone(onlyWait); |
83 | 12 | if (status != RunnableStatus.NONE) { |
84 | 6 | if (!thread.getStatus().equals(status)) { |
85 | 2 | throw new RequirementException("Requirement on thread \"" + thread.getName() + "\" not met"); |
86 | } |
|
87 | } |
|
88 | } |
|
89 | ||
90 | // wait on the threadgroup |
|
91 | 12 | if (group != null) { |
92 | 4 | for (int i = 0; i < group.size(); i++) { |
93 | 4 | JellyThread gthread = (JellyThread) group.get(i); |
94 | 4 | gthread.waitUntilDone(onlyWait); |
95 | 2 | if (status != RunnableStatus.NONE) { |
96 | 0 | if (!gthread.getStatus().equals(status)) { |
97 | 0 | throw new RequirementException("Requirement on thread \"" + gthread.getName() + "\" not met"); |
98 | } |
|
99 | } |
|
100 | } |
|
101 | } |
|
102 | 10 | } |
103 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |