KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > context > UserTransactionImpl


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy 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,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.context;
19
20 import java.io.Serializable JavaDoc;
21 import javax.transaction.HeuristicMixedException JavaDoc;
22 import javax.transaction.HeuristicRollbackException JavaDoc;
23 import javax.transaction.NotSupportedException JavaDoc;
24 import javax.transaction.RollbackException JavaDoc;
25 import javax.transaction.SystemException JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import org.apache.geronimo.transaction.TrackedConnectionAssociator;
29
30 /**
31  * Implementation of UserTransaction for use in an EJB.
32  * This adds the ability to enable or disable the operations depending on
33  * the lifecycle of the EJB instance.
34  *
35  * @version $Rev: 155839 $ $Date: 2005-03-01 15:30:08 -0800 (Tue, 01 Mar 2005) $
36  */

37 public class UserTransactionImpl implements UserTransaction JavaDoc, Serializable JavaDoc {
38     private final ThreadLocal JavaDoc state = new StateThreadLocal();
39     private static class StateThreadLocal extends ThreadLocal JavaDoc implements Serializable JavaDoc {
40         protected Object JavaDoc initialValue() {
41             return OFFLINE;
42         }
43     };
44
45     public UserTransactionImpl() {
46         state.set(OFFLINE);
47     }
48
49     public void setUp(TransactionContextManager transactionContextManager, TrackedConnectionAssociator trackedConnectionAssociator) {
50         assert !isOnline() : "Only set the tx manager when UserTransaction is stop";
51         this.ONLINE.setUp(transactionContextManager, trackedConnectionAssociator);
52     }
53
54     public boolean isOnline() {
55         return state.get() == ONLINE;
56     }
57
58     public void setOnline(boolean online) {
59         //too bad there's no implies operation
60
// online implies transactionContextManager != null
61
assert !online || ONLINE.isActive() : "online requires a tx manager";
62         state.set(online ? ONLINE : OFFLINE);
63     }
64
65     private UserTransaction JavaDoc getUserTransaction() {
66         return (UserTransaction JavaDoc) state.get();
67     }
68
69     public void begin() throws NotSupportedException JavaDoc, SystemException JavaDoc {
70         getUserTransaction().begin();
71     }
72
73     public void commit() throws HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc, IllegalStateException JavaDoc, RollbackException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc {
74         getUserTransaction().commit();
75     }
76
77     public int getStatus() throws SystemException JavaDoc {
78         return getUserTransaction().getStatus();
79     }
80
81     public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc {
82         getUserTransaction().rollback();
83     }
84
85     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc {
86         getUserTransaction().setRollbackOnly();
87     }
88
89     public void setTransactionTimeout(int timeout) throws SystemException JavaDoc {
90         getUserTransaction().setTransactionTimeout(timeout);
91     }
92
93     private final OnlineUserTransaction ONLINE = new OnlineUserTransaction();
94
95     private static final UserTransaction JavaDoc OFFLINE = new OfflineUserTransaction();
96     private static final class OfflineUserTransaction implements UserTransaction JavaDoc, Serializable JavaDoc {
97         public void begin() {
98             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
99         }
100
101         public void commit() {
102             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
103         }
104
105         public int getStatus() {
106             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
107         }
108
109         public void rollback() {
110             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
111         }
112
113         public void setRollbackOnly() {
114             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
115         }
116
117         public void setTransactionTimeout(int seconds) {
118             throw new IllegalStateException JavaDoc("Cannot use UserTransaction methods in this state");
119         }
120     };
121 }
122
Popular Tags