%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.threads.JellyThread |
|
|
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 | package org.apache.commons.jelly.tags.threads; |
|
17 | ||
18 | import org.apache.commons.logging.Log; |
|
19 | import org.apache.commons.logging.LogFactory; |
|
20 | ||
21 | /** |
|
22 | * Adds some functionality to the jdk thread class. |
|
23 | * |
|
24 | * @author <a href="mailto:jason@jhorman.org">Jason Horman</a> |
|
25 | */ |
|
26 | ||
27 | 2 | public class JellyThread extends Thread { |
28 | /** The Log to which logging calls will be made. */ |
|
29 | 2 | private static final Log log = LogFactory.getLog(ThreadTag.class); |
30 | ||
31 | /** While this thread is still running it owns this mutex */ |
|
32 | 76 | private Mutex runningMutex = new Mutex(); |
33 | /** The Runnable target */ |
|
34 | 76 | private Runnable target = null; |
35 | ||
36 | /** Tracks the status of this thread */ |
|
37 | 76 | RunnableStatus status = new RunnableStatus(); |
38 | ||
39 | 76 | public JellyThread() { |
40 | // aquire my still running lock immediately |
|
41 | 76 | while (true) { |
42 | try { |
|
43 | 76 | runningMutex.acquire(); |
44 | 76 | break; |
45 | 0 | } catch (InterruptedException e) { |
46 | } |
|
47 | } |
|
48 | 76 | } |
49 | ||
50 | /** |
|
51 | * Set the Runnable target that will be run |
|
52 | */ |
|
53 | public void setTarget(Runnable target) { |
|
54 | 38 | this.target = target; |
55 | 38 | } |
56 | ||
57 | /** |
|
58 | * Run the thread |
|
59 | */ |
|
60 | public void run() { |
|
61 | 38 | log.debug("Starting thread \"" + getName() + "\""); |
62 | ||
63 | // run the runnable item |
|
64 | try { |
|
65 | ||
66 | 38 | log.debug("Thread \"" + getName() + "\" running"); |
67 | 38 | target.run(); |
68 | ||
69 | // as long as there were no runtime exceptions set SUCCESS |
|
70 | 28 | status.set(RunnableStatus.SUCCESS); |
71 | ||
72 | 28 | } catch(RequirementException e) { |
73 | ||
74 | 2 | status.set(RunnableStatus.AVOIDED); |
75 | 2 | log.warn("Thread \"" + getName() + "\" avoided, " + e.getMessage()); |
76 | ||
77 | 2 | } catch(TimeoutException e) { |
78 | ||
79 | 2 | status.set(RunnableStatus.AVOIDED); |
80 | 2 | log.warn("Thread \"" + getName() + "\" avoided, " + e.getMessage()); |
81 | ||
82 | 2 | } catch (Exception e) { |
83 | ||
84 | // runtime exceptions will cause a status of FAILURE |
|
85 | 2 | status.set(RunnableStatus.FAILURE, e); |
86 | 2 | log.error("Thread \"" + getName() + "\" failure, " + e.getMessage()); |
87 | 2 | log.debug(e); |
88 | ||
89 | } |
|
90 | ||
91 | // release the i'm still running mutex |
|
92 | 34 | runningMutex.release(); |
93 | ||
94 | 34 | log.debug("Thread \"" + getName() + "\" finished"); |
95 | 34 | } |
96 | ||
97 | /** |
|
98 | * Call this method from a different thread to wait until this thread is done. This |
|
99 | * is used by the {@link WaitForTag} class. |
|
100 | */ |
|
101 | public void waitUntilDone(long howLong) throws TimeoutException { |
|
102 | 18 | if (Thread.currentThread() == this) { |
103 | 0 | throw new RuntimeException("This method should be called from a different thread than itself"); |
104 | } |
|
105 | ||
106 | // wait until the calling thread can aquire the lock |
|
107 | 0 | while (true) { |
108 | try { |
|
109 | 18 | if (howLong == -1) { |
110 | 16 | runningMutex.acquire(); |
111 | 14 | break; |
112 | 2 | } else if (!runningMutex.attempt(howLong)) { |
113 | 2 | throw new TimeoutException("max wait time exceeded"); |
114 | } |
|
115 | 0 | } catch (InterruptedException e) { |
116 | } |
|
117 | } |
|
118 | ||
119 | // release the lock, just needed it to get started |
|
120 | 14 | runningMutex.release(); |
121 | 14 | } |
122 | ||
123 | /** Get the status of this thread */ |
|
124 | public RunnableStatus getStatus() { |
|
125 | 6 | return status; |
126 | } |
|
127 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |