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;
19  
20  import org.aopalliance.intercept.MethodInvocation;
21  import org.springframework.beans.factory.InitializingBean;
22  
23  import ch.elca.el4j.services.exceptionhandler.handler.ExceptionHandler;
24  
25  /**
26   * This class configures an exception handler. It maps exception types to
27   * exception handlers.
28   *
29   * @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/ClassExceptionConfiguration.java $
30   *
31   * @author Andreas Bur (ABU)
32   */
33  public class ClassExceptionConfiguration
34  	implements ExceptionConfiguration, InitializingBean {
35  
36  	/** The exception types which the handler is responsible for. */
37  	private Class[] m_exceptionTypes;
38  	
39  	/** The exception handler. */
40  	private ExceptionHandler m_exceptionHandler;
41  	
42  	/**
43  	 * @return Returns the exception types which the handler is responsible for.
44  	 */
45  	public Class[] getExceptionTypes() {
46  		return m_exceptionTypes;
47  	}
48  
49  	/**
50  	 * Sets the exception types which the hander is responsible for.
51  	 *
52  	 * @param exceptionTypes
53  	 *      The exception types to set.
54  	 */
55  	public void setExceptionTypes(Class[] exceptionTypes) {
56  		m_exceptionTypes = exceptionTypes;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	public ExceptionHandler getExceptionHandler() {
63  		return m_exceptionHandler;
64  	}
65  	
66  	/**
67  	 * Sets the exception handler.
68  	 *
69  	 * @param exceptionHandler
70  	 *      The exception handler to set.
71  	 */
72  	public void setExceptionHandler(ExceptionHandler exceptionHandler) {
73  		m_exceptionHandler = exceptionHandler;
74  	}
75  
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	public boolean handlesExceptions(Throwable t, MethodInvocation invocation) {
80  		for (int i = 0; i < m_exceptionTypes.length; i++) {
81  			if (m_exceptionTypes[i].isInstance(t)) {
82  				return true;
83  			}
84  		}
85  		return false;
86  	}
87  
88  	/**
89  	 * {@inheritDoc}
90  	 */
91  	public void afterPropertiesSet() throws Exception {
92  		if (m_exceptionTypes == null || m_exceptionTypes.length == 0) {
93  			throw new IllegalAccessException(
94  					"The property 'exceptionTypes' is required.");
95  		}
96  		if (m_exceptionHandler == null) {
97  			throw new IllegalAccessException(
98  					"The property 'exceptionHandler' is required.");
99  		}
100 	}
101 }