/*-----------------------------------------------------------------------------
 *
 * Enhydra JdbcS
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright 1997-2000 Lutris Technologies Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * www.enhydra.org/license/epl.html
 *
 * Software distributed under the License is distributed on an "ASIS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * -----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */
package org.enhydra.jdbc.pool;

/**
 * PoolKeeper class allows to clean up automatically dead and
 * expired objects.
 */
public class PoolKeeper implements Runnable {

    private long 		sleepTime; 	// timeout between 2 clean up
    private GenericPool 	pool;		// the pool to clean up
    private boolean             running = true;
    /**
     * constructor, called by GenericPool (any kind of object)
     */
    public PoolKeeper(long sleepTime, GenericPool pool) {
	this.sleepTime = sleepTime;
	this.pool = pool;
    } // constructor

    public void stop() {
        running = false;
    }

    /**
     * run method. allows to clean up the pool
     */
    public void run() {
	while (running) {
	    try {
		synchronized(this) {
		    wait(this.sleepTime);			// wait for timeout ms before attack
		}
	    } catch(InterruptedException e) {}
            this.pool.cleanUp();			// clean up the Pool and reallocate objects
	}
        // release the pool.
        this.pool = null;
    } // method run

} // class PoolKeeper

