KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transaction > Transaction


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

18 package org.apache.activemq.transaction;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.transaction.xa.XAException JavaDoc;
25
26 import org.apache.activemq.command.TransactionId;
27
28 /**
29  * Keeps track of all the actions the need to be done when
30  * a transaction does a commit or rollback.
31  *
32  * @version $Revision: 1.5 $
33  */

34 public abstract class Transaction {
35
36     static final public byte START_STATE = 0; // can go to: 1,2,3
37
static final public byte IN_USE_STATE = 1; // can go to: 2,3
38
static final public byte PREPARED_STATE = 2; // can go to: 3
39
static final public byte FINISHED_STATE = 3;
40
41     private ArrayList JavaDoc synchronizations = new ArrayList JavaDoc();
42     private byte state = START_STATE;
43
44     public byte getState() {
45         return state;
46     }
47
48     public void setState(byte state) {
49         this.state = state;
50     }
51
52     public void addSynchronization(Synchronization r) {
53         synchronizations.add(r);
54         if (state == START_STATE) {
55             state = IN_USE_STATE;
56         }
57     }
58
59     public void prePrepare() throws Exception JavaDoc {
60
61         // Is it ok to call prepare now given the state of the
62
// transaction?
63
switch (state) {
64             case START_STATE:
65             case IN_USE_STATE:
66                 break;
67             default:
68                 XAException JavaDoc xae = new XAException JavaDoc("Prepare cannot be called now.");
69                 xae.errorCode = XAException.XAER_PROTO;
70                 throw xae;
71         }
72
73 // // Run the prePrepareTasks
74
// for (Iterator iter = prePrepareTasks.iterator(); iter.hasNext();) {
75
// Callback r = (Callback) iter.next();
76
// r.execute();
77
// }
78
}
79
80     protected void fireAfterCommit() throws Exception JavaDoc {
81         for (Iterator JavaDoc iter = synchronizations.iterator(); iter.hasNext();) {
82             Synchronization s = (Synchronization) iter.next();
83             s.afterCommit();
84         }
85     }
86
87     public void fireAfterRollback() throws Exception JavaDoc {
88         for (Iterator JavaDoc iter = synchronizations.iterator(); iter.hasNext();) {
89             Synchronization s = (Synchronization) iter.next();
90             s.afterRollback();
91         }
92     }
93
94     public String JavaDoc toString() {
95         return super.toString() + "[synchronizations=" + synchronizations +"]";
96     }
97
98     abstract public void commit(boolean onePhase) throws XAException JavaDoc, IOException JavaDoc;
99     abstract public void rollback() throws XAException JavaDoc, IOException JavaDoc;
100     abstract public int prepare() throws XAException JavaDoc, IOException JavaDoc;
101     
102     abstract public TransactionId getTransactionId();
103
104     public boolean isPrepared() {
105         return getState()==PREPARED_STATE;
106     }
107 }
108
Popular Tags