001/* 
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018
019package org.apache.commons.exec;
020
021import java.util.Enumeration;
022import java.util.Vector;
023
024/**
025 * Generalization of <code>ExecuteWatchdog</code>
026 * 
027 * @see org.apache.commons.exec.ExecuteWatchdog
028 *
029 * @version $Id: Watchdog.java 1557338 2014-01-11 10:34:22Z sebb $
030 */
031public class Watchdog implements Runnable {
032
033    private final Vector<TimeoutObserver> observers = new Vector<TimeoutObserver>(1);
034
035    private final long timeout;
036
037    private boolean stopped = false;
038
039    public Watchdog(final long timeout) {
040        if (timeout < 1) {
041            throw new IllegalArgumentException("timeout must not be less than 1.");
042        }
043        this.timeout = timeout;
044    }
045
046    public void addTimeoutObserver(final TimeoutObserver to) {
047        observers.addElement(to);
048    }
049
050    public void removeTimeoutObserver(final TimeoutObserver to) {
051        observers.removeElement(to);
052    }
053
054    protected final void fireTimeoutOccured() {
055        final Enumeration<TimeoutObserver> e = observers.elements();
056        while (e.hasMoreElements()) {
057            e.nextElement().timeoutOccured(this);
058        }
059    }
060
061    public synchronized void start() {
062        stopped = false;
063        final Thread t = new Thread(this, "WATCHDOG");
064        t.setDaemon(true);
065        t.start();
066    }
067
068    public synchronized void stop() {
069        stopped = true;
070        notifyAll();
071    }
072
073    public void run() {
074        final long startTime = System.currentTimeMillis();
075        boolean isWaiting;
076        synchronized (this) {
077            long timeLeft = timeout - (System.currentTimeMillis() - startTime);
078            isWaiting = timeLeft > 0;
079            while (!stopped && isWaiting) {
080                try {
081                    wait(timeLeft);
082                } catch (final InterruptedException e) {
083                }
084                timeLeft = timeout - (System.currentTimeMillis() - startTime);
085                isWaiting = timeLeft > 0;
086            }
087        }
088
089        // notify the listeners outside of the synchronized block (see EXEC-60)
090        if (!isWaiting) {
091            fireTimeoutOccured();
092        }
093    }
094    
095}