001: /*
002: * $Id: ConnectionFactory.java,v 1.3 2003/12/01 20:46:49 ajzeneski 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: */
025: package org.ofbiz.entity.jdbc;
026:
027: import java.sql.Connection;
028: import java.sql.DriverManager;
029: import java.sql.SQLException;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.entity.GenericEntityException;
033: import org.ofbiz.entity.transaction.DBCPConnectionFactory;
034: import org.ofbiz.entity.transaction.TransactionFactory;
035: import org.ofbiz.entity.transaction.MinervaConnectionFactory;
036: import org.w3c.dom.Element;
037:
038: /**
039: * ConnectionFactory - central source for JDBC connections
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.3 $
044: * @since 2.0
045: */
046: public class ConnectionFactory {
047: // Debug module name
048: public static final String module = ConnectionFactory.class
049: .getName();
050:
051: public static Connection getConnection(String helperName)
052: throws SQLException, GenericEntityException {
053: // Debug.logVerbose("Getting a connection", module);
054:
055: Connection con = TransactionFactory.getConnection(helperName);
056: if (con == null) {
057: Debug.logError(
058: "******* ERROR: No database connection found for helperName \""
059: + helperName + "\"", module);
060: }
061: return con;
062: }
063:
064: public static Connection tryGenericConnectionSources(
065: String helperName, Element inlineJdbcElement)
066: throws SQLException, GenericEntityException {
067: // first try Minerva
068: try {
069: Connection con = MinervaConnectionFactory.getConnection(
070: helperName, inlineJdbcElement);
071: if (con != null)
072: return con;
073: } catch (Exception ex) {
074: Debug.logError(ex,
075: "There was an error getting a Minerva datasource.",
076: module);
077: }
078:
079: // next try DBCP
080: try {
081: Connection con = DBCPConnectionFactory.getConnection(
082: helperName, inlineJdbcElement);
083: if (con != null)
084: return con;
085: } catch (Exception ex) {
086: Debug.logError(ex,
087: "There was an error getting a DBCP datasource.",
088: module);
089: }
090:
091: // Default to plain JDBC.
092: String driverClassName = inlineJdbcElement
093: .getAttribute("jdbc-driver");
094:
095: if (driverClassName != null && driverClassName.length() > 0) {
096: try {
097: ClassLoader loader = Thread.currentThread()
098: .getContextClassLoader();
099: Class clazz = loader.loadClass(driverClassName);
100: clazz.newInstance();
101: } catch (ClassNotFoundException e) {
102: Debug.logWarning(e,
103: "Could not find JDBC driver class named "
104: + driverClassName, module);
105: return null;
106: } catch (java.lang.IllegalAccessException e) {
107: Debug.logWarning(e,
108: "Not allowed to access JDBC driver class named "
109: + driverClassName, module);
110: return null;
111: } catch (java.lang.InstantiationException e) {
112: Debug.logWarning(e,
113: "Could not create new instance of JDBC driver class named "
114: + driverClassName, module);
115: return null;
116: }
117: return DriverManager.getConnection(inlineJdbcElement
118: .getAttribute("jdbc-uri"), inlineJdbcElement
119: .getAttribute("jdbc-username"), inlineJdbcElement
120: .getAttribute("jdbc-password"));
121: }
122:
123: return null;
124: }
125: }
|