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  import org.springframework.aop.target.HotSwappableTargetSource;
23  
24  import ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor;
25  import ch.elca.el4j.services.exceptionhandler.InappropriateHandlerException;
26  import ch.elca.el4j.services.exceptionhandler.RetryException;
27  
28  /**
29   * This class simplifies the implementation of exception handlers that use
30   * another implementation with the same interface to fulfil the callers
31   * invocation.
32   *
33   * @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/AbstractSwappableTargetExceptionHandler.java $
34   *
35   * @author Andreas Bur (ABU)
36   * @see ch.elca.el4j.services.exceptionhandler.handler.AbstractReconfigureExceptionHandler
37   */
38  public abstract class AbstractSwappableTargetExceptionHandler extends
39  		AbstractRetryExceptionHandler {
40  
41  	/** The TargetSource to change the target on. */
42  	private HotSwappableTargetSource m_swapper;
43  	
44  	/**
45  	 * Sets the TargetSource that is used in the proxy, where the target has to
46  	 * be swapped.
47  	 *
48  	 * @param swapper
49  	 *      The TargetSource to set.
50  	 */
51  	public void setSwapper(HotSwappableTargetSource swapper) {
52  		m_swapper = swapper;
53  	}
54  
55  	/**
56  	 * {@inheritDoc}
57  	 */
58  	protected Object retry(Throwable t,
59  		AbstractExceptionHandlerInterceptor exceptionInvoker,
60  		MethodInvocation invocation, Logger logger)
61  		throws InappropriateHandlerException, RetryException {
62  		
63  		try {
64  			Object newTarget = getNewTarget(
65  					m_swapper.getTarget(), t, invocation, logger);
66  			m_swapper.swap(newTarget);
67  			
68  		} catch (InappropriateHandlerException ihe) {
69  			throw ihe;
70  		} catch (Throwable nt) {
71  			throw new RetryException(getRetries());
72  		}
73  		
74  		throw new RetryException(getRetries(), m_swapper);
75  	}
76  
77  	/**
78  	 * Determines a new target to be used by the proxy.
79  	 *
80  	 * @param current
81  	 *      The current target which the proxy is working on.
82  	 *
83  	 * @param t
84  	 *      The exception that caused this strategy. Subclasses may distinguish
85  	 *      between different exception types.
86  	 *
87  	 * @param invocation
88  	 *      The original invocation that failed.
89  	 *
90  	 * @param logger
91  	 *      The logger.
92  	 *
93  	 * @return Returns an alternative bean that implements the same interface
94  	 *      as the original one.
95  	 *
96  	 * @throws RetryException
97  	 *      Signals that the complete invocation has to be rerun.
98  	 *
99  	 * @throws Throwable
100 	 *      Whenever something goes wrong.
101 	 */
102 	protected abstract Object getNewTarget(Object current, Throwable t,
103 		MethodInvocation invocation, Logger logger)
104 		throws RetryException, Throwable;
105 }