view artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java @ 68:541814404822

Added interfaces for XML processing services. TODO: Implement them! artifacts/trunk@593 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 09:48:18 +0000
parents 93edc04f3a10
children 0f48188a6e02
line wrap: on
line source
package de.intevation.artifactdatabase;

import javax.sql.DataSource;

import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

import java.io.File;

import org.apache.log4j.Logger;

/**
 *  @author Sascha L. Teichmann
 */
public class DBConnection
{
    private static Logger logger = Logger.getLogger(DBConnection.class);

    public static final String DB_DRIVER =
        "/artifact-database/database/driver/text()";
    public static final String DB_URL =
        "/artifact-database/database/url/text()";
    public static final String DB_USER =
        "/artifact-database/database/user/text()";
    public static final String DB_PASSWORD =
        "/artifact-database/database/password/text()";

    public static final String DEFAULT_DRIVER =
        "org.h2.Driver";

    public static final String DEFAULT_DATABASE_FILE =
        "artifacts.db";

    public static final String DEFAULT_URL = getDefaultURL();

    public static final String DEFAULT_USER     = "";
    public static final String DEFAULT_PASSWORD = "";

    private DBConnection() {
    }

    public static final String getDefaultURL() {
        File configDir = Config.getConfigDirectory();
        File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE);
        return "jdbc:h2:" + databaseFile;
    }

    private static BasicDataSource dataSource;

    private static final void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (dataSource != null) {
                    try {
                        dataSource.close();
                    }
                    catch (SQLException sqle) {
                    }
                    dataSource = null;
                }
            }
        });
    }

    public static synchronized DataSource getDataSource() {
        if (dataSource == null) {
            dataSource = new BasicDataSource();

            String driver = Config.getStringXPath(
                DB_DRIVER, DEFAULT_DRIVER);

            String url = Config.getStringXPath(
                DB_URL, DEFAULT_URL);

            String user = Config.getStringXPath(
                DB_USER, DEFAULT_USER);

            String password = Config.getStringXPath(
                DB_PASSWORD, DEFAULT_PASSWORD);

            logger.info("database driver: " + driver);
            logger.info("database url: " + url);

            dataSource.setDriverClassName(driver);
            dataSource.setUsername(user);
            dataSource.setPassword(password);
            dataSource.setUrl(url);
            addShutdownHook();
        }

        return dataSource;
    }
}
//  vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org