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  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
26  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
27  import org.springframework.transaction.interceptor.TransactionAttribute;
28  
29  /**
30   * Meta data source for transactional metadata of type
31   * <b>Commons Attributes</b>.
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/core/src/main/java/ch/elca/el4j/core/transaction/CommonsAttributesTransactionMetaDataSource.java $
34   *
35   * @author Martin Zeltner (MZE)
36   */
37  public class CommonsAttributesTransactionMetaDataSource
38  	extends TransactionMetaDataSource {
39  	
40  	/**
41  	 * Searches for one transaction attribute and adds found found rollback
42  	 * rules for rule based transaction attributes.
43  	 *
44  	 * {@inheritDoc}
45  	 */
46  	@Override
47  	protected Collection filterMetaData(Collection metaData) {
48  		if (metaData == null || metaData.isEmpty()) {
49  			return null;
50  		}
51  		
52  		TransactionAttribute txAttribute = null;
53  
54  		// Check whether there is a transaction attribute.
55  		for (Iterator itMetaData = metaData.iterator();
56  			itMetaData.hasNext() && txAttribute == null;) {
57  			Object att = itMetaData.next();
58  			if (att instanceof TransactionAttribute) {
59  				txAttribute = (TransactionAttribute) att;
60  			}
61  		}
62  
63  		// Check if we have a RuleBasedTransactionAttribute.
64  		if (txAttribute != null
65  			&& txAttribute instanceof RuleBasedTransactionAttribute) {
66  			RuleBasedTransactionAttribute rbta
67  				= (RuleBasedTransactionAttribute) txAttribute;
68  			// We really want value: bit of a hack.
69  			List<RollbackRuleAttribute> rollbackRules
70  				= new ArrayList<RollbackRuleAttribute>();
71  			for (Iterator itMetaData = metaData.iterator();
72  				itMetaData.hasNext();) {
73  				Object attribute = itMetaData.next();
74  				if (attribute instanceof RollbackRuleAttribute) {
75  					rollbackRules.add((RollbackRuleAttribute) attribute);
76  				}
77  			}
78  			// Repeatedly setting this isn't elegant, but it works.
79  			rbta.setRollbackRules(rollbackRules);
80  			
81  			// Finalize the rollback rule behavior.
82  			finalizeRollbackBehavior(rbta);
83  		}
84  		
85  		return txAttribute == null ? Collections.EMPTY_SET
86  			: Collections.singleton(txAttribute);
87  	}
88  }