KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fictional > resourceadapter > LocalTransactionImpl


1 /*
2  * Created on Jul 10, 2003
3  *
4  * LocalTransactionImpl.java is used to test the J2EE Connector
5  * as implemented by JOnAS. This class implements the LocalTransaction Interface
6  *
7  */

8 package fictional.resourceadapter;
9
10 import javax.resource.ResourceException;
11 import javax.resource.spi.ManagedConnection;
12 import javax.resource.spi.ConnectionEvent;
13 import javax.resource.spi.LocalTransaction;
14
15 import org.objectweb.jonas.common.Log;
16 import org.objectweb.util.monolog.api.Logger;
17 import org.objectweb.util.monolog.api.BasicLevel;
18
19 /**
20  * @author Bob Kruse
21  *
22  * Jtest Resource Adapter
23  *
24  * used to test the J2EE Connector as implemented by JOnAS.
25  *
26  */

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