View Javadoc

1   /*
2   * Copyright 2004 The Apache Software Foundation
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  
17  package javax.servlet;
18  
19  
20  /**
21   * Defines an exception that a servlet or filter throws to indicate
22   * that it is permanently or temporarily unavailable. 
23   *
24   * <p>When a servlet or filter is permanently unavailable, something is wrong
25   * with it, and it cannot handle
26   * requests until some action is taken. For example, a servlet
27   * might be configured incorrectly, or a filter's state may be corrupted.
28   * The component should log both the error and the corrective action
29   * that is needed.
30   *
31   * <p>A servlet or filter is temporarily unavailable if it cannot handle
32   * requests momentarily due to some system-wide problem. For example,
33   * a third-tier server might not be accessible, or there may be 
34   * insufficient memory or disk storage to handle requests. A system
35   * administrator may need to take corrective action.
36   *
37   * <p>Servlet containers can safely treat both types of unavailable
38   * exceptions in the same way. However, treating temporary unavailability
39   * effectively makes the servlet container more robust. Specifically,
40   * the servlet container might block requests to the servlet or filter for a period
41   * of time suggested by the exception, rather than rejecting them until
42   * the servlet container restarts.
43   *
44   *
45   * @author 	Various
46   * @version 	$Version$
47   *
48   */
49  
50  public class UnavailableException
51  extends ServletException {
52  
53      private Servlet     servlet;           // what's unavailable
54      private boolean     permanent;         // needs admin action?
55      private int         seconds;           // unavailability estimate
56  
57      /**
58       * 
59       * @deprecated	As of Java Servlet API 2.2, use {@link
60       * 			#UnavailableException(String)} instead.
61       *
62       * @param servlet 	the <code>Servlet</code> instance that is
63       *                  unavailable
64       *
65       * @param msg 	a <code>String</code> specifying the
66       *                  descriptive message
67       *
68       */
69  
70      public UnavailableException(Servlet servlet, String msg) {
71  	super(msg);
72  	this.servlet = servlet;
73  	permanent = true;
74      }
75   
76      /**
77       * @deprecated	As of Java Servlet API 2.2, use {@link
78       *			#UnavailableException(String, int)} instead.
79       *
80       * @param seconds	an integer specifying the number of seconds
81       * 			the servlet expects to be unavailable; if
82       *			zero or negative, indicates that the servlet
83       *			can't make an estimate
84       *
85       * @param servlet	the <code>Servlet</code> that is unavailable
86       * 
87       * @param msg	a <code>String</code> specifying the descriptive 
88       *			message, which can be written to a log file or 
89       *			displayed for the user.
90       *
91       */
92      
93      public UnavailableException(int seconds, Servlet servlet, String msg) {
94  	super(msg);
95  	this.servlet = servlet;
96  	if (seconds <= 0)
97  	    this.seconds = -1;
98  	else
99  	    this.seconds = seconds;
100 	permanent = false;
101     }
102 
103     /**
104      * 
105      * Constructs a new exception with a descriptive
106      * message indicating that the servlet is permanently
107      * unavailable.
108      *
109      * @param msg 	a <code>String</code> specifying the
110      *                  descriptive message
111      *
112      */
113 
114     public UnavailableException(String msg) {
115 	super(msg);
116 
117 	permanent = true;
118     }
119 
120     /**
121      * Constructs a new exception with a descriptive message
122      * indicating that the servlet is temporarily unavailable
123      * and giving an estimate of how long it will be unavailable.
124      * 
125      * <p>In some cases, the servlet cannot make an estimate. For
126      * example, the servlet might know that a server it needs is
127      * not running, but not be able to report how long it will take
128      * to be restored to functionality. This can be indicated with
129      * a negative or zero value for the <code>seconds</code> argument.
130      *
131      * @param msg	a <code>String</code> specifying the
132      *                  descriptive message, which can be written
133      *                  to a log file or displayed for the user.
134      *
135      * @param seconds	an integer specifying the number of seconds
136      * 			the servlet expects to be unavailable; if
137      *			zero or negative, indicates that the servlet
138      *			can't make an estimate
139      *
140      */
141     
142     public UnavailableException(String msg, int seconds) {
143 	super(msg);
144 
145 	if (seconds <= 0)
146 	    this.seconds = -1;
147 	else
148 	    this.seconds = seconds;
149 
150 	permanent = false;
151     }
152 
153     /**
154      *
155      * Returns a <code>boolean</code> indicating
156      * whether the servlet is permanently unavailable.
157      * If so, something is wrong with the servlet, and the
158      * system administrator must take some corrective action.
159      *
160      * @return		<code>true</code> if the servlet is
161      *			permanently unavailable; <code>false</code>
162      *			if the servlet is available or temporarily
163      *			unavailable
164      *
165      */
166      
167     public boolean isPermanent() {
168 	return permanent;
169     }
170   
171     /**
172      * @deprecated	As of Java Servlet API 2.2, with no replacement.
173      *
174      * Returns the servlet that is reporting its unavailability.
175      * 
176      * @return		the <code>Servlet</code> object that is 
177      *			throwing the <code>UnavailableException</code>
178      *
179      */
180      
181     public Servlet getServlet() {
182 	return servlet;
183     }
184 
185     /**
186      * Returns the number of seconds the servlet expects to 
187      * be temporarily unavailable.  
188      *
189      * <p>If this method returns a negative number, the servlet
190      * is permanently unavailable or cannot provide an estimate of
191      * how long it will be unavailable. No effort is
192      * made to correct for the time elapsed since the exception was
193      * first reported.
194      *
195      * @return		an integer specifying the number of seconds
196      *			the servlet will be temporarily unavailable,
197      *			or a negative number if the servlet is permanently
198      *			unavailable or cannot make an estimate
199      *
200      */
201      
202     public int getUnavailableSeconds() {
203 	return permanent ? -1 : seconds;
204     }
205 }