Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/service/impl/AbstractTransaction.java


1   /** 
2    * 
3    * Copyright 2004 Hiram Chirino
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.activemq.service.impl;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  
23  import javax.transaction.xa.XAException;
24  
25  import org.activemq.service.Transaction;
26  import org.activemq.service.TransactionTask;
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.1.1.1 $
33   */
34  public abstract class AbstractTransaction implements 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  
42      private ArrayList prePrepareTasks = new ArrayList();
43      private ArrayList postCommitTasks = new ArrayList();
44      private ArrayList postRollbackTasks = new ArrayList();
45      private byte state = START_STATE;
46  
47      public byte getState() {
48          return state;
49      }
50  
51      public void setState(byte state) {
52          this.state = state;
53      }
54  
55      public void addPostCommitTask(TransactionTask r) {
56          postCommitTasks.add(r);
57          if (state == START_STATE) {
58              state = IN_USE_STATE;
59          }
60      }
61  
62      public void addPostRollbackTask(TransactionTask r) {
63          postRollbackTasks.add(r);
64          if (state == START_STATE) {
65              state = IN_USE_STATE;
66          }
67      }
68  
69      public void addPrePrepareTask(TransactionTask r) {
70          prePrepareTasks.add(r);
71          if (state == START_STATE) {
72              state = IN_USE_STATE;
73          }
74      }
75  
76      public void prePrepare() throws Throwable {
77  
78          // Is it ok to call prepare now given the state of the
79          // transaction?
80          switch (state) {
81              case START_STATE:
82              case IN_USE_STATE:
83                  break;
84              default:
85                  XAException xae = new XAException("Prepare cannot be called now.");
86                  xae.errorCode = XAException.XAER_PROTO;
87                  throw xae;
88          }
89  
90          // Run the prePrepareTasks
91          for (Iterator iter = prePrepareTasks.iterator(); iter.hasNext();) {
92              TransactionTask r = (TransactionTask) iter.next();
93              r.execute();
94          }
95      }
96  
97      protected void postCommit() throws Throwable {
98          // Run the postCommitTasks
99          for (Iterator iter = postCommitTasks.iterator(); iter.hasNext();) {
100             TransactionTask r = (TransactionTask) iter.next();
101             r.execute();
102         }
103     }
104 
105     public void postRollback() throws Throwable {
106         // Run the postRollbackTasks
107         for (Iterator iter = postRollbackTasks.iterator(); iter.hasNext();) {
108             TransactionTask r = (TransactionTask) iter.next();
109             r.execute();
110         }
111     }
112 
113     public String toString() {
114         return super.toString() + "[prePrepares=" + prePrepareTasks + "; postCommits=" + postCommitTasks
115                 + "; postRollbacks=" + postRollbackTasks + "]";
116     }
117 
118 }