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 * Base class for tags that will "use" threads.
27 *
28 * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
29 */
30
31 public abstract class UseThreadTag extends TagSupport {
32 /*** The thread to use in some way. */
33 private Thread thread = null;
34 /*** Threads can be grouped and acted on as a set */
35 private List threadGroup = null;
36 /*** If true doTag will search for a parent thread to use if setThread was not called */
37 private boolean searchForParent = true;
38
39 /***
40 * The default behavior is to either use the set thread or to
41 * search for a parent thread to use.
42 */
43 public void doTag(XMLOutput output) throws JellyTagException {
44 try {
45
46 if (thread != null) {
47 useThread(thread, output);
48 } else if (threadGroup != null) {
49 useThreadGroup(threadGroup, output);
50 } else {
51
52
53 if (searchForParent) {
54
55 ThreadTag tt = (ThreadTag) findAncestorWithClass(ThreadTag.class);
56
57 if (tt != null) {
58 useThread(tt.getThread(), output);
59 } else {
60
61 GroupTag gt = (GroupTag) findAncestorWithClass(GroupTag.class);
62 if (gt != null) {
63 useThreadGroup(gt.getThreads(), output);
64 } else {
65 throw new JellyTagException("no thread or thread group found");
66 }
67 }
68 } else {
69 throw new JellyTagException("no thread or thread group found");
70 }
71 }
72 }
73 catch (InterruptedException e) {
74 throw new JellyTagException(e);
75 }
76 }
77
78 /*** Implement this method to do something with the thread */
79 protected abstract void useThread(Thread thread, XMLOutput output) throws InterruptedException ;
80
81 /*** Implement this method to do something with the threadGroup */
82 protected abstract void useThreadGroup(List threadGroup, XMLOutput output) throws InterruptedException ;
83
84 /***
85 * Set the thread to use in some way.
86 */
87 public void setThread(Thread thread) {
88 this.thread = thread;
89 }
90
91 /***
92 * Get a reference to the thread to use
93 */
94 public Thread getThread() {
95 return thread;
96 }
97
98 /***
99 * Set the thread group to "use".
100 * @param threadGroup The threadGroup created with the <i>group</i> tag.
101 */
102 public void setThreadGroup(List threadGroup) {
103 this.threadGroup = threadGroup;
104 }
105
106 /***
107 * Get the thread group
108 */
109 public List getThreadGroup() {
110 return threadGroup;
111 }
112
113 /***
114 * If true the tag will search for a parent thread tag to "use" if
115 * no thread was set via <i>setThread</i>. This is <i>true</i> by default.
116 */
117 public void setSearchForParentThread(boolean searchForParent) {
118 this.searchForParent = searchForParent;
119 }
120 }