81a5bfad36f3479957d42a16a2c67aae6301fab8
[utils] / test / enterprise / src / main / java / org / wamblee / test / transactions / SimpleTransactionManager.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.test.transactions;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.transaction.UserTransaction;
22
23 import org.wamblee.general.ThreadSpecificProxyFactory;
24 import org.wamblee.test.persistence.JpaBuilder;
25
26 /**
27  * 
28  * <p>
29  * Simple transaction manager provides a simple mechanism to manage transactions
30  * in test code through the {@link UserTransaction} object.
31  * To construct the transaction manager use: 
32  * </p>
33  * <pre>
34  *      SimpleTransactionManager manager = 
35  *          new SimpleTransactionManager(new DefaultUserTransactionFactory());
36  * </pre>
37  * 
38  * <p>
39  * Next, add resources to manage using {@link #addResource(TransactionResource)}:
40  * <pre>
41  *      manager.addResource(jpaTester.getJpaBuilder());
42  * </pre>
43  * <p>
44  * As you can see from the above, {@link JpaBuilder} is a resource that can be used
45  * so this mechanism can be used with JPA testing. 
46  * </p>
47  * 
48  * <p>
49  * The next step is to manage transactions using the standard <code>UserTransaction</code>
50  * APIs:
51  * </p>
52  * <pre>
53  *      UserTransaction transaction = manager.getTransaction();
54  *      transaction.begin();
55  *     
56  *      ... do work...
57  *  
58  *      transaction.commit(); 
59  * </pre>
60  * 
61  * @author Erik Brakkee
62  * 
63  */
64 public class SimpleTransactionManager {
65
66     private UserTransactionFactory factory;
67     private ThreadSpecificProxyFactory<UserTransaction> transaction; 
68     private UserTransactionCallback callback;
69     private List<TransactionResource> resources;
70
71     /**
72      * Constructs the transaction manager.
73      * 
74      * @param aFactory
75      *            Factory to create transactions with.
76      */
77     public SimpleTransactionManager(UserTransactionFactory aFactory) {
78         factory = aFactory;
79         transaction = new ThreadSpecificProxyFactory<UserTransaction>(UserTransaction.class);
80         callback = new UserTransactionCallback() {
81
82             @Override
83             public void transactionFinished() {
84                 transaction.set(null);
85             }
86         };
87         resources = new ArrayList<TransactionResource>();
88     }
89
90     /**
91      * Adds a resource to manage. Adding resources is no longer allowed after
92      * the first transaction has started.
93      * 
94      * @param aResource
95      *            Resource.
96      */
97     public void addResource(TransactionResource aResource) {
98         resources.add(aResource);
99     }
100
101     /**
102      * Gets the user transaction. This is a contextual reference, meaning that
103      * it will delegate to the appropriate thread-specific user transaction. 
104      * It is also safe to store in a JNDI tree and for caching by applications. 
105      * 
106      * @return User transaction.
107      */
108     public UserTransaction getTransaction() {
109         UserTransaction tx = transaction.get();
110         if (tx == null) {
111             tx = factory.create(callback, resources);
112             transaction.set(tx);
113         }
114         return transaction.getProxy();
115     }
116     
117     /**
118      * Gets the thread-specific transaction object. 
119      * @return Transaction object. 
120      */
121     UserTransaction getThreadSpecificTransaction() { 
122        getTransaction(); // create tx if needed
123        return transaction.get();
124     }
125 }