1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17
18 package ch.elca.el4j.services.exceptionhandler.handler;
19
20 import org.aopalliance.intercept.MethodInvocation;
21 import org.slf4j.Logger;
22
23 import ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor;
24 import ch.elca.el4j.services.exceptionhandler.RetryException;
25
26 /**
27 * This class allows to specify a number of exception handlers that are
28 * called in sequence until the first succeeds, i.e. it doesn't throw an
29 * exception. In this case, it returns its result. If all exception handler
30 * fail, the last caught exception is thrown.
31 *
32 * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/exception_handling/src/main/java/ch/elca/el4j/services/exceptionhandler/handler/SequenceExceptionHandler.java $
33 *
34 * @author Andreas Bur (ABU)
35 */
36 public class SequenceExceptionHandler extends AbstractExceptionHandler {
37
38 /** The exception handlers. */
39 private ExceptionHandler[] m_exceptionHandlers;
40
41 /**
42 * Sets the exception handlers.
43 *
44 * @param exceptionHandlers
45 * The exception handlers to set.
46 */
47 public void setExceptionHandlers(
48 ExceptionHandler[] exceptionHandlers) {
49 m_exceptionHandlers = exceptionHandlers;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 protected Object handleException(Throwable t,
56 AbstractExceptionHandlerInterceptor exceptionInvoker,
57 MethodInvocation invocation, Logger logger) throws Throwable {
58
59 Throwable lastThrowable = t;
60
61 for (int i = 0; i < m_exceptionHandlers.length; i++) {
62 try {
63 return m_exceptionHandlers[i].handleException(
64 lastThrowable, exceptionInvoker, invocation);
65
66 } catch (RetryException re) {
67 throw re;
68 } catch (Throwable lt) {
69 lastThrowable = lt;
70
71 if (logger.isDebugEnabled()) {
72 StringBuffer buffer = new StringBuffer("Handler [");
73 buffer.append(m_exceptionHandlers[i].getClass().getName());
74 buffer.append("] failed.");
75 if (i < m_exceptionHandlers.length) {
76 buffer.append(" Trying next one.");
77 }
78 logger.debug(buffer.toString(), lt);
79 }
80 }
81 }
82
83 throw lastThrowable;
84 }
85 }