001: /*
002: * $Id: TransactionFactory.java,v 1.1 2003/08/17 04:56:27 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.transaction;
025:
026: import java.sql.Connection;
027: import java.sql.SQLException;
028:
029: import javax.transaction.TransactionManager;
030: import javax.transaction.UserTransaction;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.entity.GenericEntityException;
034: import org.ofbiz.entity.config.EntityConfigUtil;
035:
036: /**
037: * TransactionFactory - central source for JTA objects
038: *
039: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
040: * @version $Revision: 1.1 $
041: * @since 2.0
042: */
043: public class TransactionFactory {
044:
045: public static final String module = TransactionFactory.class
046: .getName();
047: public static TransactionFactoryInterface transactionFactory = null;
048:
049: public static TransactionFactoryInterface getTransactionFactory() {
050: if (transactionFactory == null) { // don't want to block here
051: synchronized (TransactionFactory.class) {
052: // must check if null again as one of the blocked threads can still enter
053: if (transactionFactory == null) {
054: try {
055: String className = EntityConfigUtil
056: .getTxFactoryClass();
057:
058: if (className == null) {
059: throw new IllegalStateException(
060: "Could not find transaction factory class name definition");
061: }
062: Class tfClass = null;
063:
064: if (className != null && className.length() > 0) {
065: try {
066: ClassLoader loader = Thread
067: .currentThread()
068: .getContextClassLoader();
069: tfClass = loader.loadClass(className);
070: } catch (ClassNotFoundException e) {
071: Debug.logWarning(e, module);
072: throw new IllegalStateException(
073: "Error loading TransactionFactory class \""
074: + className + "\": "
075: + e.getMessage());
076: }
077: }
078:
079: try {
080: transactionFactory = (TransactionFactoryInterface) tfClass
081: .newInstance();
082: } catch (IllegalAccessException e) {
083: Debug.logWarning(e, module);
084: throw new IllegalStateException(
085: "Error loading TransactionFactory class \""
086: + className + "\": "
087: + e.getMessage());
088: } catch (InstantiationException e) {
089: Debug.logWarning(e, module);
090: throw new IllegalStateException(
091: "Error loading TransactionFactory class \""
092: + className + "\": "
093: + e.getMessage());
094: }
095: } catch (SecurityException e) {
096: Debug.logError(e, module);
097: throw new IllegalStateException(
098: "Error loading TransactionFactory class: "
099: + e.getMessage());
100: }
101: }
102: }
103: }
104: return transactionFactory;
105: }
106:
107: public static TransactionManager getTransactionManager() {
108: return getTransactionFactory().getTransactionManager();
109: }
110:
111: public static UserTransaction getUserTransaction() {
112: return getTransactionFactory().getUserTransaction();
113: }
114:
115: public static String getTxMgrName() {
116: return getTransactionFactory().getTxMgrName();
117: }
118:
119: public static Connection getConnection(String helperName)
120: throws SQLException, GenericEntityException {
121: return getTransactionFactory().getConnection(helperName);
122: }
123:
124: public static void shutdown() {
125: getTransactionFactory().shutdown();
126: }
127: }
|