001: package org.apache.ojb.odmg;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Hashtable;
019:
020: import org.apache.ojb.broker.util.configuration.Configuration;
021: import org.odmg.TransactionNotInProgressException;
022:
023: /**
024: * In a non-appserver environment, without a transaction manager, we can
025: * safely associate the current ODMG transaction with the calling thread.
026: *
027: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
028: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
029: * @version $Id: LocalTxManager.java,v 1.5.2.5 2005/12/21 22:29:21 tomdz Exp $
030: */
031: public class LocalTxManager implements OJBTxManager {
032: /**
033: * Internal table which provides mapping between threads and transactions.
034: * This is required because a Thread can join a Transaction already in
035: * progress.
036: * If the thread joins a transaction, then "getTransaction()" should return
037: * the apropriate one. The only way we can ensure that is by keeping hold
038: * of the txTable.
039: */
040: private static TransactionTable tx_table = new TransactionTable();
041:
042: public LocalTxManager() {
043:
044: }
045:
046: /**
047: * Returns the current transaction for the calling thread.
048: *
049: * @throws org.odmg.TransactionNotInProgressException
050: * {@link org.odmg.TransactionNotInProgressException} if no transaction was found.
051: */
052: public TransactionImpl getCurrentTransaction() {
053: TransactionImpl tx = tx_table.get(Thread.currentThread());
054: if (tx == null) {
055: throw new TransactionNotInProgressException(
056: "Calling method needed transaction, but no transaction found for current thread :-(");
057: }
058: return tx;
059: }
060:
061: /**
062: * Returns the current transaction for the calling thread or <code>null</code>
063: * if no transaction was found.
064: */
065: public TransactionImpl getTransaction() {
066: return tx_table.get(Thread.currentThread());
067: }
068:
069: /**
070: * add the current transaction to the map key'd by the calling thread.
071: */
072: public void registerTx(TransactionImpl tx) {
073: tx_table.put(Thread.currentThread(), tx);
074: }
075:
076: /**
077: * remove the current transaction from the map key'd by the calling thread.
078: */
079: public void deregisterTx(Object token) {
080: tx_table.remove(Thread.currentThread());
081: }
082:
083: /**
084: * included to keep interface contract consistent.
085: */
086: public void abortExternalTx(TransactionImpl odmgTrans) {
087: /**
088: * no op
089: */
090: }
091:
092: public void configure(Configuration config) {
093:
094: }
095:
096: //=======================================================
097: // inner class
098: //=======================================================
099: /**
100: * TransactionTable provides a mapping between the calling
101: * thread and the Transaction it is currently using.
102: * One thread can be joined with one transaction at
103: * a certain point in time. But a thread can join with
104: * different transactions subsequently.
105: * This mapping from threads to Transactions is based on ODMG.
106: *
107: * @author Thomas Mahler & David Dixon-Peugh
108: */
109: static final class TransactionTable {
110: /**
111: * the internal Hashtable mapping Transactions to threads
112: */
113: private Hashtable m_table = new Hashtable();
114:
115: /**
116: * Creates new TransactionTable
117: */
118: public TransactionTable() {
119: }
120:
121: /**
122: * Retreive a Transaction associated with a thread.
123: *
124: * @param key_thread The thread to lookup.
125: * @return The transaction associated with the thread.
126: */
127: public TransactionImpl get(Thread key_thread) {
128: return (TransactionImpl) m_table.get(key_thread);
129: }
130:
131: /**
132: * Store the Thread/Transaction pair in the TransactionTable
133: *
134: * @param key_thread Thread that the transaction will be associated to
135: * @param value_tx Transaction to be associated with the thread
136: */
137: public void put(Thread key_thread, TransactionImpl value_tx) {
138: m_table.put(key_thread, value_tx);
139: }
140:
141: /**
142: * Remove the entry for the thread
143: *
144: * @param key_thread Thread to be removed.
145: */
146: public void remove(Thread key_thread) {
147: m_table.remove(key_thread);
148: }
149: }
150: }
|