PollThread.java :  » Workflow-Engines » obe-1.0 » org » obe » client » Java Open Source

Java Open Source » Workflow Engines » obe 1.0 
obe 1.0 » org » obe » client » PollThread.java
/*--

 Copyright (C) 2002 Anthony Eden.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows
    these conditions in the documentation and/or other materials
    provided with the distribution.

 3. The names "OBE" and "Open Business Engine" must not be used to
   endorse or promote products derived from this software without prior
   written permission.  For written permission, please contact
   me@anthonyeden.com.

 4. Products derived from this software may not be called "OBE" or
   "Open Business Engine", nor may "OBE" or "Open Business Engine"
   appear in their name, without prior written permission from
   Anthony Eden (me@anthonyeden.com).

 In addition, I request (but do not require) that you include in the
 end-user documentation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by
      Anthony Eden (http://www.anthonyeden.com/)."

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

 For more information on OBE, please see <http://obe.sourceforge.net/>.

 */

package org.obe.client;

import com.anthonyeden.lib.event.StatusListener;
import com.anthonyeden.lib.event.StatusListenerSupport;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * A thread which can poll Pollable classes at regular intervals.
 * <p/>
 * <p>The PollThread is not started until the <code>startPolling()</code>
 * method is invoked.  PollThreads can poll as many resources as necessary.
 * Pollable resources are added to the thread with the <code>add(Pollable p)</code>
 * method.  If a Pollable object is already in the PollThread's list of
 * resources then it will not be added again.  Resources can be added after
 * polling has been started.</p>
 * <p/>
 * <p>Here is an example of creating a poll thread:</p>
 * <pre>
 * PollThread t = new PollThread();
 * t.add(pollable);
 * t.startPolling();
 * </pre>
 *
 * @author Anthony Eden
 */
public class PollThread implements Runnable {
    /**
     * The default pause time (10 seconds).
     */
    public static final int DEFAULT_PAUSE_TIME = 10000;

    private int pauseTime;
    private Thread thread;
    private boolean running;
    private List pollObjects = new ArrayList();
    private StatusListenerSupport statusListenerSupport;

    /**
     * Construct a new PollThread whose poll delay is specified by
     * the <code>DEFAULT_PAUSE_TIME</code> value.
     */

    public PollThread() {
        this(DEFAULT_PAUSE_TIME);
    }

    /**
     * Construct a new PollThread which will poll every <code>pauseTime</code>
     * milliseconds.
     *
     * @param pauseTime The poll delay in milliseconds
     */

    public PollThread(int pauseTime) {
        this.pauseTime = pauseTime;
        statusListenerSupport = new StatusListenerSupport(this);
    }

    /**
     * Add a status listener.
     *
     * @param l The status listener
     */

    public void addStatusListener(StatusListener l) {
        statusListenerSupport.addStatusListener(l);
    }

    /**
     * Remove a status listener.
     *
     * @param l The status listener
     */

    public void removeStatusListener(StatusListener l) {
        statusListenerSupport.removeStatusListener(l);
    }

    /**
     * Get the pause time in milliseconds.
     *
     * @return The pause time
     */

    public int getPauseTime() {
        return pauseTime;
    }

    /**
     * Set the pause time in milliseconds.
     *
     * @param pauseTime The new pause time in milliseconds
     */

    public void setPauseTime(int pauseTime) {
        this.pauseTime = pauseTime;
    }

    /**
     * Start polling.
     */

    public void startPolling() {
        if (!running) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    /**
     * Stop polling.
     */

    public void stopPolling() {
        if (running) {
            running = false;
            thread.interrupt();
        }
    }

    /**
     * Implementation method from the Runnable interface.
     */

    public void run() {
        while (running) {

            poll();

            if (!running) {
                return;
            }

            try {
                Thread.sleep(pauseTime);
            } catch (InterruptedException e) {
                // ignore
            }
        }
    }

    /**
     * Add the given Pollable object.
     *
     * @param p The Pollable object
     */

    public void add(Pollable p) {
        if (!pollObjects.contains(p)) {
            pollObjects.add(p);
        }
    }

    /**
     * Remove the given Pollable object.
     *
     * @param p The Pollable object
     */

    public void remove(Pollable p) {
        pollObjects.remove(p);
    }

    /**
     * Execute a poll on all Pollable objects in the stack.
     */

    public void poll() {
        Iterator iter = pollObjects.iterator();
        while (iter.hasNext()) {
            if (!running) {
                return;
            }

            Pollable p = (Pollable)iter.next();
            statusListenerSupport.fireStatusChanged("Polling");
            p.poll();
        }

        statusListenerSupport.fireStatusChanged("");
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.