View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.transaction.xa;
17  
18  import javax.transaction.xa.Xid;
19  
20  /***
21   * @author Ludovic Orban
22   */
23  public class XidTransactionIDImpl implements XidTransactionID {
24  
25      /***
26       * The decision types a XID transaction ID can be in
27       */
28      private static enum Decision {
29          IN_DOUBT,
30          COMMIT,
31          ROLLBACK
32      }
33  
34      private final SerializableXid xid;
35      private volatile Decision decision = Decision.IN_DOUBT;
36  
37      /***
38       * Constructor
39       * @param xid a XID
40       */
41      public XidTransactionIDImpl(Xid xid) {
42          this.xid = new SerializableXid(xid);
43      }
44  
45      /***
46       * {@inheritDoc}
47       */
48      public boolean isDecisionCommit() {
49          return decision.equals(Decision.COMMIT);
50      }
51  
52      /***
53       * {@inheritDoc}
54       */
55      public void markForCommit() {
56          if (decision.equals(Decision.ROLLBACK)) {
57              throw new IllegalStateException(this + " already marked for rollback, cannot re-mark it for commit");
58          }
59          this.decision = Decision.COMMIT;
60      }
61  
62      /***
63       * {@inheritDoc}
64       */
65      public boolean isDecisionRollback() {
66          return decision.equals(Decision.ROLLBACK);
67      }
68  
69      /***
70       * {@inheritDoc}
71       */
72      public void markForRollback() {
73          if (decision.equals(Decision.COMMIT)) {
74              throw new IllegalStateException(this + " already marked for commit, cannot re-mark it for rollback");
75          }
76          this.decision = Decision.ROLLBACK;
77      }
78  
79      /***
80       * {@inheritDoc}
81       */
82      public Xid getXid() {
83          return xid;
84      }
85  
86      /***
87       * {@inheritDoc}
88       */
89      @Override
90      public final boolean equals(Object obj) {
91          if (obj instanceof XidTransactionIDImpl) {
92              XidTransactionIDImpl otherId = (XidTransactionIDImpl) obj;
93              return xid.equals(otherId.xid);
94          }
95          return false;
96      }
97  
98      /***
99       * {@inheritDoc}
100      */
101     @Override
102     public final int hashCode() {
103         return xid.hashCode();
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     @Override
110     public String toString() {
111         return "Unclustered " + xid + " (decision: " + decision + ")";
112     }
113 }