| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * 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 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.db.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.service.ServiceFactory; |
| 22 | import hu.netmind.beankeeper.service.StoreContext; |
| 23 | import hu.netmind.beankeeper.service.Service; |
| 24 | import hu.netmind.beankeeper.common.StoreException; |
| 25 | import hu.netmind.beankeeper.db.*; |
| 26 | import javax.sql.DataSource; |
| 27 | import java.sql.Connection; |
| 28 | import java.sql.DatabaseMetaData; |
| 29 | import org.apache.log4j.Logger; |
| 30 | import hu.netmind.beankeeper.config.ConfigurationTracker; |
| 31 | import hu.netmind.beankeeper.logging.SnapshotLogger; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | /** |
| 35 | * Database factory determines which database instance to use, |
| 36 | * and instantiates it. |
| 37 | * @author Brautigam Robert |
| 38 | * @version Revision: $Revision$ |
| 39 | */ |
| 40 | public class DatabaseFactory implements ServiceFactory |
| 41 | { |
| 42 | private static Logger logger = Logger.getLogger(DatabaseFactory.class); |
| 43 | |
| 44 | private Object instance = null; |
| 45 | |
| 46 | private ConfigurationTracker configurationTracker = null; // Injected |
| 47 | private SnapshotLogger snapshotLogger = null; // Injected |
| 48 | |
| 49 | /** |
| 50 | * Return the service instance created by this factory. |
| 51 | */ |
| 52 | public Service getService() |
| 53 | { |
| 54 | return (Service) instance; |
| 55 | } |
| 56 | |
| 57 | public void release() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the database instance for the datasource provided. This method |
| 63 | * tries to guess the backend software specific settings and |
| 64 | * algorithms, and instantiate a specific database implementation. |
| 65 | * @return The database instance. |
| 66 | */ |
| 67 | public void init(Map parameters) |
| 68 | { |
| 69 | try |
| 70 | { |
| 71 | // Get the datasource |
| 72 | DataSource source = (DataSource) parameters.get(StoreContext.PARAM_DATASOURCE); |
| 73 | if ( source == null ) |
| 74 | { |
| 75 | String driver = (String) parameters.get(StoreContext.PARAM_DRIVERCLASS); |
| 76 | String url = (String) parameters.get(StoreContext.PARAM_DRIVERURL); |
| 77 | if ( (driver==null) || (url==null) ) |
| 78 | throw new StoreException("Database can not be initialized, no datasource or driver parameters given."); |
| 79 | source = new DriverDataSource(driver,url); |
| 80 | } |
| 81 | // Determine database meta-data |
| 82 | Connection conn = source.getConnection(); |
| 83 | DatabaseMetaData databaseMetaData = conn.getMetaData(); |
| 84 | logger.info("got data source to: "+databaseMetaData.getDatabaseProductName()+ |
| 85 | " ("+databaseMetaData.getDatabaseProductVersion()+") through driver: "+ |
| 86 | databaseMetaData.getDriverName()+" ("+databaseMetaData.getDriverVersion()+")"); |
| 87 | String databaseName = databaseMetaData.getDatabaseProductName(); |
| 88 | conn.close(); |
| 89 | // Create connection pool and add to parameters |
| 90 | ConnectionSource connectionSource = new ConnectionSourceImpl(configurationTracker,snapshotLogger,source); |
| 91 | parameters.put(StoreContext.PARAM_CONNECTIONSOURCE,connectionSource); |
| 92 | // Create and return database implementation |
| 93 | if ( databaseName.equalsIgnoreCase("postgresql") ) |
| 94 | { |
| 95 | logger.debug("selecting postgres implementation."); |
| 96 | instance = new PostgresDatabaseImpl(); |
| 97 | } else if ( databaseName.equalsIgnoreCase("mysql") ) { |
| 98 | logger.debug("selecting mysql implementation."); |
| 99 | instance = new MysqlDatabaseImpl(); |
| 100 | } else if ( databaseName.equalsIgnoreCase("oracle") ) { |
| 101 | logger.debug("selecting oracle implementation."); |
| 102 | instance = new OracleDatabaseImpl(); |
| 103 | } else if ( databaseName.equalsIgnoreCase("hsql database engine") ) { |
| 104 | logger.debug("selecting hsql implementation."); |
| 105 | instance = new HSQLDatabaseImpl(); |
| 106 | } else { |
| 107 | logger.fatal("unknown database '"+databaseName+"', can not support promised features, so bailing out."); |
| 108 | throw new StoreException("Unknown database encountered: "+databaseName+ |
| 109 | ", cannot continue. For a list of supported databases consult the documentation."); |
| 110 | } |
| 111 | } catch ( StoreException e ) { |
| 112 | throw e; |
| 113 | } catch ( Exception e ) { |
| 114 | throw new StoreException("could not instantiate database implementation",e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |