1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.umo.TransactionException;
15
16 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
17
18
19
20
21
22 public abstract class AbstractSingleResourceTransaction extends AbstractTransaction
23 {
24
25 protected volatile Object key;
26 protected volatile Object resource;
27
28 protected final AtomicBoolean started = new AtomicBoolean(false);
29 protected final AtomicBoolean committed = new AtomicBoolean(false);
30 protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
31 protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false);
32
33
34
35
36
37
38 public void begin() throws TransactionException
39 {
40 super.begin();
41 started.compareAndSet(false, true);
42 }
43
44
45
46
47
48
49 public void commit() throws TransactionException
50 {
51 super.commit();
52 committed.compareAndSet(false, true);
53 }
54
55
56
57
58
59
60 public void rollback() throws TransactionException
61 {
62 super.rollback();
63 rolledBack.compareAndSet(false, true);
64 }
65
66
67
68
69
70
71 public int getStatus() throws TransactionStatusException
72 {
73 if (rolledBack.get())
74 {
75 return STATUS_ROLLEDBACK;
76 }
77 if (committed.get())
78 {
79 return STATUS_COMMITTED;
80 }
81 if (rollbackOnly.get())
82 {
83 return STATUS_MARKED_ROLLBACK;
84 }
85 if (started.get())
86 {
87 return STATUS_ACTIVE;
88 }
89 return STATUS_NO_TRANSACTION;
90 }
91
92
93
94
95
96
97 public Object getResource(Object key)
98 {
99 return key != null && this.key == key ? this.resource : null;
100 }
101
102
103
104
105
106
107 public boolean hasResource(Object key)
108 {
109 return key != null && this.key == key;
110 }
111
112
113
114
115
116
117
118 public void bindResource(Object key, Object resource) throws TransactionException
119 {
120 if (key == null)
121 {
122 throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
123 }
124 if (resource == null)
125 {
126 throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
127 }
128 if (this.key != null)
129 {
130 throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
131 }
132
133 if (logger.isDebugEnabled())
134 {
135 logger.debug("Binding " + resource + " to " + key);
136 }
137
138 this.key = key;
139 this.resource = resource;
140 }
141
142
143
144
145
146
147 public void setRollbackOnly()
148 {
149 rollbackOnly.set(true);
150 }
151
152 public Object getId()
153 {
154 return key;
155 }
156 }