View Javadoc

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.SafetyFacadeInterceptor;
25  
26  /**
27   * Retry exception handlers force the {@link
28   * ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor}
29   * to rerun the complete invocation. They signal a retry with the
30   * {@link ch.elca.el4j.services.exceptionhandler.RetryException} which contains
31   * the expected number of retries. The exception handler interceptor however is
32   * free to choose another number of retries (e.g. if different retry exception
33   * handler are used).
34   *
35   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +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/AbstractRetryExceptionHandler.java $
36   *
37   * @author Andreas Bur (ABU)
38   */
39  public abstract class AbstractRetryExceptionHandler
40  	extends AbstractExceptionHandler {
41  
42  	/** The default number of retries. */
43  	private static final int DEFAULT_RETRIES = 5;
44  	
45  	/** Number of retires. Default is <code>5</code>. */
46  	private int m_retries = DEFAULT_RETRIES;
47  	
48  	/**
49  	 * Sets the number of retires. Default is <code>5</code>.
50  	 *
51  	 * @param retries
52  	 *      The number of retries to set.
53  	 */
54  	public void setRetries(int retries) {
55  		m_retries = retries;
56  	}
57  	
58  	/**
59  	 * @return Returns the number of expected retires.
60  	 */
61  	protected int getRetries() {
62  		return m_retries;
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	public Object handleException(Throwable t,
69  			AbstractExceptionHandlerInterceptor exceptionInvoker,
70  			MethodInvocation invocation, Logger logger) throws Throwable {
71  		
72  		if (SafetyFacadeInterceptor.getRetries() == -1
73  				|| SafetyFacadeInterceptor.getRetries() > 0) {
74  			return retry(t, exceptionInvoker, invocation, logger);
75  			
76  		} else {
77  			throw t;
78  		}
79  	}
80  
81  	/**
82  	 * Creates a new retry exception. Subclasses may also do some other tasks
83  	 * (e.g. waiting). This method is called only if the number of retires is
84  	 * not already exceeded.
85  	 *
86  	 * @param t
87  	 *      The exception thrown in the method invocation.
88  	 *
89  	 * @param exceptionInvoker
90  	 *      The exception invoker that called this exception handler.
91  	 *
92  	 * @param invocation
93  	 *      The original method invocation.
94  	 * @param logger
95  	 *      The logger to be used by subclasses.
96  	 *
97  	 * @return Returns an object that is treated as the original invocation's
98  	 *      return value.
99  	 *
100 	 * @throws Throwable
101 	 *      Whenever something goes wrong.
102 	 */
103 	protected abstract Object retry(Throwable t,
104 			AbstractExceptionHandlerInterceptor exceptionInvoker,
105 			MethodInvocation invocation, Logger logger) throws Throwable;
106 }