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.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  
23  import org.springframework.transaction.annotation.Transactional;
24  import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
25  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
26  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
27  
28  /**
29   * Meta data source for transactional metadata of type <b>Java 5 Annotation</b>.
30   *
31   * @svnLink $Revision: 3902 $;$Date: 2009-08-17 14:34:48 +0200 (Mo, 17. 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/AnnotationTransactionMetaDataSource.java $
32   *
33   * @author Martin Zeltner (MZE)
34   */
35  public class AnnotationTransactionMetaDataSource
36  	extends TransactionMetaDataSource {
37  	
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	@Override
42  	protected Collection filterMetaData(Collection metaData) {
43  		if (metaData == null || metaData.isEmpty()) {
44  			return null;
45  		}
46  
47  		RuleBasedTransactionAttribute rbta = null;
48  		
49  		// See if there is a transactional metadata.
50  		for (Object att : metaData) {
51  			if (att instanceof Transactional) {
52  				Transactional ruleBasedTx = (Transactional) att;
53  				rbta = new RuleBasedTransactionAttribute();
54  				addMetaDataTransactional(rbta, ruleBasedTx);
55  				break;
56  			}
57  		}
58  		
59  		// If there was a transactional metadata look out for a
60  		// rollback constraint.
61  		Collection result;
62  		if (rbta != null) {
63  			finalizeRollbackBehavior(rbta);
64  			result = Collections.singleton(rbta);
65  		} else {
66  			result = Collections.EMPTY_SET;
67  		}
68  		return result;
69  	}
70  
71  	/**
72  	 * Adds the given transactional metadata to the given rule based rollback
73  	 * attribute.
74  	 *
75  	 * @param rbta Is the rule based rollback attribute to complete
76  	 * @param ruleBasedTx Is the transactional metadata to add.
77  	 */
78  	@SuppressWarnings("unchecked")
79  	protected void addMetaDataTransactional(
80  		RuleBasedTransactionAttribute rbta, Transactional ruleBasedTx) {
81  		rbta.setPropagationBehavior(ruleBasedTx.propagation().value());
82  		rbta.setIsolationLevel(ruleBasedTx.isolation().value());
83  		rbta.setTimeout(ruleBasedTx.timeout());
84  		rbta.setReadOnly(ruleBasedTx.readOnly());
85  	
86  		ArrayList<RollbackRuleAttribute> rollBackRules
87  			= new ArrayList<RollbackRuleAttribute>();
88  	
89  		Class[] rbf = ruleBasedTx.rollbackFor();
90  		for (int i = 0; i < rbf.length; ++i) {
91  			RollbackRuleAttribute rule
92  				= new RollbackRuleAttribute(rbf[i]);
93  			rollBackRules.add(rule);
94  		}
95  	
96  		String[] rbfc = ruleBasedTx.rollbackForClassName();
97  		for (int i = 0; i < rbfc.length; ++i) {
98  			RollbackRuleAttribute rule
99  				= new RollbackRuleAttribute(rbfc[i]);
100 			rollBackRules.add(rule);
101 		}
102 	
103 		Class[] nrbf = ruleBasedTx.noRollbackFor();
104 		for (int i = 0; i < nrbf.length; ++i) {
105 			NoRollbackRuleAttribute rule
106 				= new NoRollbackRuleAttribute(nrbf[i]);
107 			rollBackRules.add(rule);
108 		}
109 	
110 		String[] nrbfc = ruleBasedTx.noRollbackForClassName();
111 		for (int i = 0; i < nrbfc.length; ++i) {
112 			NoRollbackRuleAttribute rule
113 				= new NoRollbackRuleAttribute(nrbfc[i]);
114 			rollBackRules.add(rule);
115 		}
116 	
117 		rbta.getRollbackRules().addAll(rollBackRules);
118 	}
119 }