view artifact-database/src/main/java/de/intevation/artifactdatabase/SQL.java @ 294:b530e83acf2e

Adapted the signature of Hook.execute() which now retrieves a Document as third parameter. artifacts/trunk@2333 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 14 Jul 2011 14:41:00 +0000
parents b2115f484edb
children
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 de.intevation.artifacts.common.utils.Config;

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Singleton to provide SQL statement strings as key/value pairs.
 * This mechanism is used to encapsulate database specific SQL
 * dialects.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public final class SQL
{
    private static Logger logger = Logger.getLogger(SQL.class);

    private SQL() {
    }

    private static Properties statements;

    /**
     * Returns key/value pairs of SQL statements for the used database
     * backend.
     * The concrete set of SQL statements is determined by the
     * used JDBC database driver which is configured in conf.xml.
     * The class name of the driver is transformed by replacing
     * all '.' with '_' and lower case the resulting string.
     * The transformed string is used to load a properties file
     * in '/sql/' which should contain the statements.
     * Example:<br>
     * <code>org.postgresql.Driver</code> results in loading of
     * <code>/sql/org-postgresql-driver.properties</code>.
     * @return The key/value pairs of SQL statements.
     */
    public static final synchronized Properties getStatements() {
        if (statements == null) {
            statements = loadStatements();
        }
        return statements;
    }

    private static final Properties loadStatements() {
        String driver = Config.getStringXPath(
            DBConnection.DB_DRIVER, DBConnection.DEFAULT_DRIVER);

        Properties properties = new Properties();

        InputStream in = null;
        try {
            String res = "/sql/" + driver.replace('.', '-').toLowerCase()
                + ".properties";
            in = SQL.class.getResourceAsStream(res);

            if (in == null) {
                logger.warn("No SQL file for driver '" + driver + "' found.");
                res = "/sql/"
                    + DBConnection.DEFAULT_DRIVER.replace('.', '-')
                        .toLowerCase()
                    + ".properties";
                if ((in = SQL.class.getResourceAsStream(res)) == null) {
                    logger.error("No SQL file found");
                }
            }

            properties.load(in);
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage(), ioe);
        }
        finally {
            if (in != null) {
                try { in.close(); } catch (IOException ioe) {}
            }
        }

        return properties;
    }

    /**
     * Returns a particular SQL statement for a given key.
     * @param key The key of the statement.
     * @return The corresponing SQL statement or null if none
     * is found.
     */
    public static final String get(String key) {
        return getStatements().getProperty(key);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org