001: /**********************************************************************
002: Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.transaction;
018:
019: import java.util.Hashtable;
020:
021: import org.jpox.management.ManagementServer;
022: import org.jpox.management.runtime.TransactionRuntime;
023: import org.jpox.util.ClassUtils;
024:
025: /**
026: * TransactionManager is facade for creating (Open/XA) transactions
027: * A cache of transactions is held per userObject
028: */
029: public class TransactionManager {
030: Hashtable transactions = new Hashtable();
031:
032: /** runtime metrics for the transaction system **/
033: private TransactionRuntime txRuntime = null;
034:
035: /**
036: * Register TransactionManager MBeans in ManagementServer
037: * @param domainName Domain name
038: * @param instanceName Instance name
039: * @param mgmtServer JMX server
040: */
041: public void registerMbean(String domainName, String instanceName,
042: ManagementServer mgmtServer) {
043: if (mgmtServer != null) {
044: // Register MBean with server
045: txRuntime = new TransactionRuntime();
046: String mbeanName = domainName
047: + ":InstanceName="
048: + instanceName
049: + ",Type="
050: + ClassUtils.getClassNameForClass(txRuntime
051: .getClass()) + ",Name=TransactionRuntime";
052: mgmtServer.registerMBean(txRuntime, mbeanName);
053: }
054: }
055:
056: public TransactionRuntime getTransactionRuntime() {
057: return txRuntime;
058: }
059:
060: /**
061: * Begin a new transaction
062: * @param om the user object (an user reference) associated to this transaction
063: * @throws JPOXTransactionException if there is already a Transaction associated to this
064: * user object
065: */
066: public void begin(Object om) {
067: Transaction tx = (Transaction) transactions.get(om);
068: if (tx != null) {
069: throw new JPOXTransactionException(
070: "Invalid state. Transaction has already started");
071: }
072: tx = new Transaction();
073: transactions.put(om, tx);
074: }
075:
076: public void commit(Object om) {
077: Transaction tx = (Transaction) transactions.get(om);
078: if (tx == null) {
079: throw new JPOXTransactionException(
080: "Invalid state. Transaction does not exist");
081: }
082: try {
083: tx.commit();
084: } finally {
085: transactions.remove(om);
086: }
087: }
088:
089: public Transaction getTransaction(Object om) {
090: if (om == null) {
091: return null;
092: }
093: return (Transaction) transactions.get(om);
094: }
095:
096: public void resume(Object om, Transaction tx) {
097: throw new UnsupportedOperationException();
098: }
099:
100: public void rollback(Object om) {
101: Transaction tx = (Transaction) transactions.get(om);
102: if (tx == null) {
103: throw new JPOXTransactionException(
104: "Invalid state. Transaction does not exist");
105: }
106: try {
107: tx.rollback();
108: } finally {
109: transactions.remove(om);
110: }
111: }
112:
113: public void setRollbackOnly(Object om) {
114: Transaction tx = (Transaction) transactions.get(om);
115: if (tx == null) {
116: throw new JPOXTransactionException(
117: "Invalid state. Transaction does not exist");
118: }
119: tx.setRollbackOnly();
120: }
121:
122: public void setTransactionTimeout(Object om, int millis) {
123: throw new UnsupportedOperationException();
124: }
125:
126: public Transaction suspend(Object om) {
127: throw new UnsupportedOperationException();
128: }
129: }
|