KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package com.sun.enterprise.distributedtx;
25
26 import javax.transaction.TransactionSynchronizationRegistry JavaDoc;
27 import com.sun.enterprise.Switch;
28 import com.sun.enterprise.util.i18n.StringManager;
29 import javax.transaction.Synchronization JavaDoc;
30 import javax.transaction.Status JavaDoc;
31 import javax.transaction.SystemException JavaDoc;
32
33 public class TransactionSynchronizationRegistryImpl
34              implements TransactionSynchronizationRegistry JavaDoc {
35
36     /**
37      * Return an opaque object to represent the transaction bound to the
38      * current thread at the time this method is called. The returned object
39      * overrides <code>hashCode</code> and <code>equals</code> methods to allow
40      * its use as the key in a <code>java.util.HashMap</code> for use by the
41      * caller. If there is no transaction currently active, null is returned.
42      *
43      * <P>The returned object will return the same <code>hashCode</code> and
44      * compare equal to all other objects returned by calling this method
45      * from any component executing in the same transaction context in the
46      * same application server.
47      *
48      * <P>The <code>toString</code> method returns a <code>String</code>
49      * that might be usable by a human reader to usefully understand the
50      * transaction context. The result of the <code>toString</code> method
51      * is otherwise not defined. Specifically, there is no forward or backward
52      * compatibility guarantee for the result returned by the
53      * <code>toString</code> method.
54      *
55      * <P>The object is not necessarily serializable, and is not useful
56      * outside the virtual machine from which it was obtained.
57      *
58      * @return An object representing the current transaction,
59      * or null if no transaction is active.
60      */

61     public Object JavaDoc getTransactionKey() {
62         try {
63             return Switch.getSwitch().getTransactionManager().getTransaction();
64         } catch (SystemException JavaDoc ex) {
65             return null;
66         }
67     }
68     
69     /**
70      * Add an object to the map of resources being managed for
71      * the current transaction. The supplied key must be of a caller-
72      * defined class so as not to conflict with other users. The class
73      * of the key must guarantee that the <code>hashCode</code> and
74      * <code>equals</code> methods are suitable for keys in a map.
75      * The key and value are not examined or used by the implementation.
76      *
77      * @param key The key for looking up the associated value object.
78      *
79      * @param value The value object to keep track of.
80      *
81      * @exception IllegalStateException Thrown if the current thread
82      * is not associated with a transaction.
83      */

84     public void putResource(Object JavaDoc key, Object JavaDoc value) {
85         try {
86             J2EETransaction tran = (J2EETransaction)Switch.getSwitch().
87                                 getTransactionManager().getTransaction();
88             if (tran == null)
89                 throw new IllegalStateException JavaDoc(
90                       sm.getString("enterprise_distributedtx.no_transaction"));
91             tran.putUserResource(key, value);
92         } catch (SystemException JavaDoc ex) {
93             throw new IllegalStateException JavaDoc(
94                       sm.getString("enterprise_distributedtx.no_transaction"));
95         }
96     }
97     
98     /**
99      * Get an object from the map of resources being managed for
100      * the current transaction. The key must have been supplied earlier
101      * by a call to <code>putResouce</code> in the same transaction. If the key
102      * cannot be found in the current resource map, null is returned.
103      *
104      * @param key The key for looking up the associated value object.
105      *
106      * @return The value object, or null if not found.
107      *
108      * @exception IllegalStateException Thrown if the current thread
109      * is not associated with a transaction.
110      */

111     public Object JavaDoc getResource(Object JavaDoc key){
112         try {
113             J2EETransaction tran = (J2EETransaction)Switch.getSwitch().
114                                 getTransactionManager().getTransaction();
115             if (tran == null)
116                 throw new IllegalStateException JavaDoc(
117                       sm.getString("enterprise_distributedtx.no_transaction"));
118             return tran.getUserResource(key);
119         } catch (SystemException JavaDoc ex) {
120             throw new IllegalStateException JavaDoc(
121                       sm.getString("enterprise_distributedtx.no_transaction"));
122         }
123     }
124
125     /**
126      * Register a <code>Synchronization</code> instance with special ordering
127      * semantics. The <code>beforeCompletion</code> method on the registered
128      * <code>Synchronization</code> will be called after all user and
129      * system component <code>beforeCompletion</code> callbacks, but before
130      * the 2-phase commit process starts. This allows user and system components to
131      * flush state changes to the caching manager, during their
132      * <code>SessionSynchronization</code> callbacks, and allows managers
133      * to flush state changes to Connectors, during the callbacks
134      * registered with this method. Similarly, the <code>afterCompletion</code>
135      * callback will be called after 2-phase commit completes but before
136      * any user and system <code>afterCompletion</code> callbacks.
137      *
138      * <P>The <code>beforeCompletion</code> callback will be invoked in the
139      * transaction context of the current transaction bound to the thread
140      * of the caller of this method, which is the same transaction context
141      * active at the time this method is called. Allowable methods include
142      * access to resources, for example, Connectors. No access is allowed to
143      * user components, for example, timer services or bean methods,
144      * as these might change the state of POJOs, or plain old Java objects,
145      * being managed by the caching manager.
146      *
147      * <P>The <code>afterCompletion</code> callback will be invoked in an
148      * undefined transaction context. No access is permitted to resources or
149      * user components as defined above. Resources can be closed, but
150      * no transactional work can be performed with them.
151      *
152      * <P>Other than the transaction context, no component J2EE context is
153      * active during either of the callbacks.
154      *
155      * @param sync The synchronization callback object.
156      *
157      * @exception IllegalStateException Thrown if the current thread
158      * is not associated with a transaction.
159      */

160     public void registerInterposedSynchronization(Synchronization JavaDoc sync) {
161         try {
162             J2EETransaction tran = (J2EETransaction)Switch.getSwitch().
163                                 getTransactionManager().getTransaction();
164             if (tran == null)
165                 throw new IllegalStateException JavaDoc(
166                       sm.getString("enterprise_distributedtx.no_transaction"));
167             tran.registerInterposedSynchronization(sync);
168         } catch (SystemException JavaDoc ex) {
169             throw new IllegalStateException JavaDoc(
170                       sm.getString("enterprise_distributedtx.no_transaction"));
171         }
172     }
173
174     /**
175      * Returns the status of the transaction bound to the current thread.
176      * This is the result of executing <code>getStatus</code> method on the
177      * <code>TransactionManager</code>, in the current transaction context.
178      *
179      * @return The status of the current transaction.
180      */

181     public int getTransactionStatus() {
182         try {
183             return Switch.getSwitch().getTransactionManager().getStatus();
184         } catch (SystemException JavaDoc ex) {
185            return Status.STATUS_NO_TRANSACTION;
186         }
187     }
188
189     /**
190      * Set the <code>rollbackOnly</code> status of the transaction bound to the
191      * current thread.
192      *
193      * @exception IllegalStateException Thrown if the current thread
194      * is not associated with a transaction.
195      */

196     public void setRollbackOnly() {
197         try {
198             Switch.getSwitch().getTransactionManager().setRollbackOnly();
199         } catch (SystemException JavaDoc ex) {
200             throw new IllegalStateException JavaDoc(
201                       sm.getString("enterprise_distributedtx.no_transaction"));
202         }
203     }
204
205     /**
206      * Get the <code>rollbackOnly</code> status of the transaction bound to the
207      * current thread.
208      *
209      * @return true, if the current transaction is marked for rollback only.
210      *
211      * @exception IllegalStateException Thrown if the current thread
212      * is not associated with a transaction.
213      */

214     public boolean getRollbackOnly() {
215     {
216         int status = getTransactionStatus();
217         if ( status == Status.STATUS_NO_TRANSACTION ) {
218             throw new IllegalStateException JavaDoc(
219                       sm.getString("enterprise_distributedtx.no_transaction"));
220         }
221         if ( status == Status.STATUS_MARKED_ROLLBACK
222             || status == Status.STATUS_ROLLING_BACK )
223                 return true;
224             else
225                 return false;
226         }
227     }
228
229     public static TransactionSynchronizationRegistry JavaDoc getInstance() {
230         return _instance;
231     }
232     
233     private static TransactionSynchronizationRegistryImpl _instance =
234                        new TransactionSynchronizationRegistryImpl();
235     private static StringManager sm =
236                    StringManager.getManager(TransactionSynchronizationRegistryImpl.class);
237 }
238
Popular Tags