KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > adapter > DBFactory


1 package org.apache.torque.adapter;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */

21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * This class creates different {@link org.apache.torque.adapter.DB}
27  * objects based on specified JDBC driver name.
28  *
29  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
30  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
31  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
32  * @author <a HREF="mailto:ralf@reswi.ruhr.de">Ralf Stranzenbach</a>
33  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
34  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
35  * @version $Id: DBFactory.java 476550 2006-11-18 16:08:37Z tfischer $
36  */

37 public class DBFactory
38 {
39     /**
40      * JDBC driver to Torque Adapter map.
41      */

42     private static Map JavaDoc adapters = new HashMap JavaDoc(40);
43
44     /**
45      * Initialize the JDBC driver to Torque Adapter map.
46      */

47     static
48     {
49         adapters.put("com.ibm.as400.access.AS400JDBCDriver", DBDB2400.class);
50         adapters.put("COM.ibm.db2.jdbc.app.DB2Driver", DBDB2App.class);
51         adapters.put("COM.ibm.db2.jdbc.net.DB2Driver", DBDB2Net.class);
52         adapters.put("COM.cloudscape.core.JDBCDriver", DBCloudscape.class);
53         adapters.put("org.firebirdsql.jdbc.FBDriver", DBFirebird.class);
54         adapters.put("org.hsql.jdbcDriver", DBHypersonicSQL.class);
55         adapters.put("org.hsqldb.jdbcDriver", DBHypersonicSQL.class);
56         adapters.put("interbase.interclient.Driver", DBInterbase.class);
57         adapters.put("org.enhydra.instantdb.jdbc.idbDriver", DBInstantDB.class);
58         adapters.put("com.microsoft.jdbc.sqlserver.SQLServerDriver",
59             DBMSSQL.class);
60         adapters.put("com.jnetdirect.jsql.JSQLDriver", DBMSSQL.class);
61         adapters.put("org.gjt.mm.mysql.Driver", DBMM.class);
62         adapters.put("oracle.jdbc.driver.OracleDriver", DBOracle.class);
63         adapters.put("org.postgresql.Driver", DBPostgres.class);
64         adapters.put("com.sap.dbtech.jdbc.DriverSapDB", DBSapDB.class);
65         adapters.put("com.sybase.jdbc.SybDriver", DBSybase.class);
66         adapters.put("com.sybase.jdbc2.jdbc.SybDriver", DBSybase.class);
67         adapters.put("weblogic.jdbc.pool.Driver", DBWeblogic.class);
68         adapters.put("org.axiondb.jdbc.AxionDriver", DBAxion.class);
69         adapters.put("com.informix.jdbc.IfxDriver", DBInformix.class);
70         adapters.put("sun.jdbc.odbc.JdbcOdbcDriver", DBOdbc.class);
71
72         adapters.put("com.ibm.db2.jcc.DB2Driver", DBDerby.class);
73         adapters.put("org.apache.derby.jdbc.EmbeddedDriver", DBDerby.class);
74
75
76         // add some short names to be used when drivers are not used
77
adapters.put("as400", DBDB2400.class);
78         adapters.put("db2app", DBDB2App.class);
79         adapters.put("db2net", DBDB2Net.class);
80         adapters.put("cloudscape", DBCloudscape.class);
81         adapters.put("firebird", DBFirebird.class);
82         adapters.put("hypersonic", DBHypersonicSQL.class);
83         adapters.put("interbase", DBInterbase.class);
84         adapters.put("instantdb", DBInstantDB.class);
85         adapters.put("mssql", DBMSSQL.class);
86         adapters.put("mysql", DBMM.class);
87         adapters.put("oracle", DBOracle.class);
88         adapters.put("postgresql", DBPostgres.class);
89         adapters.put("sapdb", DBSapDB.class);
90         adapters.put("sybase", DBSybase.class);
91         adapters.put("weblogic", DBWeblogic.class);
92         adapters.put("axion", DBAxion.class);
93         adapters.put("informix", DBInformix.class);
94         adapters.put("odbc", DBOdbc.class);
95         adapters.put("msaccess", DBOdbc.class);
96
97         adapters.put("derby", DBDerby.class);
98
99         adapters.put("", DBNone.class);
100     }
101
102     /**
103      * Private constructor to prevent instantiation.
104      *
105      * Class contains only static methods, so no instances are needed.
106      */

107     private DBFactory()
108     {
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 JavaDoc key)
123         throws InstantiationException JavaDoc
124     {
125         Class JavaDoc adapterClass = (Class JavaDoc) adapters.get(key);
126
127         if (adapterClass == null)
128         {
129             return null;
130         }
131
132         try
133         {
134             DB adapter = (DB) adapterClass.newInstance();
135             // adapter.setJDBCDriver(driver);
136
return adapter;
137         }
138         catch (IllegalAccessException JavaDoc e)
139         {
140             throw new InstantiationException JavaDoc(
141                 "Could not instantiate adapter for key : "
142                 + key
143                 + ": Assure that adapter classes are in your classpath");
144         }
145     }
146
147     /**
148      * Creates a new instance of the Torque database adapter associated
149      * with the specified JDBC driver or adapter key and the class defined.
150      *
151      * @param key The fully-qualified name of the JDBC driver
152      * or a shorter form adapter key.
153      * @param className The fully qualified name of the adapter class
154      * @return An instance of a Torque database adapter.
155      * @throws InstantiationException throws if the adapter could not be
156      * instantiated
157      */

158     public static DB create(String JavaDoc key, String JavaDoc className)
159         throws InstantiationException JavaDoc
160     {
161         Class JavaDoc adapterClass;
162
163         try
164         {
165             adapterClass = (Class JavaDoc) Class.forName(className);
166         }
167         catch (ClassNotFoundException JavaDoc e)
168         {
169             throw new InstantiationException JavaDoc(
170                     "Could not find adapter "
171                     + className
172                     + " for key "
173                     + key
174                     + ": Check your configuration file");
175         }
176
177         try
178         {
179             DB adapter = (DB) adapterClass.newInstance();
180             adapters.put(key, adapterClass);
181             // adapter.setJDBCDriver(driver);
182
return adapter;
183         }
184         catch (IllegalAccessException JavaDoc e)
185         {
186             throw new InstantiationException JavaDoc(
187                 "Could not instantiate adapter for key: "
188                 + key
189                 + ": Assure that adapter classes are in your classpath");
190         }
191     }
192 }
193
Popular Tags