aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java')
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java170
1 files changed, 170 insertions, 0 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java
new file mode 100644
index 000000000..a89ede528
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java
@@ -0,0 +1,170 @@
+package at.gv.egovernment.moa.id.commons.db;
+
+import java.util.Properties;
+
+import org.apache.commons.lang3.StringUtils;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.service.ServiceRegistry;
+import org.hibernate.service.ServiceRegistryBuilder;
+
+import at.gv.egovernment.moa.id.commons.db.dao.session.AssertionStore;
+import at.gv.egovernment.moa.id.commons.db.dao.session.AuthenticatedSessionStore;
+import at.gv.egovernment.moa.id.commons.db.dao.session.OASessionStore;
+import at.gv.egovernment.moa.id.commons.db.dao.session.OldSSOSessionIDStore;
+import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException;
+import at.gv.egovernment.moa.logging.Logger;
+
+public final class MOASessionDBUtils {
+
+ private static SessionFactory sessionFactory;
+ private static ServiceRegistry serviceRegistry;
+
+ @SuppressWarnings("rawtypes")
+ private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();
+ private static boolean automaticSessionHandling = false;
+
+ private static final String[] AUTOMATIC_SESSION_HANDLING_VALUES = new String[] { "jta", "thread" };
+ private static final String SESSION_HANDLING_KEY = "hibernate.current_session_context_class";
+
+ private static Configuration configuration;
+
+ protected MOASessionDBUtils() { }
+
+ public static void initHibernate(Configuration config, Properties hibernateProperties) {
+
+ String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY));
+ if (scm != null) {
+ automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1;
+ }
+ Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\".");
+ if (automaticSessionHandling) {
+ Logger.info("Hibernate is automatically handling session context management.");
+ } else {
+ Logger.info("Hibernate is NOT automatically handling session context management. Using build-in ThreadLocal session handling.");
+ }
+ try {
+ //Create the SessionFactory
+ Logger.debug("Creating initial session factory...");
+
+ config.configure();
+ serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
+ sessionFactory = config.buildSessionFactory(serviceRegistry);
+ Logger.debug("Initial session factory successfully created.");
+
+ } catch (Throwable ex) {
+ Logger.error("Initial session factory creation failed: " + ex.getMessage());
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ /**
+ * Checks if a session factory is currently available. If necessary a new
+ * session factory is created.
+ *
+ * @return current (or new) session factory
+ * @throws HibernateException
+ * thrown if a hibernate error occurs
+ */
+ public static Session getCurrentSession() {
+ if (automaticSessionHandling) {
+ return sessionFactory.getCurrentSession();
+ }
+ Session session = (Session) THREAD_LOCAL.get();
+ // Open a new Session, if this Thread has none yet
+ if (session == null || !session.isConnected()) {
+ session = getNewSession();
+ }
+ return session;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Session getNewSession() {
+ if (automaticSessionHandling) {
+ Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead.");
+ return sessionFactory.getCurrentSession();
+ }
+ Session session = (Session) THREAD_LOCAL.get();
+ if (session != null) {
+ Logger.warn("Previous session has not been closed; closing session now.");
+ closeSession();
+ }
+ Logger.debug("Opening new hibernate session...");
+ try {
+ session = sessionFactory.openSession();
+ THREAD_LOCAL.set(session);
+ } catch (HibernateException hex) {
+ Logger.error(hex.getMessage());
+ }
+ return session;
+ }
+
+ /**
+ * Closes the current session.
+ *
+ * @throws HibernateException
+ * thrown if session is already closed or a hibernate error
+ * occurs.
+ */
+ @SuppressWarnings("unchecked")
+ public static void closeSession() {
+ if (automaticSessionHandling) {
+ Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand.");
+ return;
+ }
+ Logger.debug("Closing current hibernate session...");
+ Session session = (Session) THREAD_LOCAL.get();
+ THREAD_LOCAL.set(null);
+ if (session != null) {
+ try {
+ session.close();
+
+ } catch (HibernateException hex) {
+ Logger.error(hex.getMessage());
+ }
+ }
+ }
+
+ public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException {
+ Transaction tx = null;
+ try {
+ Session session = MOASessionDBUtils.getCurrentSession();
+
+ synchronized (session) {
+ tx = session.beginTransaction();
+ session.saveOrUpdate(dbo);
+ tx.commit();
+ }
+ return true;
+
+ } catch(HibernateException e) {
+ Logger.warn("Error during database saveOrUpdate. Rollback.", e);
+ tx.rollback();
+ throw new MOADatabaseException(e);
+ }
+ }
+
+ public static boolean delete(Object dbo) {
+ Transaction tx = null;
+ try {
+ Session session = MOASessionDBUtils.getCurrentSession();
+
+ synchronized (session) {
+ tx = session.beginTransaction();
+ session.delete(dbo);
+ tx.commit();
+ }
+
+ return true;
+
+ } catch(HibernateException e) {
+ Logger.warn("Error during database delete. Rollback.", e);
+ tx.rollback();
+ return false;
+ }
+ }
+
+}