001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config;
031:
032: import com.caucho.util.*;
033:
034: import java.io.PrintWriter;
035: import java.lang.reflect.*;
036:
037: /**
038: * Thrown by the various Builders
039: */
040: public class ConfigException extends ConfigRuntimeException implements
041: CompileException, DisplayableException {
042: /**
043: * Create a null exception
044: */
045: public ConfigException() {
046: }
047:
048: /**
049: * Creates an exception with a message
050: */
051: public ConfigException(String msg) {
052: super (msg);
053: }
054:
055: /**
056: * Creates an exception with a message and throwable
057: */
058: public ConfigException(String msg, Throwable e) {
059: super (msg, e);
060: }
061:
062: /**
063: * Creates an exception with a throwable
064: */
065: /*
066: public ConfigException(Throwable e)
067: {
068: super(getMessage(e), e);
069: }
070: */
071:
072: private static String getMessage(Throwable e) {
073: if (e instanceof DisplayableException
074: || e instanceof CompileException)
075: return e.getMessage();
076: else
077: return e.toString();
078: }
079:
080: public static RuntimeException create(String location, Throwable e) {
081: if (e instanceof InstantiationException && e.getCause() != null)
082: e = e.getCause();
083:
084: if (e instanceof InvocationTargetException
085: && e.getCause() != null)
086: e = e.getCause();
087:
088: if (e instanceof LineConfigException)
089: throw (LineConfigException) e;
090: else if (e instanceof DisplayableException) {
091: return new ConfigException(location + e.getMessage(), e);
092: } else
093: return new ConfigException(location + e, e);
094: }
095:
096: public static RuntimeException createLine(String line, Throwable e) {
097: while (e.getCause() != null
098: && (e instanceof InstantiationException
099: || e instanceof InvocationTargetException || e
100: .getClass()
101: .equals(ConfigRuntimeException.class))) {
102: e = e.getCause();
103: }
104:
105: if (e instanceof LineConfigException)
106: throw (LineConfigException) e;
107: else if (e instanceof DisplayableException) {
108: return new LineConfigException(line + e.getMessage(), e);
109: } else
110: return new LineConfigException(line + e, e);
111: }
112:
113: public static RuntimeException create(Field field, Throwable e) {
114: return create(loc(field), e);
115: }
116:
117: public static RuntimeException create(Method method, Throwable e) {
118: return create(loc(method), e);
119: }
120:
121: public static RuntimeException create(Method method, String msg,
122: Throwable e) {
123: return new ConfigException(loc(method) + msg, e);
124: }
125:
126: public static RuntimeException create(Method method, String msg) {
127: return new ConfigException(loc(method) + msg);
128: }
129:
130: public static RuntimeException create(Throwable e) {
131: while (e.getCause() != null
132: && (e instanceof InstantiationException
133: || e instanceof InvocationTargetException || e
134: .getClass()
135: .equals(ConfigRuntimeException.class))) {
136: e = e.getCause();
137: }
138:
139: if (e instanceof RuntimeException)
140: return (RuntimeException) e;
141: else if (e instanceof LineCompileException)
142: return new LineConfigException(e.getMessage(), e);
143: else if (e instanceof DisplayableException
144: || e instanceof CompileException)
145: return new ConfigException(e.getMessage(), e);
146: else
147: return new ConfigRuntimeException(e);
148: }
149:
150: public void print(PrintWriter out) {
151: out.println(Html.escapeHtml(getMessage()));
152: }
153:
154: public static String loc(Field field) {
155: return field.getDeclaringClass().getName() + "."
156: + field.getName() + ": ";
157: }
158:
159: public static String loc(Method method) {
160: return method.getDeclaringClass().getName() + "."
161: + method.getName() + "(): ";
162: }
163: }
|