view flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 499:cce054f27dac

Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default. flys-backend/trunk@1853 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 21:34:43 +0000
parents d37ccb04ab5d
children c8a2fbc612be
line wrap: on
line source
package de.intevation.flys.backend;

import de.intevation.artifacts.common.utils.Config;

import java.util.Properties;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import de.intevation.flys.model.Annotation;
import de.intevation.flys.model.Attribute;
import de.intevation.flys.model.DischargeTable;
import de.intevation.flys.model.DischargeTableValue;
import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.MainValueType;
import de.intevation.flys.model.NamedMainValue;
import de.intevation.flys.model.MainValue;
import de.intevation.flys.model.Position;
import de.intevation.flys.model.Range;
import de.intevation.flys.model.River;
import de.intevation.flys.model.TimeInterval;
import de.intevation.flys.model.WstColumn;
import de.intevation.flys.model.WstColumnQRange;
import de.intevation.flys.model.WstColumnValue;
import de.intevation.flys.model.Wst;
import de.intevation.flys.model.WstQRange;

import org.apache.log4j.Logger;

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

    public static final String XPATH_USER =
        "/artifact-database/backend-database/user/text()";

    public static final String XPATH_PASSWORD =
        "/artifact-database/backend-database/password/text()";

    public static final String XPATH_DIALECT =
        "/artifact-database/backend-database/dialect/text()";

    public static final String XPATH_DRIVER =
        "/artifact-database/backend-database/driver/text()";

    public static final String XPATH_URL =
        "/artifact-database/backend-database/url/text()";

    public static final String DEFAULT_USER =
        System.getProperty("flys.backend.user", "flys");

    public static final String DEFAULT_PASSWORD =
        System.getProperty("flys.backend.password", "flys");

    public static final String DEFAULT_DIALECT =
        System.getProperty(
            "flys.backend.dialect",
            "org.hibernate.dialect.PostgreSQLDialect");

    public static final String DEFAULT_DRIVER =
        System.getProperty(
            "flys.backend.driver",
            "org.postgresql.Driver");

    public static final String DEFAULT_URL =
        System.getProperty(
            "flys.backend.url",
            "jdbc:postgresql://localhost:5432/flys");

    private static SessionFactory sessionFactory;

    private SessionFactoryProvider() {
    }

    public static synchronized SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            String user =
                Config.getStringXPath(XPATH_USER, DEFAULT_USER);
            String password =
                Config.getStringXPath(XPATH_PASSWORD, DEFAULT_PASSWORD);
            String dialect =
                Config.getStringXPath(XPATH_DIALECT, DEFAULT_DIALECT);
            String driver =
                Config.getStringXPath(XPATH_DRIVER, DEFAULT_DRIVER);
            String url =
                Config.getStringXPath(XPATH_URL, DEFAULT_URL);

            sessionFactory = createSessionFactory(
                user, password, dialect, driver, url);
        }
        return sessionFactory;
    }

    public static SessionFactory createSessionFactory() {
        return createSessionFactory(
            DEFAULT_USER,
            DEFAULT_PASSWORD,
            DEFAULT_DIALECT,
            DEFAULT_DRIVER,
            DEFAULT_URL);
    }

    public static SessionFactory createSessionFactory(
        String user,
        String password,
        String dialect,
        String driver,
        String url
    ) {
        Configuration cfg = new Configuration();

        // TODO: Use package reflection here.
        cfg.addAnnotatedClass(Annotation.class);
        cfg.addAnnotatedClass(Attribute.class);
        cfg.addAnnotatedClass(DischargeTable.class);
        cfg.addAnnotatedClass(DischargeTableValue.class);
        cfg.addAnnotatedClass(Gauge.class);
        cfg.addAnnotatedClass(MainValueType.class);
        cfg.addAnnotatedClass(NamedMainValue.class);
        cfg.addAnnotatedClass(MainValue.class);
        cfg.addAnnotatedClass(Position.class);
        cfg.addAnnotatedClass(Range.class);
        cfg.addAnnotatedClass(River.class);
        cfg.addAnnotatedClass(TimeInterval.class);
        cfg.addAnnotatedClass(WstColumn.class);
        cfg.addAnnotatedClass(WstColumnQRange.class);
        cfg.addAnnotatedClass(WstColumnValue.class);
        cfg.addAnnotatedClass(Wst.class);
        cfg.addAnnotatedClass(WstQRange.class);

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

        Properties props = new Properties();

        // We rely on our own connection pool
        props.setProperty(
            "hibernate.connection.provider_class",
            "org.hibernate.connection.DBCPConnectionProvider");

        props.setProperty(Environment.DIALECT, dialect);
        props.setProperty(Environment.USER,    user);
        props.setProperty(Environment.PASS,    password);
        props.setProperty(Environment.DRIVER,  driver);
        props.setProperty(Environment.URL,     url);

        cfg.mergeProperties(props);

        return cfg.buildSessionFactory();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org