1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ThreadedAction.java,v 1.5 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import EDU.oswego.cs.dl.util.concurrent.Semaphore;
48
49
50 /***
51 * Helper class to run an action in a separate thread, and catch any
52 * exception that the action generates.
53 *
54 * @version $Revision: 1.5 $ $Date: 2004/01/31 13:44:24 $
55 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
56 */
57 public abstract class ThreadedAction extends Thread {
58
59 /***
60 * The completion listener. May be <code>null</code>.
61 */
62 private CompletionListener _listener = null;
63
64 /***
65 * Semaphore to enable synchronization on completion
66 */
67 private Semaphore _completedLock = new Semaphore(0);
68
69 /***
70 * The exception generated by the action, or <code>null</code>, if no
71 * exception was thrown
72 */
73 private volatile Exception _exception = null;
74
75 /***
76 * The time the action started
77 */
78 private volatile long _start;
79
80 /***
81 * The time the action ended
82 */
83 private volatile long _end;
84
85
86 /***
87 * Construct a new <code>ThreadedAction</code>
88 */
89 public ThreadedAction() {
90 }
91
92 /***
93 * Construct a new <code>ThreadedAction</code>, with a listener to notify
94 * on completion
95 *
96 * @param listener the listener to notify on completion
97 */
98 public ThreadedAction(CompletionListener listener) {
99 _listener = listener;
100 }
101
102 /***
103 * Run the action. This delegates to {@link #runProtected}.<br/>
104 * If a {@link CompletionListener} was supplied, it will be notified
105 * on completion of the action.
106 */
107 public void run() {
108 try {
109 _start = System.currentTimeMillis();
110 runProtected();
111 } catch (Exception exception) {
112 setException(exception);
113 } finally {
114 _end = System.currentTimeMillis();
115 }
116 _completedLock.release();
117 if (_listener != null) {
118 _listener.completed();
119 }
120 }
121
122 /***
123 * Run the action
124 *
125 * @throws Exception for any error
126 */
127 public abstract void runProtected() throws Exception;
128
129 /***
130 * Returns the elapsed time of the action. This is the time
131 * taken to run {@link #runProtected}
132 *
133 * @return the elapsed time of the action, in milliseconds
134 */
135 public long getElapsedTime() {
136 return _end - _start;
137 }
138
139 /***
140 * Returns any exception thrown by {@link #runProtected}, or
141 * <code>null</code>, if no exception was thrown
142 *
143 * @return any exception thrown, or <code>null</code>, if none was
144 * thrown
145 */
146 public Exception getException() {
147 return _exception;
148 }
149
150 /***
151 * Wait for the action to complete
152 *
153 * @throws InterruptedException if interrupted
154 */
155 public void waitForCompletion() throws InterruptedException {
156 _completedLock.acquire();
157 _completedLock.release();
158 }
159
160 /***
161 * Wait for the action to complete
162 *
163 * @param timeout the number of milleseconds to wait. An argument less
164 * than or equal to zero means not to wait at all
165 * @throws InterruptedException if interrupted while waiting
166 * @return <code>true</code> if the action completed in the given time
167 * frame
168 */
169 public boolean waitForCompletion(long timeout)
170 throws InterruptedException {
171 boolean completed = _completedLock.attempt(timeout);
172 if (completed) {
173 _completedLock.release();
174 }
175 return completed;
176 }
177
178 /***
179 * Set the exception
180 *
181 * @param exception the exception
182 */
183 protected void setException(Exception exception) {
184 _exception = exception;
185 }
186
187 }
This page was automatically generated by Maven