View Javadoc

1   package org.apache.torque.adapter;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /***
23   * This class creates different {@link org.apache.torque.adapter.DB}
24   * objects based on specified JDBC driver name.
25   *
26   * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
27   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
28   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
29   * @author <a href="mailto:ralf@reswi.ruhr.de">Ralf Stranzenbach</a>
30   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31   * @version $Id: DBFactory.java,v 1.35.2.2 2004/05/20 04:35:15 seade Exp $
32   */
33  public class DBFactory
34  {
35      /***
36       * JDBC driver to Torque Adapter map.
37       */
38      private static Map adapters = new HashMap(40);
39  
40      /***
41       * Initialize the JDBC driver to Torque Adapter map.
42       */
43      static
44      {
45          adapters.put("com.ibm.as400.access.AS400JDBCDriver", DBDB2400.class);
46          adapters.put("COM.ibm.db2.jdbc.app.DB2Driver", DBDB2App.class);
47          adapters.put("COM.ibm.db2.jdbc.net.DB2Driver", DBDB2Net.class);
48          adapters.put("COM.cloudscape.core.JDBCDriver", DBCloudscape.class);
49          adapters.put("org.hsql.jdbcDriver", DBHypersonicSQL.class);
50          adapters.put("org.hsqldb.jdbcDriver", DBHypersonicSQL.class);
51          adapters.put("interbase.interclient.Driver", DBInterbase.class);
52          adapters.put("org.enhydra.instantdb.jdbc.idbDriver", DBInstantDB.class);
53          adapters.put("com.microsoft.jdbc.sqlserver.SQLServerDriver",
54              DBMSSQL.class);
55          adapters.put("com.jnetdirect.jsql.JSQLDriver", DBMSSQL.class);
56          adapters.put("org.gjt.mm.mysql.Driver", DBMM.class);
57          adapters.put("oracle.jdbc.driver.OracleDriver", DBOracle.class);
58          adapters.put("org.postgresql.Driver", DBPostgres.class);
59          adapters.put("com.sap.dbtech.jdbc.DriverSapDB", DBSapDB.class);
60          adapters.put("com.sybase.jdbc.SybDriver", DBSybase.class);
61          adapters.put("com.sybase.jdbc2.jdbc.SybDriver", DBSybase.class);
62          adapters.put("weblogic.jdbc.pool.Driver", DBWeblogic.class);
63          adapters.put("org.axiondb.jdbc.AxionDriver", DBAxion.class);
64          adapters.put("com.informix.jdbc.IfxDriver", DBInformix.class);
65          adapters.put("sun.jdbc.odbc.JdbcOdbcDriver", DBOdbc.class);
66  
67          // add some short names to be used when drivers are not used
68          adapters.put("as400", DBDB2400.class);
69          adapters.put("db2app", DBDB2App.class);
70          adapters.put("db2net", DBDB2Net.class);
71          adapters.put("cloudscape", DBCloudscape.class);
72          adapters.put("hypersonic", DBHypersonicSQL.class);
73          adapters.put("interbase", DBInterbase.class);
74          adapters.put("instantdb", DBInstantDB.class);
75          adapters.put("mssql", DBMSSQL.class);
76          adapters.put("mysql", DBMM.class);
77          adapters.put("oracle", DBOracle.class);
78          adapters.put("postgresql", DBPostgres.class);
79          adapters.put("sapdb", DBSapDB.class);
80          adapters.put("sybase", DBSybase.class);
81          adapters.put("weblogic", DBWeblogic.class);
82          adapters.put("axion", DBAxion.class);
83          adapters.put("informix", DBInformix.class);
84          adapters.put("odbc", DBOdbc.class);
85          adapters.put("msaccess", DBOdbc.class);
86  
87          adapters.put("", DBNone.class);
88      }
89  
90      /***
91       * Creates a new instance of the Torque database adapter associated
92       * with the specified JDBC driver or adapter key.
93       *
94       * @param driver The fully-qualified name of the JDBC driver to
95       * create a new adapter instance for or a shorter form adapter key.
96       * @return An instance of a Torque database adapter.
97       * @throws InstantiationException throws if the JDBC driver could not be
98       *      instantiated
99       */
100     public static DB create(String driver)
101         throws InstantiationException
102     {
103         Class adapterClass = (Class) adapters.get(driver);
104 
105         if (adapterClass != null)
106         {
107             try
108             {
109                 DB adapter = (DB) adapterClass.newInstance();
110                 // adapter.setJDBCDriver(driver);
111                 return adapter;
112             }
113             catch (IllegalAccessException e)
114             {
115                 throw new InstantiationException(
116                     "Could not instantiate adapter for JDBC driver: "
117                     + driver
118                     + ": Assure that adapter bytecodes are in your classpath");
119             }
120         }
121         else
122         {
123             throw new InstantiationException(
124                 "Unknown JDBC driver: "
125                 + driver
126                 + ": Check your configuration file");
127         }
128     }
129 }