001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz;
022:
023: import java.io.PrintStream;
024: import java.io.PrintWriter;
025:
026: import org.quartz.utils.ExceptionHelper;
027:
028: /**
029: * <p>
030: * Base class for exceptions thrown by the Quartz <code>{@link Scheduler}</code>.
031: * </p>
032: *
033: * <p>
034: * <code>SchedulerException</code> s may contain a reference to another
035: * <code>Exception</code>, which was the underlying cause of the <code>SchedulerException</code>.
036: * </p>
037: *
038: * @author James House
039: */
040: public class SchedulerException extends Exception {
041:
042: /*
043: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
044: *
045: * Constants.
046: *
047: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
048: */
049:
050: public static final int ERR_UNSPECIFIED = 0;
051:
052: public static final int ERR_BAD_CONFIGURATION = 50;
053:
054: public static final int ERR_TIME_BROKER_FAILURE = 70;
055:
056: public static final int ERR_CLIENT_ERROR = 100;
057:
058: public static final int ERR_COMMUNICATION_FAILURE = 200;
059:
060: public static final int ERR_UNSUPPORTED_FUNCTION_IN_THIS_CONFIGURATION = 210;
061:
062: public static final int ERR_PERSISTENCE = 400;
063:
064: public static final int ERR_PERSISTENCE_JOB_DOES_NOT_EXIST = 410;
065:
066: public static final int ERR_PERSISTENCE_CALENDAR_DOES_NOT_EXIST = 420;
067:
068: public static final int ERR_PERSISTENCE_TRIGGER_DOES_NOT_EXIST = 430;
069:
070: public static final int ERR_PERSISTENCE_CRITICAL_FAILURE = 499;
071:
072: public static final int ERR_THREAD_POOL = 500;
073:
074: public static final int ERR_THREAD_POOL_EXHAUSTED = 510;
075:
076: public static final int ERR_THREAD_POOL_CRITICAL_FAILURE = 599;
077:
078: public static final int ERR_JOB_LISTENER = 600;
079:
080: public static final int ERR_JOB_LISTENER_NOT_FOUND = 610;
081:
082: public static final int ERR_TRIGGER_LISTENER = 700;
083:
084: public static final int ERR_TRIGGER_LISTENER_NOT_FOUND = 710;
085:
086: public static final int ERR_JOB_EXECUTION_THREW_EXCEPTION = 800;
087:
088: public static final int ERR_TRIGGER_THREW_EXCEPTION = 850;
089:
090: /*
091: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
092: *
093: * Data members.
094: *
095: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
096: */
097:
098: private Throwable cause;
099:
100: private int errorCode = ERR_UNSPECIFIED;
101:
102: /*
103: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104: *
105: * Constructors.
106: *
107: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108: */
109:
110: public SchedulerException() {
111: super ();
112: }
113:
114: public SchedulerException(String msg) {
115: super (msg);
116: }
117:
118: public SchedulerException(String msg, int errorCode) {
119: super (msg);
120: setErrorCode(errorCode);
121: }
122:
123: public SchedulerException(Throwable cause) {
124: super (cause.toString());
125: setCause(cause);
126: }
127:
128: public SchedulerException(String msg, Throwable cause) {
129: super (msg);
130: setCause(cause);
131: }
132:
133: public SchedulerException(String msg, Throwable cause, int errorCode) {
134: super (msg);
135: setCause(cause);
136: setErrorCode(errorCode);
137: }
138:
139: private void setCause(Throwable cause) {
140: if (ExceptionHelper.supportsNestedThrowable()) {
141: ExceptionHelper.setCause(this , cause);
142: } else {
143: this .cause = cause;
144: }
145: }
146:
147: /*
148: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149: *
150: * Interface.
151: *
152: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153: */
154:
155: /**
156: * <p>
157: * Return the exception that is the underlying cause of this exception.
158: * </p>
159: *
160: * <p>
161: * This may be used to find more detail about the cause of the error.
162: * </p>
163: *
164: * @return the underlying exception, or <code>null</code> if there is not
165: * one.
166: */
167: public Throwable getUnderlyingException() {
168: return (ExceptionHelper.supportsNestedThrowable()) ? ExceptionHelper
169: .getCause(this )
170: : cause;
171: }
172:
173: /**
174: * <p>
175: * Get the error code associated with this exception.
176: * </p>
177: *
178: * <p>
179: * This may be used to find more detail about the cause of the error.
180: * </p>
181: *
182: * @return one of the ERR_XXX constants defined in this class.
183: */
184: public int getErrorCode() {
185: return errorCode;
186: }
187:
188: /**
189: * <p>
190: * Get the error code associated with this exception.
191: * </p>
192: *
193: * <p>
194: * This may be used to provide more detail about the cause of the error.
195: * </p>
196: *
197: * @param errorCode
198: * one of the ERR_XXX constants defined in this class.
199: */
200: public void setErrorCode(int errorCode) {
201: this .errorCode = errorCode;
202: }
203:
204: /**
205: * <p>
206: * Determine if the specified error code is in the <code>'ERR_PERSISTENCE'</code>
207: * category of errors.
208: * </p>
209: */
210: public boolean isPersistenceError() {
211: return (errorCode >= ERR_PERSISTENCE && errorCode <= ERR_PERSISTENCE + 99);
212: }
213:
214: /**
215: * <p>
216: * Determine if the specified error code is in the <code>'ERR_THREAD_POOL'</code>
217: * category of errors.
218: * </p>
219: */
220: public boolean isThreadPoolError() {
221: return (errorCode >= ERR_THREAD_POOL && errorCode <= ERR_THREAD_POOL + 99);
222: }
223:
224: /**
225: * <p>
226: * Determine if the specified error code is in the <code>'ERR_JOB_LISTENER'</code>
227: * category of errors.
228: * </p>
229: */
230: public boolean isJobListenerError() {
231: return (errorCode >= ERR_JOB_LISTENER && errorCode <= ERR_JOB_LISTENER + 99);
232: }
233:
234: /**
235: * <p>
236: * Determine if the specified error code is in the <code>'ERR_TRIGGER_LISTENER'</code>
237: * category of errors.
238: * </p>
239: */
240: public boolean isTriggerListenerError() {
241: return (errorCode >= ERR_TRIGGER_LISTENER && errorCode <= ERR_TRIGGER_LISTENER + 99);
242: }
243:
244: /**
245: * <p>
246: * Determine if the specified error code is in the <code>'ERR_CLIENT_ERROR'</code>
247: * category of errors.
248: * </p>
249: */
250: public boolean isClientError() {
251: return (errorCode >= ERR_CLIENT_ERROR && errorCode <= ERR_CLIENT_ERROR + 99);
252: }
253:
254: /**
255: * <p>
256: * Determine if the specified error code is in the <code>'ERR_CLIENT_ERROR'</code>
257: * category of errors.
258: * </p>
259: */
260: public boolean isConfigurationError() {
261: return (errorCode >= ERR_BAD_CONFIGURATION && errorCode <= ERR_BAD_CONFIGURATION + 49);
262: }
263:
264: public String toString() {
265: Throwable cause = getUnderlyingException();
266: if (cause == null || cause == this ) {
267: return super .toString();
268: } else {
269: return super .toString() + " [See nested exception: "
270: + cause + "]";
271: }
272: }
273:
274: /**
275: * <P>
276: * Print a stack trace to the standard error stream.
277: * </P>
278: *
279: * <P>
280: * This overridden version will print the nested stack trace if available,
281: * otherwise it prints only this exception's stack.
282: * </P>
283: */
284: public void printStackTrace() {
285: printStackTrace(System.err);
286: }
287:
288: /**
289: * <P>
290: * Print a stack trace to the specified stream.
291: * </P>
292: *
293: * <P>
294: * This overridden version will print the nested stack trace if available,
295: * otherwise it prints only this exception's stack.
296: * </P>
297: *
298: * @param out
299: * the stream to which the stack traces will be printed.
300: */
301: public void printStackTrace(PrintStream out) {
302: super .printStackTrace(out);
303:
304: if (cause != null) {
305: synchronized (out) {
306: out
307: .println("* Nested Exception (Underlying Cause) ---------------");
308: cause.printStackTrace(out);
309: }
310: }
311: }
312:
313: /**
314: * <P>
315: * Print a stack trace to the specified writer.
316: * </P>
317: *
318: * <P>
319: * This overridden version will print the nested stack trace if available,
320: * otherwise it prints this exception's stack.
321: * </P>
322: *
323: * @param out
324: * the writer to which the stack traces will be printed.
325: */
326: public void printStackTrace(PrintWriter out) {
327: super .printStackTrace(out);
328:
329: if (cause != null) {
330: synchronized (out) {
331: out
332: .println("* Nested Exception (Underlying Cause) ---------------");
333: cause.printStackTrace(out);
334: }
335: }
336: }
337:
338: }
|