001: /*--
002:
003: Copyright (C) 2002 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: me@anthonyeden.com.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Anthony Eden (me@anthonyeden.com).
027:
028: In addition, I request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by
032: Anthony Eden (http://www.anthonyeden.com/)."
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
039: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
040: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
041: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
042: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
043: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
044: POSSIBILITY OF SUCH DAMAGE.
045:
046: For more information on OBE, please see <http://obe.sourceforge.net/>.
047:
048: */
049:
050: package org.obe.client;
051:
052: import com.anthonyeden.lib.event.StatusListener;
053: import com.anthonyeden.lib.event.StatusListenerSupport;
054:
055: import java.util.ArrayList;
056: import java.util.Iterator;
057: import java.util.List;
058:
059: /**
060: * A thread which can poll Pollable classes at regular intervals.
061: * <p/>
062: * <p>The PollThread is not started until the <code>startPolling()</code>
063: * method is invoked. PollThreads can poll as many resources as necessary.
064: * Pollable resources are added to the thread with the <code>add(Pollable p)</code>
065: * method. If a Pollable object is already in the PollThread's list of
066: * resources then it will not be added again. Resources can be added after
067: * polling has been started.</p>
068: * <p/>
069: * <p>Here is an example of creating a poll thread:</p>
070: * <pre>
071: * PollThread t = new PollThread();
072: * t.add(pollable);
073: * t.startPolling();
074: * </pre>
075: *
076: * @author Anthony Eden
077: */
078: public class PollThread implements Runnable {
079: /**
080: * The default pause time (10 seconds).
081: */
082: public static final int DEFAULT_PAUSE_TIME = 10000;
083:
084: private int pauseTime;
085: private Thread thread;
086: private boolean running;
087: private List pollObjects = new ArrayList();
088: private StatusListenerSupport statusListenerSupport;
089:
090: /**
091: * Construct a new PollThread whose poll delay is specified by
092: * the <code>DEFAULT_PAUSE_TIME</code> value.
093: */
094:
095: public PollThread() {
096: this (DEFAULT_PAUSE_TIME);
097: }
098:
099: /**
100: * Construct a new PollThread which will poll every <code>pauseTime</code>
101: * milliseconds.
102: *
103: * @param pauseTime The poll delay in milliseconds
104: */
105:
106: public PollThread(int pauseTime) {
107: this .pauseTime = pauseTime;
108: statusListenerSupport = new StatusListenerSupport(this );
109: }
110:
111: /**
112: * Add a status listener.
113: *
114: * @param l The status listener
115: */
116:
117: public void addStatusListener(StatusListener l) {
118: statusListenerSupport.addStatusListener(l);
119: }
120:
121: /**
122: * Remove a status listener.
123: *
124: * @param l The status listener
125: */
126:
127: public void removeStatusListener(StatusListener l) {
128: statusListenerSupport.removeStatusListener(l);
129: }
130:
131: /**
132: * Get the pause time in milliseconds.
133: *
134: * @return The pause time
135: */
136:
137: public int getPauseTime() {
138: return pauseTime;
139: }
140:
141: /**
142: * Set the pause time in milliseconds.
143: *
144: * @param pauseTime The new pause time in milliseconds
145: */
146:
147: public void setPauseTime(int pauseTime) {
148: this .pauseTime = pauseTime;
149: }
150:
151: /**
152: * Start polling.
153: */
154:
155: public void startPolling() {
156: if (!running) {
157: running = true;
158: thread = new Thread(this );
159: thread.start();
160: }
161: }
162:
163: /**
164: * Stop polling.
165: */
166:
167: public void stopPolling() {
168: if (running) {
169: running = false;
170: thread.interrupt();
171: }
172: }
173:
174: /**
175: * Implementation method from the Runnable interface.
176: */
177:
178: public void run() {
179: while (running) {
180:
181: poll();
182:
183: if (!running) {
184: return;
185: }
186:
187: try {
188: Thread.sleep(pauseTime);
189: } catch (InterruptedException e) {
190: // ignore
191: }
192: }
193: }
194:
195: /**
196: * Add the given Pollable object.
197: *
198: * @param p The Pollable object
199: */
200:
201: public void add(Pollable p) {
202: if (!pollObjects.contains(p)) {
203: pollObjects.add(p);
204: }
205: }
206:
207: /**
208: * Remove the given Pollable object.
209: *
210: * @param p The Pollable object
211: */
212:
213: public void remove(Pollable p) {
214: pollObjects.remove(p);
215: }
216:
217: /**
218: * Execute a poll on all Pollable objects in the stack.
219: */
220:
221: public void poll() {
222: Iterator iter = pollObjects.iterator();
223: while (iter.hasNext()) {
224: if (!running) {
225: return;
226: }
227:
228: Pollable p = (Pollable) iter.next();
229: statusListenerSupport.fireStatusChanged("Polling");
230: p.poll();
231: }
232:
233: statusListenerSupport.fireStatusChanged("");
234: }
235: }
|