changeset 3333:76c75c8b9ee0

SedDB: Make access to SedDB configurable over conf.xml and provide access to sessions. flys-backend/trunk@4666 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 14 Jun 2012 15:39:42 +0000
parents aa8ed778c8cc
children 2ae732e2c65c
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/App.java flys-backend/src/main/java/de/intevation/flys/backend/Credentials.java flys-backend/src/main/java/de/intevation/flys/backend/FLYSCredentials.java flys-backend/src/main/java/de/intevation/flys/backend/SedDBCredentials.java flys-backend/src/main/java/de/intevation/flys/backend/SedDBSessionHolder.java flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java flys-backend/src/main/java/de/intevation/flys/backend/SessionHolder.java
diffstat 8 files changed, 653 insertions(+), 242 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Jun 14 10:06:34 2012 +0000
+++ b/flys-backend/ChangeLog	Thu Jun 14 15:39:42 2012 +0000
@@ -1,3 +1,59 @@
+2012-06-14	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	 Make access to SedDB configurable over conf.xml and provide
+	 access to sessions.
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  Moved FLYS specific stuff out into the concept of credential classes
+	  which provides the user, password, dialect, driver, url and the
+	  Hibernate classes to bind.
+
+	* src/main/java/de/intevation/flys/backend/Credentials.java: New.
+	  Abstact class to provide the data needed for the SessionFactoryProvider.
+
+	* src/main/java/de/intevation/flys/backend/FLYSCredentials.java: New.
+	  Extends Credentials to serve the FLYS specific stuff.
+
+	* src/main/java/de/intevation/flys/backend/SedDBCredentials.java: New.
+	  Extends Credentials to serve the SedDB specific stuff. The parameters
+	  are drawn from conf.xml in the form:
+
+	    <artifact-database>
+	      ...
+	      <seddb-database>
+	        <user>USER</user>
+	        <password>PASSWORD</password>
+	        <driver>DRIVER</driver>
+	        <url>URL</url>
+	        <dialect>DIALECT</dialect>
+	      </seddb-database>
+	      ...
+	    </artifact-database>
+
+	    If absent defaults are:
+	    - user:     seddb
+	    - password: seddb
+	    - driver:   org.postgresql.Driver
+	    - url:      jdbc:postgresql://localhost:5432/seddb
+	    - dialect:  org.hibernate.dialect.PostgreSQLDialect
+
+	   Defaults can be overwritten with system properties:
+	   - user:      flys.seddb.user
+	   - password:  flys.seddb.password
+	   - driver:    flys.seddb.driver
+	   - url:       flys.seddb.url
+	   - dialect:   flys.seddb.dialect
+
+	* src/main/java/de/intevation/flys/backend/SedDBSessionHolder.java: New.
+	  Use this if you want to establish a session to the SedDB!
+	  Works like the SessionHolder.
+
+	* src/main/java/de/intevation/flys/backend/SessionHolder.java:
+	  Add Override annotation. This holder gives you a session 
+	  to the FLYS database.
+
+	* src/main/java/de/intevation/flys/App.java: Adjusted.
+
 2012-06-14	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/seddb/model/*.java: Hibernate access to
--- a/flys-backend/src/main/java/de/intevation/flys/App.java	Thu Jun 14 10:06:34 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/App.java	Thu Jun 14 15:39:42 2012 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys;
 
 import de.intevation.flys.backend.SessionFactoryProvider;
+import de.intevation.flys.backend.FLYSCredentials;
 
 import org.hibernate.cfg.Configuration;
 
@@ -13,7 +14,7 @@
 
         String [] setupScript = cfg.generateSchemaCreationScript(
             DialectFactory.constructDialect(
-                SessionFactoryProvider.DEFAULT_DIALECT));
+                FLYSCredentials.getDefault().getDialect()));
 
         for (String line: setupScript) {
             System.out.println(line + ";");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/Credentials.java	Thu Jun 14 15:39:42 2012 +0000
@@ -0,0 +1,79 @@
+package de.intevation.flys.backend;
+
+public abstract class Credentials
+{
+    protected String   user;
+    protected String   password;
+    protected String   dialect;
+    protected String   driver;
+    protected String   url;
+    protected Class [] classes;
+
+    public Credentials() {
+    }
+
+    public Credentials(
+        String   user,
+        String   password,
+        String   dialect,
+        String   driver,
+        String   url,
+        Class [] classes
+    ) {
+        this.user     = user;
+        this.password = password;
+        this.dialect  = dialect;
+        this.driver   = driver;
+        this.url      = url;
+        this.classes  = classes;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getDialect() {
+        return dialect;
+    }
+
+    public void setDialect(String dialect) {
+        this.dialect = dialect;
+    }
+
+    public String getDriver() {
+        return driver;
+    }
+
+    public void setDriver(String driver) {
+        this.driver = driver;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public Class [] getClasses() {
+        return classes;
+    }
+
+    public void setClasses(Class [] classes) {
+        this.classes = classes;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/FLYSCredentials.java	Thu Jun 14 15:39:42 2012 +0000
@@ -0,0 +1,231 @@
+package de.intevation.flys.backend;
+
+import de.intevation.artifacts.common.utils.Config;
+
+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.DGM;
+import de.intevation.flys.model.Depth;
+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.GaugeLocation;
+import de.intevation.flys.model.GrainFraction;
+import de.intevation.flys.model.HYK;
+import de.intevation.flys.model.HYKEntry;
+import de.intevation.flys.model.HYKFlowZone;
+import de.intevation.flys.model.HYKFlowZoneType;
+import de.intevation.flys.model.HYKFormation;
+import de.intevation.flys.model.Hws;
+import de.intevation.flys.model.HydrBoundary;
+import de.intevation.flys.model.HydrBoundaryPoly;
+import de.intevation.flys.model.Line;
+import de.intevation.flys.model.LocationSystem;
+import de.intevation.flys.model.MainValue;
+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.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.SQRelation;
+import de.intevation.flys.model.SQRelationValue;
+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.WaterlevelDifference;
+import de.intevation.flys.model.WaterlevelDifferenceColumn;
+import de.intevation.flys.model.WaterlevelDifferenceValue;
+import de.intevation.flys.model.WaterlevelQRange;
+import de.intevation.flys.model.WaterlevelValue;
+import de.intevation.flys.model.Wst;
+import de.intevation.flys.model.WstColumn;
+import de.intevation.flys.model.WstColumnQRange;
+import de.intevation.flys.model.WstColumnValue;
+import de.intevation.flys.model.WstQRange;
+
+public class FLYSCredentials
+extends      Credentials
+{
+    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 Class [] CLASSES = {
+        Annotation.class,
+        AnnotationType.class,
+        Attribute.class,
+        BedHeightEpoch.class,
+        BedHeightEpochValue.class,
+        BedHeightSingle.class,
+        BedHeightSingleValue.class,
+        BedHeightType.class,
+        Building.class,
+        Catchment.class,
+        CrossSection.class,
+        CrossSectionLine.class,
+        CrossSectionPoint.class,
+        CrossSectionTrack.class,
+        Depth.class,
+        DGM.class,
+        DischargeTable.class,
+        DischargeTableValue.class,
+        DischargeZone.class,
+        Edge.class,
+        ElevationModel.class,
+        Fixpoint.class,
+        Floodplain.class,
+        Floodmaps.class,
+        FlowVelocityMeasurement.class,
+        FlowVelocityMeasurementValue.class,
+        FlowVelocityModel.class,
+        FlowVelocityModelValue.class,
+        Gauge.class,
+        GaugeLocation.class,
+        GrainFraction.class,
+        Hws.class,
+        HydrBoundary.class,
+        HydrBoundaryPoly.class,
+        HYK.class,
+        HYKEntry.class,
+        HYKFormation.class,
+        HYKFlowZoneType.class,
+        HYKFlowZone.class,
+        Line.class,
+        LocationSystem.class,
+        MainValueType.class,
+        MorphologicalWidth.class,
+        MorphologicalWidthValue.class,
+        NamedMainValue.class,
+        MainValue.class,
+        Position.class,
+        Range.class,
+        River.class,
+        RiverAxis.class,
+        RiverAxisKm.class,
+        SedimentDensity.class,
+        SedimentDensityValue.class,
+        SedimentYield.class,
+        SedimentYieldValue.class,
+        SQRelation.class,
+        SQRelationValue.class,
+        TimeInterval.class,
+        Unit.class,
+        Waterlevel.class,
+        WaterlevelDifference.class,
+        WaterlevelDifferenceColumn.class,
+        WaterlevelDifferenceValue.class,
+        WaterlevelQRange.class,
+        WaterlevelValue.class,
+        WstColumn.class,
+        WstColumnQRange.class,
+        WstColumnValue.class,
+        Wst.class,
+        WstQRange.class
+    };
+
+    public FLYSCredentials() {
+    }
+
+    public FLYSCredentials(
+        String user,
+        String password,
+        String dialect,
+        String driver,
+        String url
+    ) {
+        super(user, password, dialect, driver, url, CLASSES);
+    }
+
+    private static Credentials instance;
+
+    public static synchronized Credentials getInstance() {
+        if (instance == 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);
+
+            instance = new FLYSCredentials(
+                user, password, dialect, driver, url);
+        }
+        return instance;
+    }
+
+    public static Credentials getDefault() {
+        return new FLYSCredentials(
+            DEFAULT_USER,
+            DEFAULT_PASSWORD,
+            DEFAULT_DIALECT,
+            DEFAULT_DRIVER,
+            DEFAULT_URL);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SedDBCredentials.java	Thu Jun 14 15:39:42 2012 +0000
@@ -0,0 +1,195 @@
+package de.intevation.flys.backend;
+
+import de.intevation.artifacts.common.utils.Config;
+
+import de.intevation.seddb.model.Bezugspegel;
+import de.intevation.seddb.model.Bezugspegelgew;
+import de.intevation.seddb.model.BezugspegelgewId;
+import de.intevation.seddb.model.Bild;
+import de.intevation.seddb.model.Gewaesser;
+import de.intevation.seddb.model.Gfaenger;
+import de.intevation.seddb.model.Glotlinks;
+import de.intevation.seddb.model.GlotlinksId;
+import de.intevation.seddb.model.Glotrechte;
+import de.intevation.seddb.model.Gprobe;
+import de.intevation.seddb.model.GprobeId;
+import de.intevation.seddb.model.Gsiebsatz;
+import de.intevation.seddb.model.Gsiebung;
+import de.intevation.seddb.model.Gsiebungsieb;
+import de.intevation.seddb.model.GsiebungsiebId;
+import de.intevation.seddb.model.Hpeilpunkt;
+import de.intevation.seddb.model.HpeilpunktId;
+import de.intevation.seddb.model.Hpeilung;
+import de.intevation.seddb.model.Messung;
+import de.intevation.seddb.model.Messunglotcount;
+import de.intevation.seddb.model.MessunglotcountId;
+import de.intevation.seddb.model.Messungsta;
+import de.intevation.seddb.model.MessungstaId;
+import de.intevation.seddb.model.Messunguferbezug;
+import de.intevation.seddb.model.MessunguferbezugId;
+import de.intevation.seddb.model.Mpeilpunkt;
+import de.intevation.seddb.model.MpeilpunktId;
+import de.intevation.seddb.model.Mpeilung;
+import de.intevation.seddb.model.Probebild;
+import de.intevation.seddb.model.Siebanalyse;
+import de.intevation.seddb.model.Slotlinks;
+import de.intevation.seddb.model.SlotlinksId;
+import de.intevation.seddb.model.Slotrechte;
+import de.intevation.seddb.model.Sohlprobe;
+import de.intevation.seddb.model.Sohltest;
+import de.intevation.seddb.model.Sprobe;
+import de.intevation.seddb.model.SprobeId;
+import de.intevation.seddb.model.Ssiebung;
+import de.intevation.seddb.model.Ssiebungsieb;
+import de.intevation.seddb.model.SsiebungsiebId;
+import de.intevation.seddb.model.Station;
+import de.intevation.seddb.model.Stationgew;
+import de.intevation.seddb.model.StationgewId;
+import de.intevation.seddb.model.TmpGloChanged;
+import de.intevation.seddb.model.TmpMesAchanged;
+import de.intevation.seddb.model.TmpMesGchanged;
+import de.intevation.seddb.model.TmpMesQchanged;
+import de.intevation.seddb.model.TmpMesSchanged;
+import de.intevation.seddb.model.Zzarchiv;
+import de.intevation.seddb.model.Zzprobenahmeart;
+import de.intevation.seddb.model.Zzsondierungart;
+import de.intevation.seddb.model.Zzthema;
+
+public class SedDBCredentials
+extends      Credentials
+{
+    public static final String XPATH_USER =
+        "/artifact-database/seddb-database/user/text()";
+
+    public static final String XPATH_PASSWORD =
+        "/artifact-database/seddb-database/password/text()";
+
+    public static final String XPATH_DIALECT =
+        "/artifact-database/seddb-database/dialect/text()";
+
+    public static final String XPATH_DRIVER =
+        "/artifact-database/seddb-database/driver/text()";
+
+    public static final String XPATH_URL =
+        "/artifact-database/seddb-database/url/text()";
+
+    public static final String DEFAULT_USER =
+        System.getProperty("flys.seddb.user", "seddb");
+
+    public static final String DEFAULT_PASSWORD =
+        System.getProperty("flys.seddb.password", "seddb");
+
+    public static final String DEFAULT_DIALECT =
+        System.getProperty(
+            "flys.seddb.dialect",
+            "org.hibernate.dialect.PostgreSQLDialect");
+
+    public static final String DEFAULT_DRIVER =
+        System.getProperty(
+            "flys.seddb.driver",
+            "org.postgresql.Driver");
+
+    public static final String DEFAULT_URL =
+        System.getProperty(
+            "flys.seddb.url",
+            "jdbc:postgresql://localhost:5432/seddb");
+
+    public static final Class [] CLASSES = {
+        BezugspegelgewId.class,
+        Bezugspegelgew.class,
+        Bezugspegel.class,
+        Bild.class,
+        Gewaesser.class,
+        Gfaenger.class,
+        GlotlinksId.class,
+        Glotlinks.class,
+        Glotrechte.class,
+        GprobeId.class,
+        Gprobe.class,
+        Gsiebsatz.class,
+        Gsiebung.class,
+        GsiebungsiebId.class,
+        Gsiebungsieb.class,
+        HpeilpunktId.class,
+        Hpeilpunkt.class,
+        Hpeilung.class,
+        Messung.class,
+        MessunglotcountId.class,
+        Messunglotcount.class,
+        MessungstaId.class,
+        Messungsta.class,
+        MessunguferbezugId.class,
+        Messunguferbezug.class,
+        MpeilpunktId.class,
+        Mpeilpunkt.class,
+        Mpeilung.class,
+        Probebild.class,
+        Siebanalyse.class,
+        SlotlinksId.class,
+        Slotlinks.class,
+        Slotrechte.class,
+        Sohlprobe.class,
+        Sohltest.class,
+        SprobeId.class,
+        Sprobe.class,
+        Ssiebung.class,
+        SsiebungsiebId.class,
+        Ssiebungsieb.class,
+        StationgewId.class,
+        Stationgew.class,
+        Station.class,
+        TmpGloChanged.class,
+        TmpMesAchanged.class,
+        TmpMesGchanged.class,
+        TmpMesQchanged.class,
+        TmpMesSchanged.class,
+        Zzarchiv.class,
+        Zzprobenahmeart.class,
+        Zzsondierungart.class,
+        Zzthema.class
+    };
+
+    private static Credentials instance;
+
+    public SedDBCredentials() {
+    }
+
+    public SedDBCredentials(
+        String user,
+        String password,
+        String dialect,
+        String driver,
+        String url
+    ) {
+        super(user, password, dialect, driver, url, CLASSES);
+    }
+
+    public static synchronized Credentials getInstance() {
+        if (instance == 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);
+
+            instance = new SedDBCredentials(
+                user, password, dialect, driver, url);
+        }
+        return instance;
+    }
+
+    public static Credentials getDefault() {
+        return new SedDBCredentials(
+            DEFAULT_USER,
+            DEFAULT_PASSWORD,
+            DEFAULT_DIALECT,
+            DEFAULT_DRIVER,
+            DEFAULT_URL);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SedDBSessionHolder.java	Thu Jun 14 15:39:42 2012 +0000
@@ -0,0 +1,43 @@
+package de.intevation.flys.backend;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+public class SedDBSessionHolder
+{
+    private static Logger log =
+        Logger.getLogger(SedDBSessionHolder.class);
+
+    public static final ThreadLocal<Session> HOLDER =
+        new ThreadLocal<Session>() {
+            @Override
+            protected Session initialValue() {
+                return create();
+            }
+        };
+
+    private SedDBSessionHolder() {
+    }
+
+    public synchronized static Session create() {
+        log.debug("create");
+        SessionFactory sessionFactory =
+            SessionFactoryProvider.getSedDBSessionFactory();
+        return sessionFactory.openSession();
+    }
+
+    public static Session acquire() {
+        log.debug("acquire");
+        Session session = create();
+        HOLDER.set(session);
+        return session;
+    }
+
+    public static void release() {
+        log.debug("release");
+        HOLDER.remove();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Thu Jun 14 10:06:34 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Thu Jun 14 15:39:42 2012 +0000
@@ -1,184 +1,68 @@
 package de.intevation.flys.backend;
 
-import de.intevation.artifacts.common.utils.Config;
+import java.lang.management.ManagementFactory;
 
-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.MalformedObjectNameException;
 import javax.management.NotCompliantMBeanException;
 import javax.management.ObjectName;
 
+import org.apache.log4j.Logger;
+
 import org.hibernate.SessionFactory;
 
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+
 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.GaugeLocation;
-import de.intevation.flys.model.GrainFraction;
-import de.intevation.flys.model.Hws;
-import de.intevation.flys.model.HydrBoundary;
-import de.intevation.flys.model.HydrBoundaryPoly;
-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.SQRelation;
-import de.intevation.flys.model.SQRelationValue;
-import de.intevation.flys.model.TimeInterval;
-import de.intevation.flys.model.Unit;
-import de.intevation.flys.model.Waterlevel;
-import de.intevation.flys.model.WaterlevelDifference;
-import de.intevation.flys.model.WaterlevelDifferenceColumn;
-import de.intevation.flys.model.WaterlevelDifferenceValue;
-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 static SessionFactory flysSessionFactory;
+    private static SessionFactory sedDBSessionFactory;
 
     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);
+        if (flysSessionFactory == null) {
+            flysSessionFactory =
+                createSessionFactory(FLYSCredentials.getInstance());
         }
-        return sessionFactory;
+        return flysSessionFactory;
     }
 
     public static SessionFactory createSessionFactory() {
-        return createSessionFactory(
-            DEFAULT_USER,
-            DEFAULT_PASSWORD,
-            DEFAULT_DIALECT,
-            DEFAULT_DRIVER,
-            DEFAULT_URL);
+        return createSessionFactory(FLYSCredentials.getDefault());
+    }
+
+    public static synchronized SessionFactory getSedDBSessionFactory() {
+        if (sedDBSessionFactory == null) {
+            sedDBSessionFactory =
+                createSessionFactory(SedDBCredentials.getInstance());
+        }
+        return sedDBSessionFactory;
+    }
+
+    public static SessionFactory createSedDBSessionFactory() {
+        return createSessionFactory(SedDBCredentials.getDefault());
     }
 
     public static SessionFactory createSessionFactory(
-        String user,
-        String password,
-        String dialect,
-        String driver,
-        String url
+        Credentials credentials
     ) {
-        Configuration cfg = createConfiguration(
-            user, password, dialect, driver, url);
+        Configuration cfg = createConfiguration(credentials);
 
         SessionFactory factory = cfg.buildSessionFactory();
 
@@ -192,7 +76,6 @@
         return factory;
     }
 
-
     public static void registerAsMBean(SessionFactory factory) {
 
         StatisticsService statsMBean = new StatisticsService();
@@ -221,102 +104,24 @@
         }
     }
 
-
     public static Configuration createConfiguration() {
-        return createConfiguration(
-            DEFAULT_USER,
-            DEFAULT_PASSWORD,
-            DEFAULT_DIALECT,
-            DEFAULT_DRIVER,
-            DEFAULT_URL);
+        return createConfiguration(FLYSCredentials.getInstance());
     }
 
     public static Configuration createConfiguration(
-        String user,
-        String password,
-        String dialect,
-        String driver,
-        String url
+        Credentials credentials
     ) {
         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(GaugeLocation.class);
-        cfg.addAnnotatedClass(GrainFraction.class);
-        cfg.addAnnotatedClass(Hws.class);
-        cfg.addAnnotatedClass(HydrBoundary.class);
-        cfg.addAnnotatedClass(HydrBoundaryPoly.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(SQRelation.class);
-        cfg.addAnnotatedClass(SQRelationValue.class);
-        cfg.addAnnotatedClass(TimeInterval.class);
-        cfg.addAnnotatedClass(Unit.class);
-        cfg.addAnnotatedClass(Waterlevel.class);
-        cfg.addAnnotatedClass(WaterlevelDifference.class);
-        cfg.addAnnotatedClass(WaterlevelDifferenceColumn.class);
-        cfg.addAnnotatedClass(WaterlevelDifferenceValue.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);
+        for (Class clazz: credentials.getClasses()) {
+            cfg.addAnnotatedClass(clazz);
+        }
 
         if (log.isDebugEnabled()) {
-            log.debug("user: "    + user);
-            log.debug("dialect: " + dialect);
-            log.debug("driver: "  + driver);
-            log.debug("url: "     + url);
+            log.debug("user: "    + credentials.getUser());
+            log.debug("dialect: " + credentials.getDialect());
+            log.debug("driver: "  + credentials.getDriver());
+            log.debug("url: "     + credentials.getUrl());
         }
 
         Properties props = new Properties();
@@ -326,11 +131,11 @@
             "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);
+        props.setProperty(Environment.DIALECT, credentials.getDialect());
+        props.setProperty(Environment.USER,    credentials.getUser());
+        props.setProperty(Environment.PASS,    credentials.getPassword());
+        props.setProperty(Environment.DRIVER,  credentials.getDriver());
+        props.setProperty(Environment.URL,     credentials.getUrl());
 
         cfg.mergeProperties(props);
 
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionHolder.java	Thu Jun 14 10:06:34 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionHolder.java	Thu Jun 14 15:39:42 2012 +0000
@@ -8,11 +8,12 @@
 
 public class SessionHolder
 {
-    private static Logger logger =
+    private static Logger log =
         Logger.getLogger(SessionHolder.class);
 
     public static final ThreadLocal<Session> HOLDER =
         new ThreadLocal<Session>() {
+            @Override
             protected Session initialValue() {
                 return create();
             }
@@ -22,21 +23,21 @@
     }
 
     public synchronized static Session create() {
-        logger.debug("create");
+        log.debug("create");
         SessionFactory sessionFactory =
             SessionFactoryProvider.getSessionFactory();
         return sessionFactory.openSession();
     }
 
     public static Session acquire() {
-        logger.debug("acquire");
+        log.debug("acquire");
         Session session = create();
         HOLDER.set(session);
         return session;
     }
 
     public static void release() {
-        logger.debug("release");
+        log.debug("release");
         HOLDER.remove();
     }
 }

http://dive4elements.wald.intevation.org