view flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 2844:c4db0f75a94a

Prepared the importer to import MINFO specific waterlevel values and added a stub for a parser. flys-backend/trunk@4287 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 24 Apr 2012 11:16:06 +0000
parents 0d55b7722fae
children dfc5bc5660c2
line wrap: on
line source
package de.intevation.flys.backend;

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

import java.lang.management.ManagementFactory;
import java.util.Properties;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MalformedObjectNameException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import org.hibernate.SessionFactory;

import org.hibernate.impl.SessionFactoryImpl;

import org.hibernate.jmx.StatisticsService;

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

import de.intevation.flys.model.Annotation;
import de.intevation.flys.model.AnnotationType;
import de.intevation.flys.model.Attribute;
import de.intevation.flys.model.BedHeightEpoch;
import de.intevation.flys.model.BedHeightEpochValue;
import de.intevation.flys.model.BedHeightSingle;
import de.intevation.flys.model.BedHeightSingleValue;
import de.intevation.flys.model.BedHeightType;
import de.intevation.flys.model.Building;
import de.intevation.flys.model.Catchment;
import de.intevation.flys.model.CrossSection;
import de.intevation.flys.model.CrossSectionLine;
import de.intevation.flys.model.CrossSectionPoint;
import de.intevation.flys.model.CrossSectionTrack;
import de.intevation.flys.model.Depth;
import de.intevation.flys.model.DGM;
import de.intevation.flys.model.DischargeTable;
import de.intevation.flys.model.DischargeTableValue;
import de.intevation.flys.model.DischargeZone;
import de.intevation.flys.model.Edge;
import de.intevation.flys.model.ElevationModel;
import de.intevation.flys.model.Fixpoint;
import de.intevation.flys.model.Floodmaps;
import de.intevation.flys.model.Floodplain;
import de.intevation.flys.model.FlowVelocityMeasurement;
import de.intevation.flys.model.FlowVelocityMeasurementValue;
import de.intevation.flys.model.FlowVelocityModel;
import de.intevation.flys.model.FlowVelocityModelValue;
import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.GrainFraction;
import de.intevation.flys.model.Hws;
import de.intevation.flys.model.HYK;
import de.intevation.flys.model.HYKEntry;
import de.intevation.flys.model.HYKFormation;
import de.intevation.flys.model.HYKFlowZoneType;
import de.intevation.flys.model.HYKFlowZone;
import de.intevation.flys.model.Line;
import de.intevation.flys.model.LocationSystem;
import de.intevation.flys.model.MainValueType;
import de.intevation.flys.model.MorphologicalWidth;
import de.intevation.flys.model.MorphologicalWidthValue;
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.RiverAxis;
import de.intevation.flys.model.RiverAxisKm;
import de.intevation.flys.model.SedimentDensity;
import de.intevation.flys.model.SedimentDensityValue;
import de.intevation.flys.model.SedimentYield;
import de.intevation.flys.model.SedimentYieldValue;
import de.intevation.flys.model.TimeInterval;
import de.intevation.flys.model.Unit;
import de.intevation.flys.model.Waterlevel;
import de.intevation.flys.model.WaterlevelQRange;
import de.intevation.flys.model.WaterlevelValue;
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");

    public static final boolean ENABLE_JMX =
        Boolean.getBoolean("flys.backend.enablejmx");

    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 = createConfiguration(
            user, password, dialect, driver, url);

        SessionFactory factory = cfg.buildSessionFactory();

        if (ENABLE_JMX) {
            registerAsMBean(factory);
        }
        else {
            log.info("No JMX support for hibernate.");
        }

        return factory;
    }


    public static void registerAsMBean(SessionFactory factory) {

        StatisticsService statsMBean = new StatisticsService();
        statsMBean.setSessionFactory(factory);
        statsMBean.setStatisticsEnabled(true);

        try {
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            mbs.registerMBean(
                statsMBean,
                new ObjectName("Hibernate:application=Statistics"));

            log.info("Enabled JMX support for hibernate.");
        }
        catch (MalformedObjectNameException mone) {
            log.warn(mone, mone);
        }
        catch (InstanceAlreadyExistsException iaee) {
            log.warn(iaee, iaee);
        }
        catch (MBeanRegistrationException mbre) {
            log.warn(mbre, mbre);
        }
        catch (NotCompliantMBeanException ncmbe) {
            log.warn(ncmbe, ncmbe);
        }
    }


    public static Configuration createConfiguration() {
        return createConfiguration(
            DEFAULT_USER,
            DEFAULT_PASSWORD,
            DEFAULT_DIALECT,
            DEFAULT_DRIVER,
            DEFAULT_URL);
    }

    public static Configuration createConfiguration(
        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(AnnotationType.class);
        cfg.addAnnotatedClass(Attribute.class);
        cfg.addAnnotatedClass(BedHeightEpoch.class);
        cfg.addAnnotatedClass(BedHeightEpochValue.class);
        cfg.addAnnotatedClass(BedHeightSingle.class);
        cfg.addAnnotatedClass(BedHeightSingleValue.class);
        cfg.addAnnotatedClass(BedHeightType.class);
        cfg.addAnnotatedClass(Building.class);
        cfg.addAnnotatedClass(Catchment.class);
        cfg.addAnnotatedClass(CrossSection.class);
        cfg.addAnnotatedClass(CrossSectionLine.class);
        cfg.addAnnotatedClass(CrossSectionPoint.class);
        cfg.addAnnotatedClass(CrossSectionTrack.class);
        cfg.addAnnotatedClass(Depth.class);
        cfg.addAnnotatedClass(DGM.class);
        cfg.addAnnotatedClass(DischargeTable.class);
        cfg.addAnnotatedClass(DischargeTableValue.class);
        cfg.addAnnotatedClass(DischargeZone.class);
        cfg.addAnnotatedClass(Edge.class);
        cfg.addAnnotatedClass(ElevationModel.class);
        cfg.addAnnotatedClass(Fixpoint.class);
        cfg.addAnnotatedClass(Floodplain.class);
        cfg.addAnnotatedClass(Floodmaps.class);
        cfg.addAnnotatedClass(FlowVelocityMeasurement.class);
        cfg.addAnnotatedClass(FlowVelocityMeasurementValue.class);
        cfg.addAnnotatedClass(FlowVelocityModel.class);
        cfg.addAnnotatedClass(FlowVelocityModelValue.class);
        cfg.addAnnotatedClass(Gauge.class);
        cfg.addAnnotatedClass(GrainFraction.class);
        cfg.addAnnotatedClass(Hws.class);
        cfg.addAnnotatedClass(HYK.class);
        cfg.addAnnotatedClass(HYKEntry.class);
        cfg.addAnnotatedClass(HYKFormation.class);
        cfg.addAnnotatedClass(HYKFlowZoneType.class);
        cfg.addAnnotatedClass(HYKFlowZone.class);
        cfg.addAnnotatedClass(Line.class);
        cfg.addAnnotatedClass(LocationSystem.class);
        cfg.addAnnotatedClass(MainValueType.class);
        cfg.addAnnotatedClass(MorphologicalWidth.class);
        cfg.addAnnotatedClass(MorphologicalWidthValue.class);
        cfg.addAnnotatedClass(NamedMainValue.class);
        cfg.addAnnotatedClass(MainValue.class);
        cfg.addAnnotatedClass(Position.class);
        cfg.addAnnotatedClass(Range.class);
        cfg.addAnnotatedClass(River.class);
        cfg.addAnnotatedClass(RiverAxis.class);
        cfg.addAnnotatedClass(RiverAxisKm.class);
        cfg.addAnnotatedClass(SedimentDensity.class);
        cfg.addAnnotatedClass(SedimentDensityValue.class);
        cfg.addAnnotatedClass(SedimentYield.class);
        cfg.addAnnotatedClass(SedimentYieldValue.class);
        cfg.addAnnotatedClass(TimeInterval.class);
        cfg.addAnnotatedClass(Unit.class);
        cfg.addAnnotatedClass(Waterlevel.class);
        cfg.addAnnotatedClass(WaterlevelQRange.class);
        cfg.addAnnotatedClass(WaterlevelValue.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;
    }


    public static String getProperty(SessionFactoryImpl factory, String key) {
        Properties props = factory.getProperties();
        return props.getProperty(key);
    }

    public static String getUser(SessionFactoryImpl factory) {
        return getProperty(factory, Environment.USER);
    }


    public static String getPass(SessionFactoryImpl factory) {
        return getProperty(factory, Environment.PASS);
    }


    public static String getURL(SessionFactoryImpl factory) {
        return getProperty(factory, Environment.URL);
    }


    public static String getDriver(SessionFactoryImpl factory) {
        return getProperty(factory, Environment.DRIVER);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org