001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.codegen;
042:
043: import java.util.HashMap;
044: import java.util.Map;
045: import com.sun.sql.framework.exception.BaseException;
046:
047: /**
048: * DBFactory is a factory to get instance of a specific DB implementation.
049: *
050: * @author Ritesh Adval
051: * @author Ahimanikya Satapathy
052: * @version $Revision$
053: */
054: public class DBFactory {
055:
056: private static DBFactory instance = null;
057: private final Map<Integer, String> dbTypeToDbClassMap = new HashMap<Integer, String>();
058: private Map<Integer, DB> dbMap = new HashMap<Integer, DB>();
059:
060: public static DBFactory getInstance() {
061: if (instance == null) {
062: instance = new DBFactory();
063: }
064: return instance;
065: }
066:
067: private DBFactory() {
068: dbTypeToDbClassMap
069: .put(new Integer(DB.BASEDB),
070: "org.netbeans.modules.sql.framework.codegen.base.BaseDB");
071: dbTypeToDbClassMap
072: .put(new Integer(DB.ORACLE8DB),
073: "org.netbeans.modules.sql.framework.codegen.oracle8.Oracle8DB");
074: dbTypeToDbClassMap
075: .put(new Integer(DB.ORACLE9DB),
076: "org.netbeans.modules.sql.framework.codegen.oracle9.Oracle9DB");
077: dbTypeToDbClassMap
078: .put(new Integer(DB.SQLSERVERDB),
079: "org.netbeans.modules.sql.framework.codegen.sqlserver.SqlServerDB");
080: dbTypeToDbClassMap
081: .put(new Integer(DB.SYBASEDB),
082: "org.netbeans.modules.sql.framework.codegen.sybase.SybaseDB");
083: dbTypeToDbClassMap
084: .put(new Integer(DB.DB2V5DB),
085: "org.netbeans.modules.sql.framework.codegen.db2v5.DB2V5DB");
086: dbTypeToDbClassMap
087: .put(new Integer(DB.DB2V7DB),
088: "org.netbeans.modules.sql.framework.codegen.db2v7.DB2V7DB");
089: dbTypeToDbClassMap
090: .put(new Integer(DB.DB2V8DB),
091: "org.netbeans.modules.sql.framework.codegen.db2v8.DB2V8DB");
092: dbTypeToDbClassMap
093: .put(new Integer(DB.AXIONDB),
094: "org.netbeans.modules.sql.framework.codegen.axion.AxionDB");
095: dbTypeToDbClassMap
096: .put(new Integer(DB.DERBYDB),
097: "org.netbeans.modules.sql.framework.codegen.derby.DerbyDB");
098: dbTypeToDbClassMap
099: .put(new Integer(DB.JDBCDB),
100: "org.netbeans.modules.sql.framework.codegen.jdbc.JdbcDB");
101: dbTypeToDbClassMap
102: .put(new Integer(DB.PostgreSQL),
103: "org.netbeans.modules.sql.framework.codegen.postgreSQL.PostgreSQLDB");
104: dbTypeToDbClassMap
105: .put(new Integer(DB.MYSQLDB),
106: "org.netbeans.modules.sql.framework.codegen.mysql.MySQLDB");
107: }
108:
109: public DB getDatabase(int dbType) throws BaseException {
110: Integer intDbType = new Integer(dbType);
111: DB db = dbMap.get(intDbType);
112:
113: if (db == null) {
114: String dbClass = dbTypeToDbClassMap
115: .get(new Integer(dbType));
116: if (dbClass == null) {
117: throw new BaseException("Cannot find a DB for type: "
118: + dbType);
119: }
120:
121: try {
122: Class cls = Class.forName(dbClass);
123: db = (DB) cls.newInstance();
124: dbMap.put(intDbType, db);
125: } catch (ClassNotFoundException ex1) {
126: throw new BaseException(
127: "Cannnot create an instance of DB of class "
128: + dbClass, ex1);
129: } catch (InstantiationException ex2) {
130: throw new BaseException(
131: "Cannnot create an instance of DB of class "
132: + dbClass, ex2);
133: } catch (IllegalAccessException ex3) {
134: throw new BaseException(
135: "Cannot create an instance of DB of class "
136: + dbClass, ex3);
137: }
138: }
139:
140: return db;
141: }
142: }
|