View Javadoc

1   /*
2    * Copyright 2002-2005 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  
17  package org.jencks.factory;
18  
19  import java.util.HashSet;
20  
21  import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
22  import org.apache.geronimo.transaction.DefaultInstanceContext;
23  import org.apache.geronimo.transaction.context.OnlineUserTransaction;
24  import org.apache.geronimo.transaction.context.TransactionContextManager;
25  import org.apache.geronimo.transaction.context.UserTransactionImpl;
26  import org.springframework.beans.factory.FactoryBean;
27  import org.springframework.beans.factory.InitializingBean;
28  
29  import javax.resource.ResourceException;
30  import javax.transaction.HeuristicMixedException;
31  import javax.transaction.HeuristicRollbackException;
32  import javax.transaction.NotSupportedException;
33  import javax.transaction.RollbackException;
34  import javax.transaction.SystemException;
35  import javax.transaction.UserTransaction;
36  
37  /***
38   * This FactoryBean creates and configures the Geronimo implementation
39   * of the UserTransaction interface.
40   * <p/>
41   * This factory is based on the Geronimo Transaction Context Manager
42   * and Connection Tracking Coordinator.
43   *
44   * @deprecated Use GeronimoTransactionManagerFactoryBean instead
45   * @author ttemplier
46   * @see UserTransactionImpl#setUp(TransactionContextManager, org.apache.geronimo.transaction.TrackedConnectionAssociator)
47   * @see UserTransactionImpl#setOnline(boolean)
48   * @see GeronimoTransactionManagerFactoryBean
49   */
50  public class UserTransactionFactoryBean implements FactoryBean, InitializingBean {
51  
52      private TransactionContextManager transactionContextManager;
53      private ConnectionTrackingCoordinator connectionTrackingCoordinator;
54  
55      private UserTransaction userTransaction;
56  
57      public Object getObject() throws Exception {
58          return userTransaction;
59      }
60  
61      public Class getObjectType() {
62          return UserTransaction.class;
63      }
64  
65      public boolean isSingleton() {
66          return true;
67      }
68  
69      /***
70       * Set the transaction context manager to configure the user transaction.
71       */
72      public void setTransactionContextManager(TransactionContextManager manager) {
73          transactionContextManager = manager;
74      }
75  
76      /***
77       * Set the connection tracking coordinator to configure the user transaction.
78       */
79      public void setConnectionTrackingCoordinator(ConnectionTrackingCoordinator coordinator) {
80          connectionTrackingCoordinator = coordinator;
81      }
82  
83      /***
84       * This method instanciates the Geronimo user transaction implementation
85       * and sets up it with the transaction context manager used and a connection
86       * tracking coordinator.
87       *
88       * It then sets the online property to true in order that the application
89       * can used it.
90       */
91      public void afterPropertiesSet() throws Exception {
92      	this.userTransaction = new GeronimoUserTransaction();
93      }
94      
95      /***
96       * This wrapper around the OnlineUserTransaction performs per-thread
97       * initialization of the geronimo transaction layer.
98       * 
99       * @author gnt
100      */
101     public class GeronimoUserTransaction implements UserTransaction {
102     	
103     	private OnlineUserTransaction userTransaction;
104     	
105     	public GeronimoUserTransaction() {
106     		this.userTransaction = new OnlineUserTransaction();
107     		this.userTransaction.setUp(transactionContextManager,
108                 connectionTrackingCoordinator);
109     	}
110 
111 		public void begin() throws NotSupportedException, SystemException {
112 			ensureContext();
113 			userTransaction.begin();
114 		}
115 
116 		public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
117 			ensureContext();
118 			userTransaction.commit();
119 		}
120 
121 		public int getStatus() throws SystemException {
122 			ensureContext();
123 			return userTransaction.getStatus();
124 		}
125 
126 		public void rollback() throws IllegalStateException, SecurityException, SystemException {
127 			ensureContext();
128 			userTransaction.rollback();
129 		}
130 
131 		public void setRollbackOnly() throws IllegalStateException, SystemException {
132 			ensureContext();
133 			userTransaction.setRollbackOnly();
134 		}
135 
136 		public void setTransactionTimeout(int arg0) throws SystemException {
137 			ensureContext();
138 			userTransaction.setTransactionTimeout(arg0);
139 		}
140 
141 		private void ensureContext() throws SystemException {
142 			if (transactionContextManager.getContext() == null) {
143 				try {
144 					transactionContextManager.newUnspecifiedTransactionContext();
145 					connectionTrackingCoordinator.enter(new DefaultInstanceContext(new HashSet(), new HashSet()));
146 				} catch (ResourceException e) {
147 					throw (SystemException) new SystemException().initCause(e);
148 				}
149 			}
150 		}
151     }
152 
153 
154 }