View Javadoc

1   /*
2    * org.riverock.dbrevision - Database revision engine
3    * For more information about DbRevision, please visit project site
4    * http://www.riverock.org
5    *
6    * Copyright (C) 2006-2006, Riverock Software, All Rights Reserved.
7    *
8    * Riverock - The Open-source Java Development Community
9    * http://www.riverock.org
10   *
11   *
12   * This library is free software; you can redistribute it and/or
13   * modify it under the terms of the GNU Lesser General Public
14   * License as published by the Free Software Foundation; either
15   * version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25   */
26  package org.riverock.dbrevision.db;
27  
28  import java.lang.reflect.Constructor;
29  import java.sql.Connection;
30  import java.util.HashMap;
31  import java.util.Map;
32  import java.util.List;
33  import java.util.ArrayList;
34  
35  import org.apache.log4j.Logger;
36  
37  import org.riverock.dbrevision.db.impl.HyperSonicDatabase;
38  import org.riverock.dbrevision.db.impl.DB2Database;
39  import org.riverock.dbrevision.db.impl.SqlServerDatabase;
40  import org.riverock.dbrevision.db.impl.MySqlDatabase;
41  import org.riverock.dbrevision.db.impl.OracleDatabase;
42  import org.riverock.dbrevision.db.impl.PostgreeSqlDatabase;
43  import org.riverock.dbrevision.db.impl.MaxDBDatabase;
44  import org.riverock.dbrevision.exception.DbRevisionException;
45  
46  /**
47   * @author Sergei Maslyukov
48   *         Date: 04.07.2006
49   *         Time: 12:03:41
50   */
51  public class DatabaseFactory {
52      private final static Logger log = Logger.getLogger(DatabaseFactory.class);
53  
54      private static Map<Database.Family, Class> familyClassMap = new HashMap<Database.Family, Class>();
55      private static Map<String, Database.Family> familyCodeMap = new HashMap<String, Database.Family>();
56  
57      public static final String ORACLE_FAMILY = "oracle";
58      public static final String MYSQL_FAMILY = "mysql";
59      public static final String HYPERSONIC_FAMILY = "hypersonic";
60      public static final String SQLSERVER_FAMILY = "sqlserver";
61      public static final String POSTGREES_FAMILY = "postgrees";
62      public static final String DB2_FAMILY = "db2";
63      public static final String MAXDB_FAMILY = "maxdb";
64  
65      static {
66          familyClassMap.put(Database.Family.ORACLE, OracleDatabase.class);
67          familyClassMap.put(Database.Family.MYSQL, MySqlDatabase.class);
68          familyClassMap.put(Database.Family.HYPERSONIC, HyperSonicDatabase.class);
69          familyClassMap.put(Database.Family.SQLSERVER, SqlServerDatabase.class);
70          familyClassMap.put(Database.Family.POSTGREES, PostgreeSqlDatabase.class);
71          familyClassMap.put(Database.Family.DB2, DB2Database.class);
72          familyClassMap.put(Database.Family.MAXDB, MaxDBDatabase.class);
73      }
74  
75      static {
76          familyCodeMap.put(ORACLE_FAMILY, Database.Family.ORACLE);
77          familyCodeMap.put(MYSQL_FAMILY, Database.Family.MYSQL);
78          familyCodeMap.put(HYPERSONIC_FAMILY, Database.Family.HYPERSONIC);
79          familyCodeMap.put(SQLSERVER_FAMILY, Database.Family.SQLSERVER);
80          familyCodeMap.put(POSTGREES_FAMILY, Database.Family.POSTGREES);
81          familyCodeMap.put(DB2_FAMILY, Database.Family.DB2);
82          familyCodeMap.put(MAXDB_FAMILY, Database.Family.MAXDB);
83      }
84  
85      public static List<String> getSupportedFamilyCode() {
86          return new ArrayList<String>(familyCodeMap.keySet());
87      }
88  
89      public static Database.Family decodeFamily(String familyCode) {
90          return familyCodeMap.get(familyCode);
91      }
92  
93      public static Database getInstance(final Connection connection, String familyCode) {
94          return getInstance(connection, decodeFamily(familyCode));
95      }
96  
97      public static Database getInstance(final Connection connection, Database.Family family) {
98          if (connection == null) {
99              String es = "Connection is null.";
100             log.fatal(es);
101             throw new DbRevisionException(es);
102         }
103         if (family == null) {
104             String es = "dbFamily not defined.";
105             log.fatal(es);
106             throw new DbRevisionException(es);
107         }
108 
109         if (log.isDebugEnabled()) {
110             log.debug("dc.getFamily(): " + family);
111         }
112         Class clazz = familyClassMap.get(family);
113         if (log.isDebugEnabled()) {
114             log.debug("clazz: " + clazz);
115         }
116 
117         if (clazz == null) {
118             throw new IllegalStateException("Database for family '"+family+"' not found");
119         }
120 
121         try {
122             Constructor constructor = clazz.getConstructor(Connection.class);
123             Database db = (Database)constructor.newInstance(connection);
124 
125             if (log.isDebugEnabled()) {
126                 log.debug("Success create dynamic object: " + db);
127             }
128             return db;
129         }
130         catch (Exception e) {
131             log.fatal("Error create instance for family " + family);
132             log.fatal("Error:", e);
133 
134             final String es = "Error create Database instance.";
135             System.out.println(es);
136             throw new DbRevisionException(es, e);
137         }
138     }
139 }