KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > distributedtx > UserTransactionImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.distributedtx;
24
25 import java.rmi.RemoteException JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import javax.transaction.*;
28
29 import com.sun.ejb.*;
30 import com.sun.enterprise.*;
31 import com.sun.enterprise.log.Log;
32 import com.sun.enterprise.util.i18n.StringManager;
33
34 import java.util.logging.*;
35 import com.sun.logging.*;
36
37 /**
38  * This class implements javax.transaction.UserTransaction .
39  * Its methods are called from TX_BEAN_MANAGED EJB code.
40  * Most of its methods just delegate to the TransactionManager
41  * after doing some EJB Container-related steps.
42  *
43  * Note: EJB1.1 Section 6.4.1 requires that the Container must be able to
44  * preserve an object reference of the UserTransaction interface across
45  * passivation, so we make this Serializable.
46  *
47  * @author Tony Ng
48  */

49
50 public class UserTransactionImpl implements UserTransaction, Serializable JavaDoc
51 {
52
53     static Logger _logger=LogDomains.getLogger(LogDomains.JTA_LOGGER);
54     // Sting Manager for Localization
55
private static StringManager sm = StringManager.getManager(UserTransactionImpl.class);
56     private static final boolean debug = false;
57     private transient J2EETransactionManager transactionManager;
58     private transient InvocationManager invocationManager;
59     private transient boolean initialized;
60
61     // for non-J2EE clients usage
62
private transient UserTransaction userTx;
63
64     // private int transactionTimeout;
65

66     // true if ejb access checks should be performed. Default is
67
// true. All instances of UserTransaction exposed to applications
68
// will have checking turned on.
69
private boolean checkEjbAccess;
70
71     /**
72      * Default constructor.
73      */

74     public UserTransactionImpl()
75     {
76         this(true);
77     }
78
79     /**
80      * Alternate version of constructor that allows control over whether
81      * ejb access checks are performed.
82      */

83     public UserTransactionImpl(boolean doEjbAccessChecks)
84     {
85         init();
86         checkEjbAccess = doEjbAccessChecks;
87     }
88
89
90     /**
91      * Could be called after passivation and reactivation
92      */

93     private void init()
94     {
95         initialized = true;
96         Switch theSwitch = Switch.getSwitch();
97         transactionManager = theSwitch.getTransactionManager();
98         invocationManager = theSwitch.getInvocationManager();
99         if (transactionManager == null) {
100             // non J2EE client, set up UserTransaction from JTS
101
userTx = new com.sun.jts.jta.UserTransactionImpl();
102         }
103     }
104
105     private void checkUserTransactionMethodAccess(ComponentInvocation inv)
106         throws IllegalStateException JavaDoc, SystemException
107     {
108         if ( (inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION)
109              && checkEjbAccess ) {
110             Container ejbContainer = (Container) inv.container;
111             if( !ejbContainer.userTransactionMethodsAllowed(inv) ) {
112                 throw new IllegalStateException JavaDoc(sm.getString("enterprise_distributedtx.operation_not_allowed"));
113             }
114         } else if (inv.getInvocationType() ==
115                    ComponentInvocation.APP_CLIENT_INVOCATION) {
116             throw new SystemException(sm.getString("enterprise_distributedtx.usertransaction_not_supported"));
117         }
118     }
119
120     public void begin() throws NotSupportedException, SystemException
121     {
122         if (!initialized) init();
123
124         if (userTx != null) {
125             userTx.begin();
126             return;
127         }
128
129         ComponentInvocation inv = invocationManager.getCurrentInvocation();
130         if (inv != null) {
131             checkUserTransactionMethodAccess(inv);
132         } else {
133             throw new InvocationException();
134         }
135
136         transactionManager.begin();
137             /**
138         if ( transactionTimeout > 0 )
139             transactionManager.begin(transactionTimeout);
140         else
141             transactionManager.begin();
142             **/

143
144         try {
145             if ( inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION )
146                 ((Container)inv.container).doAfterBegin(inv);
147
148             //transactionManager.preInvoke(null, cc); // enlist resources
149
inv.setTransaction(transactionManager.getTransaction());
150             transactionManager.enlistComponentResources();
151         } catch ( RemoteException JavaDoc ex ) {
152             _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_begin", ex);
153             SystemException sysEx = new SystemException(ex.getMessage());
154             sysEx.initCause(ex);
155             throw sysEx;
156         }
157     }
158
159     public void commit() throws RollbackException,
160         HeuristicMixedException, HeuristicRollbackException, SecurityException JavaDoc,
161         IllegalStateException JavaDoc, SystemException
162     {
163         if (!initialized) init();
164
165         if (userTx != null) {
166             userTx.commit();
167             return;
168         }
169
170         ComponentInvocation inv = invocationManager.getCurrentInvocation();
171         if (inv != null) {
172             checkUserTransactionMethodAccess(inv);
173         } else {
174             throw new InvocationException();
175         }
176
177
178         try {
179             transactionManager.delistComponentResources(false); // TMSUCCESS
180
transactionManager.commit();
181         } catch ( RemoteException JavaDoc ex ) {
182             _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_commit", ex);
183             throw new SystemException();
184         } finally {
185             inv.setTransaction(null);
186         }
187     }
188
189     public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc,
190         SystemException
191     {
192         if (!initialized) init();
193
194         if (userTx != null) {
195             userTx.rollback();
196             return;
197         }
198
199         ComponentInvocation inv = invocationManager.getCurrentInvocation();
200         if (inv != null) {
201                 checkUserTransactionMethodAccess(inv);
202         } else {
203             throw new InvocationException();
204         }
205
206
207         try {
208             transactionManager.delistComponentResources(false); // TMSUCCESS
209
transactionManager.rollback();
210         } catch ( RemoteException JavaDoc ex ) {
211             _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_utx_rollback", ex);
212             throw new SystemException();
213         } finally {
214             inv.setTransaction(null);
215         }
216     }
217
218     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException
219     {
220         if (!initialized) init();
221
222         if (userTx != null) {
223             userTx.setRollbackOnly();
224             return;
225         }
226
227         ComponentInvocation inv = invocationManager.getCurrentInvocation();
228         if (inv != null) {
229                 checkUserTransactionMethodAccess(inv);
230         } else {
231             throw new InvocationException();
232         }
233
234         transactionManager.setRollbackOnly();
235     }
236
237     public int getStatus() throws SystemException
238     {
239         if (!initialized) init();
240
241         if (userTx != null) {
242             return userTx.getStatus();
243         }
244
245         ComponentInvocation inv = invocationManager.getCurrentInvocation();
246         if (inv != null) {
247                 checkUserTransactionMethodAccess(inv);
248         } else {
249             throw new InvocationException();
250         }
251
252         return transactionManager.getStatus();
253     }
254
255     public void setTransactionTimeout(int seconds) throws SystemException
256     {
257         if (!initialized) init();
258
259         if (userTx != null) {
260             userTx.setTransactionTimeout(seconds);
261             return;
262         }
263
264         ComponentInvocation inv = invocationManager.getCurrentInvocation();
265         if (inv != null) {
266                 checkUserTransactionMethodAccess(inv);
267         } else {
268             throw new InvocationException();
269         }
270
271         
272         if (seconds < 0) seconds = 0;
273         // transactionTimeout = seconds;
274
transactionManager.setTransactionTimeout(seconds);
275     }
276 }
277
Popular Tags