sascha@10: package de.intevation.artifactdatabase; sascha@10: sascha@10: import javax.sql.DataSource; sascha@10: sascha@10: import java.sql.SQLException; sascha@10: sascha@10: import org.apache.commons.dbcp.BasicDataSource; sascha@10: sascha@10: /** sascha@10: * @author Sascha L. Teichmann sascha@10: */ sascha@10: public class DBConnection sascha@10: { sascha@10: public static final String DB_DRIVER = sascha@10: "/artifacts-database/database/driver/text()"; sascha@10: public static final String DB_URL = sascha@10: "/artifacts-database/database/url/text()"; sascha@10: public static final String DB_USER = sascha@12: "/artifacts-database/database/user/text()"; sascha@10: public static final String DB_PASSWORD = sascha@12: "/artifacts-database/database/password/text()"; sascha@10: sascha@10: public static final String DEFAULT_DRIVER = sascha@10: "org.h2.Driver"; sascha@10: sascha@10: public static final String DEFAULT_URL = sascha@10: "jdbc:h2:artifacts.db"; sascha@10: sascha@10: public static final String DEFAULT_USER = ""; sascha@10: public static final String DEFAULT_PASSWORD = ""; sascha@10: sascha@10: private DBConnection() { sascha@10: } sascha@10: sascha@10: private static BasicDataSource dataSource; sascha@10: sascha@10: private static final void addShutdownHook() { sascha@10: Runtime.getRuntime().addShutdownHook(new Thread() { 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@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: 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@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@10: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: