//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE----------------------------

package org.huihoo.workflow.selector;


public class UnavailableException extends SelectorException
{
	private boolean permanent; // needs admin action?
	private int seconds; // unavailability estimate

	public UnavailableException()
	{
		super();
	}

	public UnavailableException(String message)
	{
		super(message);
		permanent = true;
	}

	/**
	 * Constructs a new exception with a descriptive message
	 * indicating that the selector is temporarily unavailable
	 * and giving an estimate of how long it will be unavailable.
	 * 
	 * <p>In some cases, the selector cannot make an estimate. For
	 * example, the selector might know that a server it needs is
	 * not running, but not be able to report how long it will take
	 * to be restored to functionality. This can be indicated with
	 * a negative or zero value for the <code>seconds</code> argument.
	 *
	 * @param msg	a <code>String</code> specifying the
	 *                  descriptive message, which can be written
	 *                  to a log file or displayed for the user.
	 *
	 * @param seconds	an integer specifying the number of seconds
	 * 			the selector expects to be unavailable; if
	 *			zero or negative, indicates that the selector
	 *			can't make an estimate
	 *
	 */

	public UnavailableException(String message, int seconds)
	{
		super(message);

		if (seconds <= 0)
			this.seconds = -1;
		else
			this.seconds = seconds;

		permanent = false;
	}

	public UnavailableException(String message, Throwable rootCause)
	{
		super(message, rootCause);
		permanent = true;
	}

	public UnavailableException(Throwable rootCause)
	{
		super(rootCause.getLocalizedMessage());
		permanent = true;
	}

	/**
	 *
	 * Returns a <code>boolean</code> indicating
	 * whether the selector is permanently unavailable.
	 * If so, something is wrong with the selector, and the
	 * system administrator must take some corrective action.
	 *
	 * @return		<code>true</code> if the selector is
	 *			permanently unavailable; <code>false</code>
	 *			if the selector is available or temporarily
	 *			unavailable
	 *
	 */

	public boolean isPermanent()
	{
		return permanent;
	}

	/**
	 * Returns the number of seconds the selector expects to 
	 * be temporarily unavailable.  
	 *
	 * <p>If this method returns a negative number, the selector
	 * is permanently unavailable or cannot provide an estimate of
	 * how long it will be unavailable. No effort is
	 * made to correct for the time elapsed since the exception was
	 * first reported.
	 *
	 * @return		an integer specifying the number of seconds
	 *			the selector will be temporarily unavailable,
	 *			or a negative number if the selector is permanently
	 *			unavailable or cannot make an estimate
	 *
	 */

	public int getUnavailableSeconds()
	{
		return permanent ? -1 : seconds;
	}
}