01: /**
02: * Copyright (C) 2006 NetMind Consulting Bt.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 3 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package hu.netmind.persistence;
18:
19: import javax.sql.DataSource;
20: import java.sql.Connection;
21: import java.sql.DatabaseMetaData;
22: import org.apache.log4j.Logger;
23:
24: /**
25: * Database factory determines which database instance to use,
26: * and instantiates it.
27: * @author Brautigam Robert
28: * @version Revision: $Revision$
29: */
30: public class DatabaseFactory {
31: private static Logger logger = Logger
32: .getLogger(DatabaseFactory.class);
33:
34: /**
35: * Get the database instance for the datasource provided. This method
36: * tries to guess the backend software specific settings and
37: * algorithms, and instantiate a specific database implementation.
38: * @return The database instance.
39: */
40: static Database getDatabase(DataSource source) {
41: try {
42: // Determine database meta-data
43: Connection conn = source.getConnection();
44: DatabaseMetaData databaseMetaData = conn.getMetaData();
45: logger.debug("got data source to: "
46: + databaseMetaData.getDatabaseProductName() + " ("
47: + databaseMetaData.getDatabaseProductVersion()
48: + ") through driver: "
49: + databaseMetaData.getDriverName() + " ("
50: + databaseMetaData.getDriverVersion() + ")");
51: String databaseName = databaseMetaData
52: .getDatabaseProductName();
53: conn.close();
54: // Create and return database implementation
55: DatabaseImplementation implementation = null;
56: if (databaseName.equalsIgnoreCase("postgresql")) {
57: logger.debug("selecting postgres implementation.");
58: implementation = new PostgresDatabaseImpl();
59: } else if (databaseName.equalsIgnoreCase("mysql")) {
60: logger.debug("selecting mysql implementation.");
61: implementation = new MysqlDatabaseImpl();
62: } else if (databaseName.equalsIgnoreCase("oracle")) {
63: logger.debug("selecting oracle implementation.");
64: implementation = new OracleDatabaseImpl();
65: } else if (databaseName
66: .equalsIgnoreCase("hsql database engine")) {
67: logger.debug("selecting hsql implementation.");
68: implementation = new HSQLDatabaseImpl();
69: // } else if ( databaseName.equalsIgnoreCase("apache derby") ) {
70: // logger.debug("selecting derby implementation.");
71: // implementation = new DerbyDatabaseImpl();
72: } else {
73: logger
74: .fatal("unknown database '"
75: + databaseName
76: + "', can not support promised features, so bailing out.");
77: throw new StoreException(
78: "Unknown database encountered: "
79: + databaseName
80: + ", cannot continue. For a list of supported databases consult the documentation.");
81: }
82: // Return
83: return new Database(new ConnectionSource(source),
84: implementation);
85: } catch (StoreException e) {
86: throw e;
87: } catch (Exception e) {
88: throw new StoreException(
89: "could not instantiate database implementation", e);
90: }
91: }
92:
93: }
|