001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    
017    package javax.servlet;
018    
019    
020    /**
021     * Defines an exception that a servlet or filter throws to indicate
022     * that it is permanently or temporarily unavailable. 
023     *
024     * <p>When a servlet or filter is permanently unavailable, something is wrong
025     * with it, and it cannot handle
026     * requests until some action is taken. For example, a servlet
027     * might be configured incorrectly, or a filter's state may be corrupted.
028     * The component should log both the error and the corrective action
029     * that is needed.
030     *
031     * <p>A servlet or filter is temporarily unavailable if it cannot handle
032     * requests momentarily due to some system-wide problem. For example,
033     * a third-tier server might not be accessible, or there may be 
034     * insufficient memory or disk storage to handle requests. A system
035     * administrator may need to take corrective action.
036     *
037     * <p>Servlet containers can safely treat both types of unavailable
038     * exceptions in the same way. However, treating temporary unavailability
039     * effectively makes the servlet container more robust. Specifically,
040     * the servlet container might block requests to the servlet or filter for a period
041     * of time suggested by the exception, rather than rejecting them until
042     * the servlet container restarts.
043     *
044     *
045     * @author      Various
046     * @version     $Version$
047     *
048     */
049    
050    public class UnavailableException
051    extends ServletException {
052    
053        private Servlet     servlet;           // what's unavailable
054        private boolean     permanent;         // needs admin action?
055        private int         seconds;           // unavailability estimate
056    
057        /**
058         * 
059         * @deprecated      As of Java Servlet API 2.2, use {@link
060         *                  #UnavailableException(String)} instead.
061         *
062         * @param servlet   the <code>Servlet</code> instance that is
063         *                  unavailable
064         *
065         * @param msg       a <code>String</code> specifying the
066         *                  descriptive message
067         *
068         */
069    
070        public UnavailableException(Servlet servlet, String msg) {
071            super(msg);
072            this.servlet = servlet;
073            permanent = true;
074        }
075     
076        /**
077         * @deprecated      As of Java Servlet API 2.2, use {@link
078         *                  #UnavailableException(String, int)} instead.
079         *
080         * @param seconds   an integer specifying the number of seconds
081         *                  the servlet expects to be unavailable; if
082         *                  zero or negative, indicates that the servlet
083         *                  can't make an estimate
084         *
085         * @param servlet   the <code>Servlet</code> that is unavailable
086         * 
087         * @param msg       a <code>String</code> specifying the descriptive 
088         *                  message, which can be written to a log file or 
089         *                  displayed for the user.
090         *
091         */
092        
093        public UnavailableException(int seconds, Servlet servlet, String msg) {
094            super(msg);
095            this.servlet = servlet;
096            if (seconds <= 0)
097                this.seconds = -1;
098            else
099                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    }