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     */
017    package org.apache.geronimo.connector.outbound;
018    
019    import javax.transaction.Status;
020    import javax.transaction.SystemException;
021    import javax.transaction.Transaction;
022    import javax.transaction.TransactionManager;
023    
024    /**
025     * @version $Rev: 550546 $ $Date: 2007-06-25 18:52:11 +0200 (Mon, 25 Jun 2007) $
026     */
027    public final class TxUtil {
028        private TxUtil() {
029        }
030    
031        public static Transaction getTransactionIfActive(TransactionManager transactionManager) {
032            Transaction transaction = null;
033            int status = Status.STATUS_NO_TRANSACTION;
034            try {
035                transaction = transactionManager.getTransaction();
036                if (transaction != null) status = transaction.getStatus();
037            } catch (SystemException ignored) {
038            }
039    
040            if (transaction != null && status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
041                return transaction;
042            }
043            return null;
044        }
045    
046        public static boolean isTransactionActive(TransactionManager transactionManager) {
047            try {
048                int status = transactionManager.getStatus();
049                return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
050            } catch (SystemException ignored) {
051                return false;
052            }
053        }
054    
055        public static boolean isActive(Transaction transaction) {
056            try {
057                int status = transaction.getStatus();
058                return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
059            } catch (SystemException ignored) {
060                return false;
061            }
062        }
063    }
064