001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: */package org.apache.openejb.core;
017:
018: import javax.transaction.HeuristicMixedException;
019: import javax.transaction.HeuristicRollbackException;
020: import javax.transaction.NotSupportedException;
021: import javax.transaction.RollbackException;
022: import javax.transaction.SystemException;
023: import javax.transaction.TransactionManager;
024: import javax.transaction.Status;
025:
026: import org.apache.openejb.util.LogCategory;
027: import org.apache.openejb.util.Logger;
028:
029: /**
030: * @org.apache.xbean.XBean element="userTransaction"
031: */
032: public class CoreUserTransaction implements
033: javax.transaction.UserTransaction, java.io.Serializable {
034: private static final long serialVersionUID = 9203248912222645965L;
035: private static final Logger transactionLogger = Logger.getInstance(
036: LogCategory.TRANSACTION,
037: "org.apache.openejb.util.resources");
038:
039: private transient TransactionManager transactionManager;
040:
041: public CoreUserTransaction(TransactionManager transactionManager) {
042: this .transactionManager = transactionManager;
043: }
044:
045: private TransactionManager transactionManager() {
046: if (transactionManager == null) {
047: transactionManager = org.apache.openejb.OpenEJB
048: .getTransactionManager();
049: }
050: return transactionManager;
051: }
052:
053: public void begin() throws NotSupportedException, SystemException {
054: transactionManager().begin();
055: if (transactionLogger.isInfoEnabled()) {
056: transactionLogger.info("Started user transaction "
057: + transactionManager().getTransaction());
058: }
059: }
060:
061: public void commit() throws RollbackException,
062: HeuristicMixedException, HeuristicRollbackException,
063: SecurityException, IllegalStateException, SystemException {
064: if (transactionLogger.isInfoEnabled()) {
065: transactionLogger.info("Committing user transaction "
066: + transactionManager().getTransaction());
067: }
068: transactionManager().commit();
069: }
070:
071: public void rollback() throws IllegalStateException,
072: SecurityException, SystemException {
073: if (transactionLogger.isInfoEnabled()) {
074: transactionLogger.info("Rolling back user transaction "
075: + transactionManager().getTransaction());
076: }
077: transactionManager().rollback();
078: }
079:
080: public int getStatus() throws SystemException {
081: int status = transactionManager().getStatus();
082: if (transactionLogger.isInfoEnabled()) {
083: transactionLogger.info("User transaction "
084: + transactionManager().getTransaction()
085: + " has status " + getStatus(status));
086: }
087: return status;
088: }
089:
090: public void setRollbackOnly()
091: throws javax.transaction.SystemException {
092: if (transactionLogger.isInfoEnabled()) {
093: transactionLogger
094: .info("Marking user transaction for rollback: "
095: + transactionManager().getTransaction());
096: }
097: transactionManager().setRollbackOnly();
098: }
099:
100: public void setTransactionTimeout(int seconds)
101: throws SystemException {
102: transactionManager().setTransactionTimeout(seconds);
103: }
104:
105: private static String getStatus(int status) {
106: StringBuffer buffer;
107:
108: buffer = new StringBuffer(100);
109: switch (status) {
110: case Status.STATUS_ACTIVE:
111: buffer.append("STATUS_ACTIVE: ");
112: buffer
113: .append("A transaction is associated with the target object and it is in the active state.");
114: break;
115: case Status.STATUS_COMMITTED:
116: buffer.append("STATUS_COMMITTED: ");
117: buffer
118: .append("A transaction is associated with the target object and it has been committed.");
119: break;
120: case Status.STATUS_COMMITTING:
121: buffer.append("STATUS_COMMITTING: ");
122: buffer
123: .append("A transaction is associated with the target object and it is in the process of committing.");
124: break;
125: case Status.STATUS_MARKED_ROLLBACK:
126: buffer.append("STATUS_MARKED_ROLLBACK: ");
127: buffer
128: .append("A transaction is associated with the target object and it has been marked for rollback, perhaps as a result of a setRollbackOnly operation.");
129: break;
130: case Status.STATUS_NO_TRANSACTION:
131: buffer.append("STATUS_NO_TRANSACTION: ");
132: buffer
133: .append("No transaction is currently associated with the target object.");
134: break;
135: case Status.STATUS_PREPARED:
136: buffer.append("STATUS_PREPARED: ");
137: buffer
138: .append("A transaction is associated with the target object and it has been prepared, i.e.");
139: break;
140: case Status.STATUS_PREPARING:
141: buffer.append("STATUS_PREPARING: ");
142: buffer
143: .append("A transaction is associated with the target object and it is in the process of preparing.");
144: break;
145: case Status.STATUS_ROLLEDBACK:
146: buffer.append("STATUS_ROLLEDBACK: ");
147: buffer
148: .append("A transaction is associated with the target object and the outcome has been determined as rollback.");
149: break;
150: case Status.STATUS_ROLLING_BACK:
151: buffer.append("STATUS_ROLLING_BACK: ");
152: buffer
153: .append("A transaction is associated with the target object and it is in the process of rolling back.");
154: break;
155: default:
156: buffer.append("Unknown status ").append(status);
157: break;
158: }
159: return buffer.toString();
160: }
161: }
|