sascha@305: package de.intevation.artifactdatabase.db; sascha@305: sascha@305: import javax.sql.DataSource; sascha@305: sascha@305: import java.io.File; sascha@305: sascha@309: import org.apache.commons.pool.ObjectPool; sascha@309: sascha@309: import org.apache.commons.pool.impl.GenericObjectPool; sascha@309: sascha@309: import org.apache.commons.dbcp.DriverManagerConnectionFactory; sascha@309: import org.apache.commons.dbcp.PoolableConnectionFactory; sascha@309: import org.apache.commons.dbcp.PoolingDataSource; sascha@305: sascha@305: import de.intevation.artifacts.common.utils.Config; sascha@305: sascha@308: import org.apache.log4j.Logger; sascha@308: sascha@305: public class DBConnection sascha@305: { sascha@308: private static Logger log = Logger.getLogger(DBConnection.class); sascha@308: sascha@305: public static final String DEFAULT_DRIVER = "org.h2.Driver"; sascha@305: public static final String DEFAULT_USER = ""; sascha@305: public static final String DEFAULT_PASSWORD = ""; sascha@305: public static final String DEFAULT_DATABASE_FILE = "artifacts.db"; sascha@305: public static final String DEFAULT_URL = getDefaultURL(); sascha@305: sascha@305: public static final String getDefaultURL() { sascha@305: File configDir = Config.getConfigDirectory(); sascha@305: File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE); sascha@305: return "jdbc:h2:" + databaseFile; sascha@305: } sascha@305: sascha@309: protected DataSource dataSource; sascha@305: sascha@305: protected String driver; sascha@305: protected String url; sascha@305: protected String user; sascha@305: protected String password; sascha@305: sascha@305: public DBConnection() { sascha@305: } sascha@305: sascha@305: public DBConnection( sascha@305: String driver, sascha@305: String url, sascha@305: String user, sascha@305: String password sascha@305: ) { sascha@305: this.driver = driver; sascha@305: this.url = url; sascha@305: this.user = user; sascha@305: this.password = password; sascha@305: } sascha@305: sascha@305: public String getUser() { sascha@305: return user; sascha@305: } sascha@305: sascha@305: public void setUser(String user) { sascha@305: this.user = user; sascha@305: } sascha@305: sascha@305: public String getPassword() { sascha@305: return password; sascha@305: } sascha@305: sascha@305: public void setPassword(String password) { sascha@305: this.password = password; sascha@305: } sascha@305: sascha@305: public String getDriver() { sascha@305: return driver; sascha@305: } sascha@305: sascha@305: public void setDriver(String driver) { sascha@305: this.driver = driver; sascha@305: } sascha@305: sascha@305: public String getUrl() { sascha@305: return url; sascha@305: } sascha@305: sascha@305: public void setUrl(String url) { sascha@305: this.url = url; sascha@305: } sascha@305: sascha@305: public synchronized DataSource getDataSource() { sascha@305: if (dataSource == null) { sascha@308: if (log.isDebugEnabled()) { sascha@308: log.debug("create new datasource:"); sascha@308: log.debug(" driver: " + driver); sascha@308: log.debug(" url : " + url); sascha@308: log.debug(" user : " + user); sascha@308: } sascha@305: sascha@309: try { sascha@337: synchronized (DBConnection.class) { sascha@337: Class.forName(driver); sascha@337: } sascha@309: } sascha@309: catch (ClassNotFoundException cnfe) { sascha@309: log.error("cannot load driver", cnfe); sascha@309: return null; sascha@309: } sascha@309: sascha@309: DriverManagerConnectionFactory dmcf = sascha@309: new DriverManagerConnectionFactory(url, user, password); sascha@309: sascha@309: ObjectPool cp = new GenericObjectPool(); sascha@309: sascha@309: PoolableConnectionFactory pcf = new PoolableConnectionFactory( sascha@309: dmcf, cp, null, null, false, false); sascha@309: sascha@309: dataSource = new PoolingDataSource(cp); sascha@305: } sascha@305: return dataSource; sascha@305: } sascha@305: } sascha@305: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :