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 java.lang.reflect.Constructor;
21  
22  import org.slf4j.Logger;
23  import org.springframework.beans.factory.InitializingBean;
24  
25  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
26  
27  /**
28   * This class transforms exceptions into other exceptions.
29   *
30   * @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/SimpleExceptionTransformerExceptionHandler.java $
31   *
32   * @author Andreas Bur (ABU)
33   */
34  public class SimpleExceptionTransformerExceptionHandler
35  		extends AbstractExceptionTransformerExceptionHandler
36  		implements InitializingBean {
37  
38  	/** The transformed exception class. */
39  	private Class m_transformedExceptionClass;
40  	
41  	/**
42  	 * Sets the class into which an exception has to be transformed.
43  	 *
44  	 * @param transformedClass
45  	 *      The transformation's target class.
46  	 */
47  	public void setTransformedExceptionClass(Class transformedClass) {
48  		m_transformedExceptionClass = transformedClass;
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	protected Exception transform(Throwable t, Logger logger) {
55  		Exception e = createExceptionWithMessageAndThrowable(t);
56  		if (e == null) {
57  			e = createExceptionWithMessage(t);
58  		}
59  		if (e == null) {
60  			e = createException();
61  		}
62  		if (e == null) {
63  			logger.warn("Unable to transform Exception from ["
64  					+ t.getClass().getName() + "] to ["
65  					+ m_transformedExceptionClass.getName() + "].");
66  			return null;
67  		}
68  		e.setStackTrace(t.getStackTrace());
69  		
70  		return e;
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	public void afterPropertiesSet() throws Exception {
77  		CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
78  				m_transformedExceptionClass, "transformedClass", this);
79  		if (!Exception.class.isAssignableFrom(m_transformedExceptionClass)) {
80  			CoreNotificationHelper.notifyMisconfiguration(
81  					"The property 'transformedClass' has to be an instance of"
82  					+ " java.lang.Exception.");
83  		}
84  		// check whether a new instance can be created
85  		m_transformedExceptionClass.newInstance();
86  	}
87  
88  	/**
89  	 * Tries to create a new exception with the given throwable's message
90  	 * and the throwable itself as cause.
91  	 *
92  	 * @param t
93  	 *      The throwable to transform.
94  	 *
95  	 * @return Returns the transformed exception or <code>null</code> if the
96  	 *      class can't be instantiated.
97  	 */
98  	private Exception createExceptionWithMessageAndThrowable(Throwable t) {
99  		Exception e = null;
100 		Class[] params = {String.class, Throwable.class};
101 		Object[] values = {t.getMessage(), t};
102 		// Checkstyle: EmptyBlock off
103 		try {
104 			Constructor c = m_transformedExceptionClass.getConstructor(params);
105 			e = (Exception) c.newInstance(values);
106 		} catch (Exception ex) { }
107 		return e;
108 		// Checkstyle: EmptyBlock on
109 	}
110 	
111 	/**
112 	 * Tries to create a new exception with the given throwable's message.
113 	 *
114 	 * @param t
115 	 *      The throwable to transform.
116 	 *
117 	 * @return Returns the transformed exception or <code>null</code> if the
118 	 *      class can't be instantiated.
119 	 */
120 	private Exception createExceptionWithMessage(Throwable t) {
121 		Exception e = null;
122 		Class[] params = {String.class};
123 		Object[] values = {t.getMessage()};
124 		// Checkstyle: EmptyBlock off
125 		try {
126 			Constructor c = m_transformedExceptionClass.getConstructor(params);
127 			e = (Exception) c.newInstance(values);
128 		} catch (Exception ex) { }
129 		// Checkstyle: EmptyBlock on
130 		return e;
131 	}
132 	
133 	/**
134 	 * @return Returns a new instance of the target exception or
135 	 *      <code>null</code> if it can't be created.
136 	 */
137 	private Exception createException() {
138 		Exception e = null;
139 		// Checkstyle: EmptyBlock off
140 		try {
141 			e = (Exception) m_transformedExceptionClass.newInstance();
142 		} catch (Exception ex) { }
143 		// Checkstyle: EmptyBlock on
144 		return e;
145 	}
146 }