View Javadoc

1   package org.apache.torque.adapter;
2   
3   /*
4    * Copyright 2001-2005 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   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @version $Id: DBFactory.java 239630 2005-08-24 12:25:32Z henning $
33   */
34  public class DBFactory
35  {
36      /***
37       * JDBC driver to Torque Adapter map.
38       */
39      private static Map adapters = new HashMap(40);
40  
41      /***
42       * Initialize the JDBC driver to Torque Adapter map.
43       */
44      static
45      {
46          adapters.put("com.ibm.as400.access.AS400JDBCDriver", DBDB2400.class);
47          adapters.put("COM.ibm.db2.jdbc.app.DB2Driver", DBDB2App.class);
48          adapters.put("COM.ibm.db2.jdbc.net.DB2Driver", DBDB2Net.class);
49          adapters.put("COM.cloudscape.core.JDBCDriver", DBCloudscape.class);
50          adapters.put("org.hsql.jdbcDriver", DBHypersonicSQL.class);
51          adapters.put("org.hsqldb.jdbcDriver", DBHypersonicSQL.class);
52          adapters.put("interbase.interclient.Driver", DBInterbase.class);
53          adapters.put("org.enhydra.instantdb.jdbc.idbDriver", DBInstantDB.class);
54          adapters.put("com.microsoft.jdbc.sqlserver.SQLServerDriver",
55              DBMSSQL.class);
56          adapters.put("com.jnetdirect.jsql.JSQLDriver", DBMSSQL.class);
57          adapters.put("org.gjt.mm.mysql.Driver", DBMM.class);
58          adapters.put("oracle.jdbc.driver.OracleDriver", DBOracle.class);
59          adapters.put("org.postgresql.Driver", DBPostgres.class);
60          adapters.put("com.sap.dbtech.jdbc.DriverSapDB", DBSapDB.class);
61          adapters.put("com.sybase.jdbc.SybDriver", DBSybase.class);
62          adapters.put("com.sybase.jdbc2.jdbc.SybDriver", DBSybase.class);
63          adapters.put("weblogic.jdbc.pool.Driver", DBWeblogic.class);
64          adapters.put("org.axiondb.jdbc.AxionDriver", DBAxion.class);
65          adapters.put("com.informix.jdbc.IfxDriver", DBInformix.class);
66          adapters.put("sun.jdbc.odbc.JdbcOdbcDriver", DBOdbc.class);
67  
68          adapters.put("com.ibm.db2.jcc.DB2Driver", DBDerby.class);
69          adapters.put("org.apache.derby.jdbc.EmbeddedDriver", DBDerby.class);
70  
71  
72          // add some short names to be used when drivers are not used
73          adapters.put("as400", DBDB2400.class);
74          adapters.put("db2app", DBDB2App.class);
75          adapters.put("db2net", DBDB2Net.class);
76          adapters.put("cloudscape", DBCloudscape.class);
77          adapters.put("hypersonic", DBHypersonicSQL.class);
78          adapters.put("interbase", DBInterbase.class);
79          adapters.put("instantdb", DBInstantDB.class);
80          adapters.put("mssql", DBMSSQL.class);
81          adapters.put("mysql", DBMM.class);
82          adapters.put("oracle", DBOracle.class);
83          adapters.put("postgresql", DBPostgres.class);
84          adapters.put("sapdb", DBSapDB.class);
85          adapters.put("sybase", DBSybase.class);
86          adapters.put("weblogic", DBWeblogic.class);
87          adapters.put("axion", DBAxion.class);
88          adapters.put("informix", DBInformix.class);
89          adapters.put("odbc", DBOdbc.class);
90          adapters.put("msaccess", DBOdbc.class);
91  
92          adapters.put("derby", DBDerby.class);
93  
94          adapters.put("", DBNone.class);
95      }
96  
97      /***
98       * Creates a new instance of the Torque database adapter associated
99       * with the specified JDBC driver or adapter key.
100      *
101      * @param driver The fully-qualified name of the JDBC driver to
102      * create a new adapter instance for or a shorter form adapter key.
103      * @return An instance of a Torque database adapter.
104      * @throws InstantiationException throws if the JDBC driver could not be
105      *      instantiated
106      */
107     public static DB create(String driver)
108         throws InstantiationException
109     {
110         Class adapterClass = (Class) adapters.get(driver);
111 
112         if (adapterClass != null)
113         {
114             try
115             {
116                 DB adapter = (DB) adapterClass.newInstance();
117                 // adapter.setJDBCDriver(driver);
118                 return adapter;
119             }
120             catch (IllegalAccessException e)
121             {
122                 throw new InstantiationException(
123                     "Could not instantiate adapter for JDBC driver: "
124                     + driver
125                     + ": Assure that adapter bytecodes are in your classpath");
126             }
127         }
128         else
129         {
130             throw new InstantiationException(
131                 "Unknown JDBC driver: "
132                 + driver
133                 + ": Check your configuration file");
134         }
135     }
136 }