KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > LocalTransactionImpl


1 /*
2  * Created on December 10, 2003
3  *
4  * LocalTransactionImpl.java is used to test the J2EE Connector
5  * as implemented by JOnAS. This class implements
6  * javax.resource.spi.LocalTransaction and
7  * javax.resource.cci.LocalTransaction
8  */

9 package ersatz.resourceadapter;
10
11 import javax.resource.ResourceException;
12 import javax.resource.spi.ManagedConnection;
13 import javax.resource.spi.ConnectionEvent;
14
15 /**
16  * @author Bob Kruse
17  *
18  * Jtest Resource Adapter
19  *
20  * used to test the J2EE Connector as implemented by JOnAS.
21  *
22  */

23 public class LocalTransactionImpl
24         implements javax.resource.spi.LocalTransaction,
25                     javax.resource.cci.LocalTransaction
26 {
27     private ManagedConnection mc;
28     String cName = "LocalTransactionImpl";
29     private boolean sendEvent;
30     //
31
// This LocalTransactionImpl instance STATE
32
final private int RESET = 0;
33     final private int BEGUN = 1;
34     final private int ENDED = 2;
35     final private int PREPARED = 3;
36     final private int FORGOT = 4;
37     final private int COMMITTED = 5;
38     final private int ROLLEDBACK = 6;
39
40     int txState;
41
42     public LocalTransactionImpl(ManagedConnection MC, boolean se) {
43         Utility.log(cName+".constructor");
44         this.mc = MC;
45         txState = RESET;
46         sendEvent = se;
47     }
48     public void setSendEvent(boolean event) {
49         sendEvent=event;
50     }
51     public int getTxState() {
52         return txState;
53     }
54     public ManagedConnection getCurrentMc() {
55         return mc;
56     }
57     public void begin() throws ResourceException
58     {
59         Utility.log(cName+".begin (enter) txState="+txState);
60         int curState=txState;
61         ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
62         try {
63             if (txState==RESET) {
64                 if (sendEvent) {
65                     omc.sendEvent(ConnectionEvent.LOCAL_TRANSACTION_STARTED, null, omc.getCHandle());
66                     txState=BEGUN;
67                     Utility.log(cName
68                        +".begin (exit) (sendEvent(LOCAL_TRANSACTION_STARTED="
69                        +ConnectionEvent.LOCAL_TRANSACTION_STARTED+"))"
70                        +" From State="+curState+" to State="+txState);
71                 } else {
72                     txState=BEGUN;
73                 }
74                 omc.inLocalTrans=true;
75             }
76             else {
77                 IllegalStateException ex = new IllegalStateException("LocalTransaction Already active");
78                 Utility.log(cName+".begin (exit) error: State="+txState
79                                      +" should be="+RESET+". "+ex.getMessage());
80                 throw ex;
81             }
82
83         } catch (Exception e) {
84             Utility.log(cName
85                    +".begin (exit) error: unable to sendEvent"
86                    +" with 'LOCAL_TRANSACTION_STARTED' "+e.toString());
87         }
88     }
89     public void commit() throws ResourceException
90     {
91         Utility.log(cName+".commit (enter) txState="+txState);
92         int curState=txState;
93         ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
94         try {
95             if (txState==BEGUN) {
96                 if (sendEvent) {
97                     omc.sendEvent(ConnectionEvent.LOCAL_TRANSACTION_COMMITTED, null, omc.getCHandle());
98                     txState=COMMITTED;
99                     Utility.log(cName
100                        +".commit (exit) (sendEvent(LOCAL_TRANSACTION_COMMITTED="
101                        +ConnectionEvent.LOCAL_TRANSACTION_COMMITTED+"))"
102                        +" From State="+curState+" to State="+txState);
103                 } else {
104                     txState=COMMITTED;
105                 }
106             }
107             else {
108                 IllegalStateException ex = new IllegalStateException(
109                           "LocalTransaction Illegal State during commit");
110                 Utility.log(cName+".commit (exit) error: State="+txState
111                         +" should be="+BEGUN+". "+ex.getMessage());
112                 omc.inLocalTrans=false;
113                 throw ex;
114             }
115         } catch (Exception e) {
116             Utility.log(cName
117                    +".commit (exit) error: unable to sendEvent"
118                    +" with 'LOCAL_TRANSACTION_COMMITTED' "+e.toString());
119         }
120         omc.inLocalTrans=false;
121     }
122     public void rollback() throws ResourceException
123     {
124         int curState=txState;
125         Utility.log(cName+".rollback (enter) txState="+txState);
126         ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
127         try {
128             if (txState == BEGUN) {
129                 if (sendEvent) {
130                     omc.sendEvent(ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK, null, omc.getCHandle());
131                     txState=ROLLEDBACK;
132                     Utility.log(cName
133                        +".rollback (exit) (sendEvent(LOCAL_TRANSACTION_ROLLEDBACK="
134                        +ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK+"))"
135                        +" From State="+curState+" to State="+txState);
136                 } else {
137                     txState=ROLLEDBACK;
138                 }
139             }
140             else {
141                 IllegalStateException ex = new IllegalStateException(
142                           "LocalTransaction Illegal State during rollback");
143                 Utility.log(cName+".rollback (exit) error: State="+txState
144                         +" should be="+BEGUN+". "+ex.getMessage());
145                 omc.inLocalTrans=false;
146                 throw ex;
147             }
148         } catch (Exception e) {
149             Utility.log(cName
150                    +".rollback (exit) error: unable to sendEvent"
151                    +" with 'LOCAL_TRANSACTION_ROLLEDBACK' "+e.toString());
152         }
153         omc.inLocalTrans=false;
154     }
155 }
156
Popular Tags