diff options
author | Thomas Lenz <tlenz@iaik.tugraz.at> | 2013-07-18 12:01:21 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2013-07-18 12:01:21 +0200 |
commit | 49acb697426d3c313ad047449ea62ac1bf3f4fd0 (patch) | |
tree | 0e25cdfdb8b4b714655b6a16e5dd73bd81fcbea9 /id/server/moa-id-commons/src/main/java/at | |
parent | 5c345b6a0cd34a8c853aa274b081e70589b44da3 (diff) | |
download | moa-id-spss-49acb697426d3c313ad047449ea62ac1bf3f4fd0.tar.gz moa-id-spss-49acb697426d3c313ad047449ea62ac1bf3f4fd0.tar.bz2 moa-id-spss-49acb697426d3c313ad047449ea62ac1bf3f4fd0.zip |
MOA-ID 2.x Configuration implemented
-SSO deaktivated
-Login with mandate and normal tested
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at')
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java | 64 | ||||
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java | 217 | ||||
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationUtil.java | 201 | ||||
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java (renamed from id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionUtil.java) | 8 | ||||
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java | 2 |
5 files changed, 257 insertions, 235 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java new file mode 100644 index 000000000..8970a82aa --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java @@ -0,0 +1,64 @@ +package at.gv.egovernment.moa.id.commons.db; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; + +import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; +import at.gv.egovernment.moa.id.commons.db.dao.config.OnlineApplication; + +import at.gv.egovernment.moa.logging.Logger; +import at.gv.egovernment.moa.util.MiscUtil; + +public class ConfigurationDBRead { + + private static Map<String, String> QUERIES = new HashMap<String, String>(); + static { + QUERIES.put("getOnlineApplicationWithID", "select onlineapplication from OnlineApplication onlineapplication where onlineapplication.publicURLPrefix like SUBSTRING(:id, 1, LENGTH(onlineapplication.publicURLPrefix))"); + QUERIES.put("getMOAIDConfiguration", "select moaidconfiguration from MOAIDConfiguration moaidconfiguration"); + } + + @SuppressWarnings("rawtypes") + public static OnlineApplication getOnlineApplication(String id) { + MiscUtil.assertNotNull(id, "OnlineApplictionID"); + Logger.trace("Getting OnlineApplication with ID " + id + " from database."); + + List result; + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + + javax.persistence.Query query = session.createQuery(QUERIES.get("getOnlineApplicationWithID")); + //query.setParameter("id", id+"%"); + query.setParameter("id", id); + result = query.getResultList(); + + Logger.trace("Found entries: " + result.size()); + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return (OnlineApplication) result.get(0); + } + + public static MOAIDConfiguration getMOAIDConfiguration() { + Logger.trace("Load MOAID Configuration from database."); + + List result; + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + + javax.persistence.Query query = session.createQuery(QUERIES.get("getMOAIDConfiguration")); + result = query.getResultList(); + + Logger.trace("Found entries: " + result.size()); + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return (MOAIDConfiguration) result.get(0); + } + + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java index 183fcda65..bc6a0b922 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java @@ -1,42 +1,201 @@ package at.gv.egovernment.moa.id.commons.db; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.Properties; import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; -import at.gv.egovernment.moa.id.commons.db.dao.config.OnlineApplication; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; import at.gv.egovernment.moa.logging.Logger; -import at.gv.egovernment.moa.util.MiscUtil; -public class ConfigurationDBUtils { +public final class ConfigurationDBUtils { - private static Map<String, String> QUERIES = new HashMap<String, String>(); - static { - QUERIES.put("getOnlineApplicationWithID", "select onlineapplication from OnlineApplication onlineapplication where onlineapplication.publicURLPrefix LIKE :id"); + private static EntityManagerFactory entitymanagerfactory; + + @SuppressWarnings("rawtypes") + private static final ThreadLocal THREAD_LOCAL = new ThreadLocal(); + private static boolean automaticSessionHandling = false; + + protected ConfigurationDBUtils() { } + + public static void initHibernate(Properties props) { + + try { + + //add Hibernate annotations +// Configuration hibernateConfig = new Configuration(); +// hibernateConfig.addAnnotatedClass(AssertionStore.class); +// hibernateConfig.addAnnotatedClass(AuthenticatedSessionStore.class); +// hibernateConfig.addAnnotatedClass(OASessionStore.class); +// hibernateConfig.addAnnotatedClass(OldSSOSessionIDStore.class); +// hibernateConfig.addProperties(props); + + + Logger.debug("Creating initial session factory..."); +// entitymanagerfactory = +// Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", +// hibernateConfig.getProperties()); + + entitymanagerfactory = + Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", + props); + + 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 EntityManager getCurrentSession() { + if (automaticSessionHandling) { + + return entitymanagerfactory.createEntityManager(); } - @SuppressWarnings("rawtypes") - public static OnlineApplication getOnlineApplication(String id) { - MiscUtil.assertNotNull(id, "OnlineApplictionID"); - Logger.trace("Getting OnlineApplication with ID " + id + " from database."); + EntityManager session = (EntityManager) THREAD_LOCAL.get(); + // Open a new Session, if this Thread has none yet + if (session == null) { + session = getNewSession(); + } + return session; + } + + @SuppressWarnings("unchecked") + public static EntityManager 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(); + return entitymanagerfactory.createEntityManager(); + } + EntityManager session = (EntityManager) 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 = entitymanagerfactory.createEntityManager(); + 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 save(Object dbo) throws MOADatabaseException { + EntityTransaction tx = null; + + try { + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + tx = session.getTransaction(); + + synchronized (session) { + tx.begin(); + session.persist(dbo); + tx.commit(); + + session.clear(); + } + return true; + + } catch(HibernateException e) { + Logger.warn("Error during database saveOrUpdate. Rollback.", e); + tx.rollback(); + throw new MOADatabaseException(e); + } + } + + + public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException { + EntityTransaction tx = null; + + try { + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + tx = session.getTransaction(); - List result; - EntityManager session = ConfigurationUtil.getCurrentSession(); - - javax.persistence.Query query = session.createQuery(QUERIES.get("getOnlineApplicationWithID")); - query.setParameter("id", id+"%"); - result = query.getResultList(); - - Logger.trace("Found entries: " + result.size()); - - if (result.size() == 0) { - Logger.trace("No entries found."); - return null; - } - return (OnlineApplication) result.get(0); - } - + synchronized (session) { + tx.begin(); + + session.merge(dbo); + session.flush(); + + tx.commit(); + + session.clear(); + } + 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) { + EntityTransaction tx = null; + try { + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + tx = session.getTransaction(); + + synchronized (session) { + tx.begin(); + session.remove(session.contains(dbo) ? dbo : session.merge(dbo)); + tx.commit(); + + session.clear(); + } + + return true; + + } catch(HibernateException e) { + Logger.warn("Error during database delete. Rollback.", e); + tx.rollback(); + return false; + } + } + } diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationUtil.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationUtil.java deleted file mode 100644 index 695bf4028..000000000 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationUtil.java +++ /dev/null @@ -1,201 +0,0 @@ -package at.gv.egovernment.moa.id.commons.db; - -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; - -import org.hibernate.HibernateException; -import org.hibernate.Session; - -import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; -import at.gv.egovernment.moa.logging.Logger; - -public final class ConfigurationUtil { - - private static EntityManagerFactory entitymanagerfactory; - - @SuppressWarnings("rawtypes") - private static final ThreadLocal THREAD_LOCAL = new ThreadLocal(); - private static boolean automaticSessionHandling = false; - - protected ConfigurationUtil() { } - - public static void initHibernate(Properties props) { - - try { - - //add Hibernate annotations -// Configuration hibernateConfig = new Configuration(); -// hibernateConfig.addAnnotatedClass(AssertionStore.class); -// hibernateConfig.addAnnotatedClass(AuthenticatedSessionStore.class); -// hibernateConfig.addAnnotatedClass(OASessionStore.class); -// hibernateConfig.addAnnotatedClass(OldSSOSessionIDStore.class); -// hibernateConfig.addProperties(props); - - - Logger.debug("Creating initial session factory..."); -// entitymanagerfactory = -// Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", -// hibernateConfig.getProperties()); - - entitymanagerfactory = - Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", - props); - - 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 EntityManager getCurrentSession() { - if (automaticSessionHandling) { - - return entitymanagerfactory.createEntityManager(); - } - - EntityManager session = (EntityManager) THREAD_LOCAL.get(); - // Open a new Session, if this Thread has none yet - if (session == null) { - session = getNewSession(); - } - return session; - } - - @SuppressWarnings("unchecked") - public static EntityManager 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(); - return entitymanagerfactory.createEntityManager(); - } - EntityManager session = (EntityManager) 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 = entitymanagerfactory.createEntityManager(); - 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 save(Object dbo) throws MOADatabaseException { - EntityTransaction tx = null; - - try { - EntityManager session = ConfigurationUtil.getCurrentSession(); - tx = session.getTransaction(); - - synchronized (session) { - tx.begin(); - session.persist(dbo); - tx.commit(); - - session.clear(); - } - return true; - - } catch(HibernateException e) { - Logger.warn("Error during database saveOrUpdate. Rollback.", e); - tx.rollback(); - throw new MOADatabaseException(e); - } - } - - - public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException { - EntityTransaction tx = null; - - try { - EntityManager session = ConfigurationUtil.getCurrentSession(); - tx = session.getTransaction(); - - synchronized (session) { - tx.begin(); - - session.merge(dbo); - session.flush(); - - tx.commit(); - - session.clear(); - } - 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) { - EntityTransaction tx = null; - try { - EntityManager session = ConfigurationUtil.getCurrentSession(); - tx = session.getTransaction(); - - synchronized (session) { - tx.begin(); - session.remove(session.contains(dbo) ? dbo : session.merge(dbo)); - tx.commit(); - - session.clear(); - } - - return true; - - } catch(HibernateException e) { - Logger.warn("Error during database delete. Rollback.", e); - tx.rollback(); - return false; - } - } - -} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionUtil.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java index ccc4e9589..a89ede528 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionUtil.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java @@ -18,7 +18,7 @@ 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 MOASessionUtil { +public final class MOASessionDBUtils { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; @@ -32,7 +32,7 @@ public final class MOASessionUtil { private static Configuration configuration; - protected MOASessionUtil() { } + protected MOASessionDBUtils() { } public static void initHibernate(Configuration config, Properties hibernateProperties) { @@ -131,7 +131,7 @@ public final class MOASessionUtil { public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException { Transaction tx = null; try { - Session session = MOASessionUtil.getCurrentSession(); + Session session = MOASessionDBUtils.getCurrentSession(); synchronized (session) { tx = session.beginTransaction(); @@ -150,7 +150,7 @@ public final class MOASessionUtil { public static boolean delete(Object dbo) { Transaction tx = null; try { - Session session = MOASessionUtil.getCurrentSession(); + Session session = MOASessionDBUtils.getCurrentSession(); synchronized (session) { tx = session.beginTransaction(); diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java index 98c2d7461..c08fe1bb2 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java @@ -30,7 +30,7 @@ import org.hibernate.annotations.DynamicUpdate; @NamedQueries({ @NamedQuery(name="getSessionWithID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore where authenticatedsessionstore.sessionid = :sessionid"), @NamedQuery(name="getSessionWithSSOID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore where authenticatedsessionstore.SSOsessionid = :sessionid"), - @NamedQuery(name="getMOAISessionsWithTimeOut", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore where authenticatedsessionstore.created < :timeout") + @NamedQuery(name="getMOAISessionsWithTimeOut", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore where authenticatedsessionstore.created < :timeoutcreate or authenticatedsessionstore.updated < :timeoutupdate") }) public class AuthenticatedSessionStore implements Serializable{ |