Mercurial > dive4elements > framework
view artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java @ 168:f70977cf2faf
Avoid Nullpointer exception in listCollections() when no creation time of the collection is returned.
artifacts/trunk@1393 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 04 Mar 2011 09:13:18 +0000 |
parents | 933bbc9fc11f |
children | b2115f484edb |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package de.intevation.artifactdatabase; import java.io.File; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.log4j.Logger; /** * This class encapsulate the creation and pooling of database connections used * by the artifact database. The credential to open the database connections * are taken from the global configuratiion. * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> */ public class DBConnection { private static Logger logger = Logger.getLogger(DBConnection.class); /** * XPath to access the database driver within the global configuration. */ public static final String DB_DRIVER = "/artifact-database/database/driver/text()"; /** * XPath to access the database URL within the global configuration. */ public static final String DB_URL = "/artifact-database/database/url/text()"; /** * XPath to access the database use within the global configuration. */ public static final String DB_USER = "/artifact-database/database/user/text()"; /** * XPath to access the database password within the global configuration. */ public static final String DB_PASSWORD = "/artifact-database/database/password/text()"; /** * The default database driver: H2 */ public static final String DEFAULT_DRIVER = "org.h2.Driver"; /** * The default database name: artifacts.db */ public static final String DEFAULT_DATABASE_FILE = "artifacts.db"; /** * The default database URL: This is created once by #getDefaultURL() */ public static final String DEFAULT_URL = getDefaultURL(); /** * The default database user: "" */ public static final String DEFAULT_USER = ""; /** * The default database password: "" */ public static final String DEFAULT_PASSWORD = ""; private DBConnection() { } /** * Constructs the default databse URL. It concats the * config directory and the #DEFAULT_DATABASE_FILE * to the string with is needed to access H2 databases. * @return The default URL. */ 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() { @Override public void run() { if (dataSource != null) { try { dataSource.close(); } catch (SQLException sqle) { } dataSource = null; } } }); } /** * Static method to fetch a database connection. * @return a DataSource to access the database connection. */ 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); url = Config.replaceConfigDir(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 :