001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.database;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.lang.StringUtils;
024
025 import java.sql.Connection;
026 import java.sql.DriverManager;
027 import java.sql.SQLException;
028 import java.util.Properties;
029
030 public class DriverDatabaseConnector extends AbstractDatabaseConnector {
031
032 public DriverDatabaseConnector(Configuration configuration) {
033 super(configuration, true);
034 }
035
036 public String getDriver() {
037 return getConfiguration().getString(DatabaseProperties.PROP_DRIVER);
038 }
039
040 public String getUrl() {
041 return getConfiguration().getString(DatabaseProperties.PROP_URL);
042 }
043
044 public String getUsername() {
045 return getConfiguration().getString(DatabaseProperties.PROP_USER);
046 }
047
048 public String getPassword() {
049 return getConfiguration().getString(DatabaseProperties.PROP_PASSWORD);
050 }
051
052 public Connection getConnection() throws SQLException {
053 try {
054 Class.forName(getDriver());
055 } catch (ClassNotFoundException e) {
056 SQLException ex = new SQLException("SQL driver not found " + getDriver());
057 ex.initCause(e);
058 throw ex;
059 }
060 return DriverManager.getConnection(getUrl(), getUsername(), getPassword());
061 }
062
063 @Override
064 public void setupEntityManagerFactory(Properties factoryProps) {
065 factoryProps.put("hibernate.connection.url", getUrl());
066 factoryProps.put("hibernate.connection.driver_class", getDriver());
067 factoryProps.put("hibernate.connection.username", getUsername());
068 if (StringUtils.isNotEmpty(getPassword())) {
069 factoryProps.put("hibernate.connection.password", getPassword());
070 }
071 }
072 }