1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.api.transaction.TransactionException;
14 import org.mule.config.i18n.CoreMessages;
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 public void begin() throws TransactionException
34 {
35 super.begin();
36 started.compareAndSet(false, true);
37 }
38
39 public void commit() throws TransactionException
40 {
41 super.commit();
42 committed.compareAndSet(false, true);
43 }
44
45 public void rollback() throws TransactionException
46 {
47 super.rollback();
48 rolledBack.compareAndSet(false, true);
49 }
50
51 public int getStatus() throws TransactionStatusException
52 {
53 if (rolledBack.get())
54 {
55 return STATUS_ROLLEDBACK;
56 }
57 if (committed.get())
58 {
59 return STATUS_COMMITTED;
60 }
61 if (rollbackOnly.get())
62 {
63 return STATUS_MARKED_ROLLBACK;
64 }
65 if (started.get())
66 {
67 return STATUS_ACTIVE;
68 }
69 return STATUS_NO_TRANSACTION;
70 }
71
72 public Object getResource(Object key)
73 {
74 return key != null && this.key == key ? this.resource : null;
75 }
76
77 public boolean hasResource(Object key)
78 {
79 return key != null && this.key == key;
80 }
81
82 public void bindResource(Object key, Object resource) throws TransactionException
83 {
84 if (key == null)
85 {
86 throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
87 }
88 if (resource == null)
89 {
90 throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
91 }
92 if (this.key != null)
93 {
94 throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
95 }
96
97 if (logger.isDebugEnabled())
98 {
99 logger.debug("Binding " + resource + " to " + key);
100 }
101
102 this.key = key;
103 this.resource = resource;
104 }
105
106 public void setRollbackOnly()
107 {
108 rollbackOnly.set(true);
109 }
110
111 public Object getId()
112 {
113 return key;
114 }
115 }