01: /**
02: * Copyright (C) 2001-2004 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package org.objectweb.speedo.api;
18:
19: import org.objectweb.jorm.api.PException;
20: import org.objectweb.perseus.persistence.api.PersistenceException;
21: import org.objectweb.perseus.pool.api.PoolException;
22: import org.objectweb.medor.api.MedorException;
23: import org.objectweb.medor.expression.api.ExpressionException;
24:
25: /**
26: * This helper permits to find the final nested exception of a given exception.
27: *
28: * @author S.Chassande-Barrioz
29: */
30: public class ExceptionHelper {
31: private static Class jdoExceptionClass;
32:
33: static {
34: try {
35: jdoExceptionClass = Class.forName("javax.jdo.JDOException");
36: } catch (ClassNotFoundException e) {
37: jdoExceptionClass = null;
38: }
39: }
40:
41: private final static Class[] NOPARAM = new Class[0];
42: private final static Object[] NOVALUE = new Class[0];
43:
44: public static final Exception getNested(Exception e) {
45: if (e == null)
46: return null;
47: Exception c = e;
48: Exception i = null;
49: boolean notLast = true;
50: while (notLast) {
51: if (c instanceof SpeedoException) {
52: i = ((SpeedoException) c).getNestedException();
53: } else if (c instanceof PException) {
54: i = ((PException) c).getNestedException();
55: } else if (c instanceof MedorException) {
56: i = ((MedorException) c).getNestedException();
57: } else if (c instanceof ExpressionException) {
58: i = ((ExpressionException) c).getNestedException();
59: } else if (c instanceof PersistenceException) {
60: i = ((PersistenceException) c).getNestedException();
61: } else if (c instanceof PoolException) {
62: i = ((PoolException) c).getNestedException();
63: } else if ((jdoExceptionClass != null)
64: && (jdoExceptionClass.isInstance(c))) {
65: try {
66: Throwable[] ts = (Throwable[]) c.getClass()
67: .getMethod("getNestedExceptions", NOPARAM)
68: .invoke(c, NOVALUE);
69: if (ts != null && ts.length > 0
70: && (ts[0] instanceof Exception)) {
71: i = (Exception) ts[0];
72: } else {
73: notLast = false;
74: }
75: } catch (Exception e2) {
76: throw new SpeedoRuntimeException(
77: "Internal problem with Speedo code!!");
78: }
79: } else {
80: Throwable ts = c.getCause();
81: if (ts instanceof Exception) {
82: i = (Exception) ts;
83: } else {
84: notLast = false;
85: }
86: }
87: if (notLast && i != null) {
88: c = i;
89: } else {
90: notLast = false;
91: }
92: }
93: return c;
94: }
95:
96: }
|