changeset 176:3035d861a576

SessionFactoryProvider for access to the backend. flys-backend/trunk@1481 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 15 Mar 2011 17:29:11 +0000 (2011-03-15)
parents 7cdac4324609
children 31895d24387e
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java
diffstat 2 files changed, 137 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Tue Mar 15 16:31:14 2011 +0000
+++ b/flys-backend/ChangeLog	Tue Mar 15 17:29:11 2011 +0000
@@ -1,3 +1,9 @@
+2011-03-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  New. SessionFactoryProvider.getSessionFactory() provides a
+	  SessionFactory to use the Hibernate O/R mapper for the FLYS backend.
+
 2011-03-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* pom.xml: Added dependency to artifacts-commons to
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Tue Mar 15 17:29:11 2011 +0000
@@ -0,0 +1,131 @@
+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.NamedMainValues;
+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     = "flys";
+    public static final String DEFAULT_PASSWORD = "flys";
+    public static final String DEFAULT_DIALECT  =
+        "org.hibernate.dialect.PostgreSQLDialect";
+
+    public static final String DEFAULT_DRIVER =
+        "org.postgresql.Driver";
+
+    public static final String DEFAULT_URL =
+        "jdbc:postgresql://localhost:5432/flys";
+
+    private static SessionFactory sessionFactory;
+
+    private SessionFactoryProvider() {
+    }
+
+    public static synchronized SessionFactory getSessionFactory() {
+        if (sessionFactory == null) {
+            sessionFactory = createSessionFactory();
+        }
+        return sessionFactory;
+    }
+
+    private static SessionFactory createSessionFactory() {
+
+        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(NamedMainValues.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);
+
+        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);
+
+        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