ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@10: package de.intevation.artifactdatabase; sascha@10: sascha@207: import de.intevation.artifacts.common.utils.Config; sascha@207: sascha@93: import java.io.File; sascha@93: sascha@93: import java.sql.SQLException; sascha@93: sascha@10: import javax.sql.DataSource; sascha@10: sascha@10: import org.apache.commons.dbcp.BasicDataSource; sascha@10: sascha@38: import org.apache.log4j.Logger; sascha@38: sascha@10: /** sascha@87: * This class encapsulate the creation and pooling of database connections used sascha@87: * by the artifact database. The credential to open the database connections sascha@87: * are taken from the global configuratiion. sascha@87: * @author Sascha L. Teichmann sascha@10: */ sascha@10: public class DBConnection sascha@10: { sascha@38: private static Logger logger = Logger.getLogger(DBConnection.class); sascha@38: sascha@87: /** sascha@87: * XPath to access the database driver within the global configuration. sascha@87: */ sascha@10: public static final String DB_DRIVER = sascha@38: "/artifact-database/database/driver/text()"; sascha@87: /** sascha@87: * XPath to access the database URL within the global configuration. sascha@87: */ sascha@10: public static final String DB_URL = sascha@38: "/artifact-database/database/url/text()"; sascha@87: /** sascha@87: * XPath to access the database use within the global configuration. sascha@87: */ sascha@10: public static final String DB_USER = sascha@38: "/artifact-database/database/user/text()"; sascha@87: /** sascha@87: * XPath to access the database password within the global configuration. sascha@87: */ sascha@10: public static final String DB_PASSWORD = sascha@38: "/artifact-database/database/password/text()"; sascha@10: sascha@87: /** sascha@87: * The default database driver: H2 sascha@87: */ sascha@10: public static final String DEFAULT_DRIVER = sascha@10: "org.h2.Driver"; sascha@10: sascha@87: /** sascha@87: * The default database name: artifacts.db sascha@87: */ sascha@19: public static final String DEFAULT_DATABASE_FILE = sascha@19: "artifacts.db"; sascha@19: sascha@87: /** sascha@87: * The default database URL: This is created once by #getDefaultURL() sascha@87: */ sascha@19: public static final String DEFAULT_URL = getDefaultURL(); sascha@10: sascha@87: /** sascha@87: * The default database user: "" sascha@87: */ sascha@10: public static final String DEFAULT_USER = ""; sascha@87: sascha@87: /** sascha@87: * The default database password: "" sascha@87: */ sascha@10: public static final String DEFAULT_PASSWORD = ""; sascha@10: sascha@10: private DBConnection() { sascha@10: } sascha@10: sascha@87: /** sascha@87: * Constructs the default databse URL. It concats the sascha@87: * config directory and the #DEFAULT_DATABASE_FILE sascha@87: * to the string with is needed to access H2 databases. sascha@89: * @return The default URL. sascha@87: */ sascha@19: public static final String getDefaultURL() { sascha@19: File configDir = Config.getConfigDirectory(); sascha@19: File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE); sascha@19: return "jdbc:h2:" + databaseFile; sascha@19: } sascha@19: sascha@10: private static BasicDataSource dataSource; sascha@10: sascha@10: private static final void addShutdownHook() { sascha@10: Runtime.getRuntime().addShutdownHook(new Thread() { sascha@87: @Override sascha@10: public void run() { sascha@10: if (dataSource != null) { sascha@10: try { sascha@10: dataSource.close(); sascha@10: } sascha@10: catch (SQLException sqle) { sascha@10: } sascha@10: dataSource = null; sascha@10: } sascha@10: } sascha@10: }); sascha@10: } sascha@10: sascha@87: /** sascha@87: * Static method to fetch a database connection. sascha@87: * @return a DataSource to access the database connection. sascha@87: */ sascha@10: public static synchronized DataSource getDataSource() { sascha@10: if (dataSource == null) { sascha@10: dataSource = new BasicDataSource(); sascha@10: sascha@10: String driver = Config.getStringXPath( sascha@10: DB_DRIVER, DEFAULT_DRIVER); sascha@10: sascha@10: String url = Config.getStringXPath( sascha@10: DB_URL, DEFAULT_URL); sascha@10: ingo@95: url = Config.replaceConfigDir(url); ingo@95: sascha@10: String user = Config.getStringXPath( sascha@10: DB_USER, DEFAULT_USER); sascha@10: sascha@10: String password = Config.getStringXPath( sascha@10: DB_PASSWORD, DEFAULT_PASSWORD); sascha@10: sascha@38: logger.info("database driver: " + driver); sascha@38: logger.info("database url: " + url); sascha@38: sascha@10: dataSource.setDriverClassName(driver); sascha@10: dataSource.setUsername(user); sascha@10: dataSource.setPassword(password); sascha@10: dataSource.setUrl(url); sascha@10: addShutdownHook(); sascha@10: } sascha@10: sascha@10: return dataSource; sascha@10: } sascha@10: } sascha@91: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :