KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > ee > jta > UserTransactionHelper


1
2 /*
3  * Copyright 2004-2005 OpenSymphony
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz.ee.jta;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.transaction.HeuristicMixedException JavaDoc;
26 import javax.transaction.HeuristicRollbackException JavaDoc;
27 import javax.transaction.NotSupportedException JavaDoc;
28 import javax.transaction.RollbackException JavaDoc;
29 import javax.transaction.SystemException JavaDoc;
30 import javax.transaction.UserTransaction JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.quartz.SchedulerException;
35
36 /**
37  * <p>
38  * A helper for obtaining a handle to a UserTransaction...
39  * </p>
40  * <p>
41  * To ensure proper cleanup of the InitalContext used to create/lookup
42  * the UserTransaction, be sure to always call returnUserTransaction() when
43  * you are done with the UserTransaction.
44  * </p>
45  *
46  * @author James House
47  */

48 public class UserTransactionHelper {
49     
50     /*
51      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52      *
53      * Constants.
54      *
55      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56      */

57
58     public static final String JavaDoc DEFAULT_USER_TX_LOCATION = "java:comp/UserTransaction";
59
60     /*
61      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62      *
63      * Data members.
64      *
65      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66      */

67
68     private static String JavaDoc userTxURL = DEFAULT_USER_TX_LOCATION;
69
70     /*
71      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72      *
73      * Constructors.
74      *
75      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76      */

77
78     /**
79      * Do not allow the creation of an all static utility class.
80      */

81     private UserTransactionHelper() {
82     }
83
84     /*
85      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86      *
87      * Interface.
88      *
89      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90      */

91
92     public static String JavaDoc getUserTxLocation() {
93         return userTxURL;
94     }
95
96     /**
97      * Set the JNDI URL at which the Application Server's UserTransaction can
98      * be found. If not set, the default value is "java:comp/UserTransaction" -
99      * which works for nearly all application servers.
100      */

101     public static void setUserTxLocation(String JavaDoc userTxURL) {
102         if (userTxURL != null) {
103             UserTransactionHelper.userTxURL = userTxURL;
104         }
105     }
106
107     /**
108      * Create/Lookup a UserTransaction in the InitialContext via the
109      * name set in setUserTxLocation().
110      */

111     public static UserTransaction JavaDoc lookupUserTransaction() throws SchedulerException {
112         return new UserTransactionWithContext();
113     }
114     
115     /**
116      * Return a UserTransaction that was retrieved via getUserTransaction().
117      * This will make sure that the InitalContext used to lookup/create the
118      * UserTransaction is properly cleaned up.
119      */

120     public static void returnUserTransaction(UserTransaction JavaDoc userTransaction) {
121         if ((userTransaction != null) &&
122             (userTransaction instanceof UserTransactionWithContext)) {
123             UserTransactionWithContext userTransactionWithContext =
124                 (UserTransactionWithContext)userTransaction;
125             
126             userTransactionWithContext.closeContext();
127         }
128     }
129
130     /**
131      * This class wraps a UserTransaction with the InitialContext that was used
132      * to look it up, so that when the UserTransaction is returned to the
133      * UserTransactionHelper the InitialContext can be closed.
134      */

135     private static class UserTransactionWithContext implements UserTransaction JavaDoc {
136         InitialContext JavaDoc context;
137         UserTransaction JavaDoc userTransaction;
138         
139         public UserTransactionWithContext() throws SchedulerException {
140             try {
141                 context = new InitialContext JavaDoc();
142             } catch (Throwable JavaDoc t) {
143                 throw new SchedulerException(
144                     "UserTransactionHelper failed to create InitialContext to lookup/create UserTransaction.", t);
145             }
146             
147             try {
148                 userTransaction = (UserTransaction JavaDoc)context.lookup(userTxURL);
149             } catch (Throwable JavaDoc t) {
150                 closeContext();
151                 throw new SchedulerException(
152                     "UserTransactionHelper could not lookup/create UserTransaction.",
153                     t);
154             }
155             
156             if (userTransaction == null) {
157                 closeContext();
158                 throw new SchedulerException(
159                     "UserTransactionHelper could not lookup/create UserTransaction from the InitialContext.");
160             }
161         }
162
163         /**
164          * Close the InitialContext that was used to lookup/create the
165          * underlying UserTransaction.
166          */

167         public void closeContext() {
168             try {
169                 if (context != null) {
170                     context.close();
171                 }
172             } catch (Throwable JavaDoc t) {
173                 getLog().warn("Failed to close InitialContext used to get a UserTransaction.", t);
174             }
175             context = null;
176         }
177
178         /**
179          * When we are being garbage collected, make sure we were properly
180          * returned to the UserTransactionHelper.
181          */

182         protected void finalize() throws Throwable JavaDoc {
183             try {
184                 if (context != null) {
185                     getLog().warn("UserTransaction was never returned to the UserTransactionHelper.");
186                     closeContext();
187                 }
188             } finally {
189                 super.finalize();
190             }
191         }
192
193         private static Log getLog() {
194             return LogFactory.getLog(UserTransactionWithContext.class);
195         }
196         
197         // Wrapper methods that just delegate to the underlying UserTransaction
198

199         public void begin() throws NotSupportedException JavaDoc, SystemException JavaDoc {
200             userTransaction.begin();
201         }
202
203         public void commit() throws RollbackException JavaDoc, HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc, SecurityException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc {
204             userTransaction.commit();
205         }
206
207         public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc {
208             userTransaction.rollback();
209         }
210
211         public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc {
212             userTransaction.setRollbackOnly();
213         }
214
215         public int getStatus() throws SystemException JavaDoc {
216             return userTransaction.getStatus();
217         }
218
219         public void setTransactionTimeout(int seconds) throws SystemException JavaDoc {
220             userTransaction.setTransactionTimeout(seconds);
221         }
222     }
223 }
Popular Tags