001: package org.apache.torque.adapter;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: /**
026: * This class creates different {@link org.apache.torque.adapter.DB}
027: * objects based on specified JDBC driver name.
028: *
029: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
030: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
031: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
032: * @author <a href="mailto:ralf@reswi.ruhr.de">Ralf Stranzenbach</a>
033: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
034: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035: * @version $Id: DBFactory.java 476550 2006-11-18 16:08:37Z tfischer $
036: */
037: public class DBFactory {
038: /**
039: * JDBC driver to Torque Adapter map.
040: */
041: private static Map adapters = new HashMap(40);
042:
043: /**
044: * Initialize the JDBC driver to Torque Adapter map.
045: */
046: static {
047: adapters.put("com.ibm.as400.access.AS400JDBCDriver",
048: DBDB2400.class);
049: adapters.put("COM.ibm.db2.jdbc.app.DB2Driver", DBDB2App.class);
050: adapters.put("COM.ibm.db2.jdbc.net.DB2Driver", DBDB2Net.class);
051: adapters.put("COM.cloudscape.core.JDBCDriver",
052: DBCloudscape.class);
053: adapters.put("org.firebirdsql.jdbc.FBDriver", DBFirebird.class);
054: adapters.put("org.hsql.jdbcDriver", DBHypersonicSQL.class);
055: adapters.put("org.hsqldb.jdbcDriver", DBHypersonicSQL.class);
056: adapters.put("interbase.interclient.Driver", DBInterbase.class);
057: adapters.put("org.enhydra.instantdb.jdbc.idbDriver",
058: DBInstantDB.class);
059: adapters.put("com.microsoft.jdbc.sqlserver.SQLServerDriver",
060: DBMSSQL.class);
061: adapters.put("com.jnetdirect.jsql.JSQLDriver", DBMSSQL.class);
062: adapters.put("org.gjt.mm.mysql.Driver", DBMM.class);
063: adapters.put("oracle.jdbc.driver.OracleDriver", DBOracle.class);
064: adapters.put("org.postgresql.Driver", DBPostgres.class);
065: adapters.put("com.sap.dbtech.jdbc.DriverSapDB", DBSapDB.class);
066: adapters.put("com.sybase.jdbc.SybDriver", DBSybase.class);
067: adapters.put("com.sybase.jdbc2.jdbc.SybDriver", DBSybase.class);
068: adapters.put("weblogic.jdbc.pool.Driver", DBWeblogic.class);
069: adapters.put("org.axiondb.jdbc.AxionDriver", DBAxion.class);
070: adapters.put("com.informix.jdbc.IfxDriver", DBInformix.class);
071: adapters.put("sun.jdbc.odbc.JdbcOdbcDriver", DBOdbc.class);
072:
073: adapters.put("com.ibm.db2.jcc.DB2Driver", DBDerby.class);
074: adapters.put("org.apache.derby.jdbc.EmbeddedDriver",
075: DBDerby.class);
076:
077: // add some short names to be used when drivers are not used
078: adapters.put("as400", DBDB2400.class);
079: adapters.put("db2app", DBDB2App.class);
080: adapters.put("db2net", DBDB2Net.class);
081: adapters.put("cloudscape", DBCloudscape.class);
082: adapters.put("firebird", DBFirebird.class);
083: adapters.put("hypersonic", DBHypersonicSQL.class);
084: adapters.put("interbase", DBInterbase.class);
085: adapters.put("instantdb", DBInstantDB.class);
086: adapters.put("mssql", DBMSSQL.class);
087: adapters.put("mysql", DBMM.class);
088: adapters.put("oracle", DBOracle.class);
089: adapters.put("postgresql", DBPostgres.class);
090: adapters.put("sapdb", DBSapDB.class);
091: adapters.put("sybase", DBSybase.class);
092: adapters.put("weblogic", DBWeblogic.class);
093: adapters.put("axion", DBAxion.class);
094: adapters.put("informix", DBInformix.class);
095: adapters.put("odbc", DBOdbc.class);
096: adapters.put("msaccess", DBOdbc.class);
097:
098: adapters.put("derby", DBDerby.class);
099:
100: adapters.put("", DBNone.class);
101: }
102:
103: /**
104: * Private constructor to prevent instantiation.
105: *
106: * Class contains only static methods, so no instances are needed.
107: */
108: private DBFactory() {
109: }
110:
111: /**
112: * Creates a new instance of the Torque database adapter associated
113: * with the specified JDBC driver or adapter key.
114: *
115: * @param key The fully-qualified name of the JDBC driver
116: * or a shorter form adapter key.
117: * @return An instance of a Torque database adapter, or null if
118: * no adapter exists for the given key.
119: * @throws InstantiationException throws if the adapter could not be
120: * instantiated
121: */
122: public static DB create(String key) throws InstantiationException {
123: Class adapterClass = (Class) adapters.get(key);
124:
125: if (adapterClass == null) {
126: return null;
127: }
128:
129: try {
130: DB adapter = (DB) adapterClass.newInstance();
131: // adapter.setJDBCDriver(driver);
132: return adapter;
133: } catch (IllegalAccessException e) {
134: throw new InstantiationException(
135: "Could not instantiate adapter for key : "
136: + key
137: + ": Assure that adapter classes are in your classpath");
138: }
139: }
140:
141: /**
142: * Creates a new instance of the Torque database adapter associated
143: * with the specified JDBC driver or adapter key and the class defined.
144: *
145: * @param key The fully-qualified name of the JDBC driver
146: * or a shorter form adapter key.
147: * @param className The fully qualified name of the adapter class
148: * @return An instance of a Torque database adapter.
149: * @throws InstantiationException throws if the adapter could not be
150: * instantiated
151: */
152: public static DB create(String key, String className)
153: throws InstantiationException {
154: Class adapterClass;
155:
156: try {
157: adapterClass = (Class) Class.forName(className);
158: } catch (ClassNotFoundException e) {
159: throw new InstantiationException("Could not find adapter "
160: + className + " for key " + key
161: + ": Check your configuration file");
162: }
163:
164: try {
165: DB adapter = (DB) adapterClass.newInstance();
166: adapters.put(key, adapterClass);
167: // adapter.setJDBCDriver(driver);
168: return adapter;
169: } catch (IllegalAccessException e) {
170: throw new InstantiationException(
171: "Could not instantiate adapter for key: "
172: + key
173: + ": Assure that adapter classes are in your classpath");
174: }
175: }
176: }
|