KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > InternalException


1 /*
2  * Copyright 2001-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 org.apache.axis;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.utils.Messages;
21 import org.apache.commons.logging.Log;
22
23 /**
24  * Encapsulates exceptions for "should never occur" situations. Extends
25  * RuntimeException so it need not explicitly be caught. Logs the exception
26  * as a fatal error, and if debug is enabled, includes the full stack trace.
27  *
28  * @author Sam Ruby (rubys@us.ibm.com)
29  * @author Glyn Normington (glyn_normington@uk.ibm.com)
30  */

31 public class InternalException extends RuntimeException JavaDoc {
32
33     /**
34      * The <code>Log</code> used by this class to log messages.
35      */

36     protected static Log log =
37             LogFactory.getLog(InternalException.class.getName());
38
39     /**
40      * Attribute which controls whether or not logging of such events should
41      * occur. Default is true and is recommended. Sometimes this may be
42      * turned off in unit tests when internal errors are intentionally
43      * provoked.
44      */

45     private static boolean shouldLog = true;
46
47     /**
48      * Enable or dissable logging.
49      *
50      * @param logging true if you wish logging to be enabled, false otherwise
51      */

52     public static void setLogging(boolean logging) {
53         shouldLog = logging;
54     }
55
56     /**
57      * Discover whether the logging flag is set.
58      *
59      * @return true if we are logging, false otherwise
60      */

61     public static boolean getLogging() {
62         return shouldLog;
63     }
64
65     /**
66      * Construct an Internal Exception from a String. The string is wrapped
67      * in an exception, enabling a stack traceback to be obtained.
68      * @param message String form of the error
69      */

70     public InternalException(String JavaDoc message) {
71         this(new Exception JavaDoc(message));
72     }
73
74     /**
75      * Construct an Internal Exception from an Exception.
76      *
77      * @param e original exception which was unexpected
78      */

79     public InternalException(Exception JavaDoc e) {
80         super(e.toString());
81
82         if (shouldLog) {
83             // if the exception is merely bubbling up the stack, only log the
84
// event if debug is turned on.
85
if (e instanceof InternalException) {
86                 log.debug("InternalException: ", e);
87             } else {
88                 log.fatal(Messages.getMessage("exception00"), e);
89             }
90         }
91     }
92 }
93
Popular Tags