diff artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java @ 305:f33401ea2a6c

Artifact database: Refactorized the usage of dialect independent SQL to be reusable. artifacts/trunk@2412 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 10:19:35 +0000
parents
children a077bb098eb4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java	Thu Jul 28 10:19:35 2011 +0000
@@ -0,0 +1,110 @@
+package de.intevation.artifactdatabase.db;
+
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+import java.io.File;
+
+import org.apache.commons.dbcp.BasicDataSource;
+
+import de.intevation.artifacts.common.utils.Config;
+
+public class DBConnection
+{
+    public static final String DEFAULT_DRIVER        = "org.h2.Driver";
+    public static final String DEFAULT_USER          = "";
+    public static final String DEFAULT_PASSWORD      = "";
+    public static final String DEFAULT_DATABASE_FILE = "artifacts.db";
+    public static final String DEFAULT_URL           = getDefaultURL();
+
+    public static final String getDefaultURL() {
+        File configDir = Config.getConfigDirectory();
+        File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE);
+        return "jdbc:h2:" + databaseFile;
+    }
+
+    protected BasicDataSource dataSource;
+
+    protected String driver;
+    protected String url;
+    protected String user;
+    protected String password;
+
+    public DBConnection() {
+    }
+
+    public DBConnection(
+        String driver,
+        String url,
+        String user,
+        String password
+    ) {
+        this.driver   = driver;
+        this.url      = url;
+        this.user     = user;
+        this.password = password;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getDriver() {
+        return driver;
+    }
+
+    public void setDriver(String driver) {
+        this.driver = driver;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    protected void addShutdownHook() {
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                if (dataSource != null) {
+                    try {
+                        dataSource.close();
+                    }
+                    catch (SQLException sqle) {
+                    }
+                    dataSource = null;
+                }
+            }
+        });
+    }
+
+    public synchronized DataSource getDataSource() {
+        if (dataSource == null) {
+            dataSource = new BasicDataSource();
+
+            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