view flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents ee0c60757a94
children f939e1e6cfa4
line wrap: on
line source
package de.intevation.db;

import de.intevation.utils.XML;

import java.util.HashMap;

import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;

import org.apache.log4j.Logger;

public class ConnectionBuilder
{
    private static Logger log = Logger.getLogger(ConnectionBuilder.class);

    public static final String XPATH_DRIVER   = "/sync/side[@name=$type]/db/driver/text()";
    public static final String XPATH_USER     = "/sync/side[@name=$type]/db/user/text()";
    public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()";
    public static final String XPATH_URL      = "/sync/side[@name=$type]/db/url/text()";

    protected String type;
    protected String driver;
    protected String user;
    protected String password;
    protected String url;

    public ConnectionBuilder(String type, Document document) {
        this.type = type;
        extractCredentials(document);
    }

    protected void extractCredentials(Document document) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("type", type);

        driver = (String)XML.xpath(
            document, XPATH_DRIVER, XPathConstants.STRING, null, map);
        user = (String)XML.xpath(
            document, XPATH_USER, XPathConstants.STRING, null, map);
        password = (String)XML.xpath(
            document, XPATH_PASSWORD, XPathConstants.STRING, null, map);
        url = (String)XML.xpath(
            document, XPATH_URL, XPathConstants.STRING, null, map);

        if (log.isDebugEnabled()) {
            log.debug("driver: " + driver);
            log.debug("user: " + user);
            log.debug("password: *******");
            log.debug("url: " + url);
        }
    }

    public Connection getConnection() throws SQLException {

        if (driver != null && driver.length() > 0) {
            try {
                Class.forName(driver);
            }
            catch (ClassNotFoundException cnfe) {
                throw new SQLException(cnfe);
            }
        }

        Connection connection =
            DriverManager.getConnection(url, user, password);

        connection.setAutoCommit(false);

        DatabaseMetaData metaData = connection.getMetaData();

        if (metaData.supportsTransactionIsolationLevel(
            Connection.TRANSACTION_READ_UNCOMMITTED)) {
            connection.setTransactionIsolation(
                Connection.TRANSACTION_READ_UNCOMMITTED);
        }

        return connection;
    }

    public ConnectedStatements getConnectedStatements() throws SQLException {
        return new ConnectedStatements(
            getConnection(),
            new Statements(type, driver != null ? driver : "")
                .getStatements());
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org