001: /*
002: * $Id: AbstractSingleResourceTransaction.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transaction;
012:
013: import org.mule.api.transaction.TransactionException;
014: import org.mule.config.i18n.CoreMessages;
015:
016: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
017:
018: /**
019: * This abstract class can be used as a base class for transactions that can enlist
020: * only one resource (such as a JMS session or JDBC connection).
021: */
022: public abstract class AbstractSingleResourceTransaction extends
023: AbstractTransaction {
024:
025: protected volatile Object key;
026: protected volatile Object resource;
027:
028: protected final AtomicBoolean started = new AtomicBoolean(false);
029: protected final AtomicBoolean committed = new AtomicBoolean(false);
030: protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
031: protected final AtomicBoolean rollbackOnly = new AtomicBoolean(
032: false);
033:
034: /*
035: * (non-Javadoc)
036: *
037: * @see org.mule.api.Transaction#begin()
038: */
039: public void begin() throws TransactionException {
040: super .begin();
041: started.compareAndSet(false, true);
042: }
043:
044: /*
045: * (non-Javadoc)
046: *
047: * @see org.mule.api.Transaction#commit()
048: */
049: public void commit() throws TransactionException {
050: super .commit();
051: committed.compareAndSet(false, true);
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.mule.api.Transaction#rollback()
058: */
059: public void rollback() throws TransactionException {
060: super .rollback();
061: rolledBack.compareAndSet(false, true);
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see org.mule.api.Transaction#getStatus()
068: */
069: public int getStatus() throws TransactionStatusException {
070: if (rolledBack.get()) {
071: return STATUS_ROLLEDBACK;
072: }
073: if (committed.get()) {
074: return STATUS_COMMITTED;
075: }
076: if (rollbackOnly.get()) {
077: return STATUS_MARKED_ROLLBACK;
078: }
079: if (started.get()) {
080: return STATUS_ACTIVE;
081: }
082: return STATUS_NO_TRANSACTION;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.mule.api.Transaction#getResource(java.lang.Object)
089: */
090: public Object getResource(Object key) {
091: return key != null && this .key == key ? this .resource : null;
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.mule.api.Transaction#hasResource(java.lang.Object)
098: */
099: public boolean hasResource(Object key) {
100: return key != null && this .key == key;
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.mule.api.Transaction#bindResource(java.lang.Object,
107: * java.lang.Object)
108: */
109: public void bindResource(Object key, Object resource)
110: throws TransactionException {
111: if (key == null) {
112: throw new IllegalTransactionStateException(CoreMessages
113: .transactionCannotBindToNullKey());
114: }
115: if (resource == null) {
116: throw new IllegalTransactionStateException(CoreMessages
117: .transactionCannotBindNullResource());
118: }
119: if (this .key != null) {
120: throw new IllegalTransactionStateException(CoreMessages
121: .transactionSingleResourceOnly());
122: }
123: this .key = key;
124: this .resource = resource;
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.mule.api.Transaction#setRollbackOnly()
131: */
132: public void setRollbackOnly() {
133: rollbackOnly.set(true);
134: }
135:
136: public Object getId() {
137: return key;
138: }
139: }
|