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) 2006 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  package ch.elca.el4j.core.transaction;
18  
19  import java.lang.reflect.Method;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
25  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
26  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
27  import org.springframework.transaction.interceptor.TransactionAttribute;
28  import org.springframework.transaction.interceptor.TransactionAttributeSource;
29  import org.springframework.util.CollectionUtils;
30  
31  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
32  import ch.elca.el4j.util.metadata.DefaultGenericMetaDataSource;
33  
34  /**
35   * Base class for transactional metadata sources.
36   *
37   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/transaction/TransactionMetaDataSource.java $
38   *
39   * @author Martin Zeltner (MZE)
40   */
41  public class TransactionMetaDataSource
42  	extends DefaultGenericMetaDataSource
43  	implements TransactionAttributeSource, InitializingBean {
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	public TransactionAttribute getTransactionAttribute(
49  		Method method, Class targetClass) {
50  		Collection c = getMetaData(method, targetClass);
51  		if (CollectionUtils.isEmpty(c)) {
52  			return null;
53  		} else {
54  			return (TransactionAttribute) c.iterator().next();
55  		}
56  	}
57  
58  	/**
59  	 * Checks the given rule based rollback attribute and corrects the
60  	 * rollback rule if necessary.
61  	 *
62  	 * @param rbta Is the rule based rollback attribute to finalize.
63  	 */
64  	@SuppressWarnings("unchecked")
65  	protected void finalizeRollbackBehavior(
66  		RuleBasedTransactionAttribute rbta) {
67  		/**
68  		 * If no no-rollback-rule for error/runtime-exception is defined
69  		 * add the rollback-rule for error/runtime-exception.
70  		 */
71  		RollbackRuleAttribute rollbackRuleRuntimeException
72  			= new RollbackRuleAttribute(RuntimeException.class);
73  		String rollbackRuleRuntimeExceptionName
74  			= rollbackRuleRuntimeException.getExceptionName();
75  		boolean useRollbackRuleRuntimeException = true;
76  		
77  		RollbackRuleAttribute rollbackRuleError
78  			= new RollbackRuleAttribute(Error.class);
79  		String rollbackRuleErrorName
80  			= rollbackRuleError.getExceptionName();
81  		boolean useRollbackRuleError = true;
82  		
83  		List<RollbackRuleAttribute> allRules = rbta.getRollbackRules();
84  		for (RollbackRuleAttribute rule : allRules) {
85  			if (rule instanceof NoRollbackRuleAttribute) {
86  				String name = rule.getExceptionName();
87  				if (rollbackRuleRuntimeExceptionName.equals(name)) {
88  					useRollbackRuleRuntimeException = false;
89  				} else if (rollbackRuleErrorName.equals(name)) {
90  					useRollbackRuleError = false;
91  				}
92  			}
93  		}
94  		
95  		if (useRollbackRuleRuntimeException) {
96  			allRules.add(
97  				new RollbackRuleAttribute(RuntimeException.class));
98  		}
99  		if (useRollbackRuleError) {
100 			allRules.add(
101 				new RollbackRuleAttribute(Error.class));
102 		}
103 	}
104 
105 	/**
106 	 * {@inheritDoc}
107 	 */
108 	public void afterPropertiesSet() throws Exception {
109 		CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
110 			getMetaDataDelegator(), "metaDataDelegator", this);
111 	}
112 }