From cd1a5f3b69f38c1aa7bad2826f2be2df45f75346 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Tue, 24 Sep 2013 11:39:46 +0200 Subject: implement advanced statistic logger --- .../moa/id/commons/db/ConfigurationDBRead.java | 12 +- .../moa/id/commons/db/ConfigurationDBUtils.java | 17 -- .../moa/id/commons/db/MOASessionDBUtils.java | 6 - .../moa/id/commons/db/StatisticLogDBUtils.java | 166 +++++++++++++++ .../id/commons/db/dao/session/OASessionStore.java | 5 - .../id/commons/db/dao/statistic/StatisticLog.java | 237 +++++++++++++++++++++ 6 files changed, 406 insertions(+), 37 deletions(-) create mode 100644 id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/StatisticLogDBUtils.java create mode 100644 id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/statistic/StatisticLog.java (limited to 'id/server/moa-id-commons/src/main') 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 index d281eebbc..47136d162 100644 --- 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 @@ -15,6 +15,7 @@ import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; +@SuppressWarnings("rawtypes") public class ConfigurationDBRead { private static Map QUERIES = new HashMap(); @@ -36,20 +37,16 @@ public class ConfigurationDBRead { QUERIES.put("getUsersWithOADBID", "select userdatabase from UserDatabase userdatabase inner join userdatabase.onlineApplication oa where oa.hjid = :id"); QUERIES.put("searchOnlineApplicationsWithID", "select onlineapplication from OnlineApplication onlineapplication where onlineapplication.friendlyName like :id"); } - @SuppressWarnings("rawtypes") public static OnlineApplication getActiveOnlineApplication(String id) { MiscUtil.assertNotNull(id, "OnlineApplictionID"); Logger.trace("Getting OnlineApplication with ID " + id + " from database."); - - //TODO: check!!! - id = StringEscapeUtils.escapeHtml(id); List result; EntityManager session = ConfigurationDBUtils.getCurrentSession(); javax.persistence.Query query = session.createQuery(QUERIES.get("getActiveOnlineApplicationWithID")); //query.setParameter("id", id+"%"); - query.setParameter("id", id); + query.setParameter("id", StringEscapeUtils.escapeHtml(id)); result = query.getResultList(); Logger.trace("Found entries: " + result.size()); @@ -62,8 +59,6 @@ public class ConfigurationDBRead { return (OnlineApplication) result.get(0); } - - @SuppressWarnings("rawtypes") public static OnlineApplication getOnlineApplication(String id) { MiscUtil.assertNotNull(id, "OnlineApplictionID"); Logger.trace("Getting OnlineApplication with ID " + id + " from database."); @@ -86,7 +81,6 @@ public class ConfigurationDBRead { return (OnlineApplication) result.get(0); } - @SuppressWarnings("rawtypes") public static OnlineApplication getOnlineApplication(long dbid) { MiscUtil.assertNotNull(dbid, "OnlineApplictionID"); Logger.trace("Getting OnlineApplication with DBID " + dbid + " from database."); @@ -112,7 +106,7 @@ public class ConfigurationDBRead { public static MOAIDConfiguration getMOAIDConfiguration() { Logger.trace("Load MOAID Configuration from database."); - List result; + List result; EntityManager session = ConfigurationDBUtils.getCurrentSession(); javax.persistence.Query query = session.createQuery(QUERIES.get("getMOAIDConfiguration")); 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 16cea07d8..a177ea938 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 @@ -8,8 +8,6 @@ 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; @@ -27,20 +25,8 @@ public final class ConfigurationDBUtils { public static void initHibernate(Properties props) throws MOADatabaseException { 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", @@ -94,7 +80,6 @@ public final class ConfigurationDBUtils { 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_CONFIG.get(); @@ -198,8 +183,6 @@ public final class ConfigurationDBUtils { tx.begin(); session.remove(session.contains(dbo) ? dbo : session.merge(dbo)); tx.commit(); - - //session.clear(); } return true; 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 index 5e4ec0f13..057ccdef7 100644 --- 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 @@ -11,10 +11,6 @@ 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; @@ -29,8 +25,6 @@ public final class MOASessionDBUtils { 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() { } diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/StatisticLogDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/StatisticLogDBUtils.java new file mode 100644 index 000000000..b60075788 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/StatisticLogDBUtils.java @@ -0,0 +1,166 @@ +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.ex.MOADatabaseException; +import at.gv.egovernment.moa.logging.Logger; + +public final class StatisticLogDBUtils { + + private static SessionFactory sessionFactory; + private static ServiceRegistry serviceRegistry; + + @SuppressWarnings("rawtypes") + private static final ThreadLocal THREAD_LOCAL_STATISTIC = 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"; + + protected StatisticLogDBUtils() { } + + 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 StatisicLogger session factory..."); + + config.configure(); + serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); + sessionFactory = config.buildSessionFactory(serviceRegistry); + Logger.debug("Initial StatisicLogger session factory successfully created."); + + } catch (Throwable ex) { + Logger.error("Initial StatisicLogger 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_STATISTIC.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_STATISTIC.get(); + if (session != null) { + Logger.warn("Previous StatisicLogger session has not been closed; closing session now."); + closeSession(); + } + Logger.debug("Opening new StatisicLogger hibernate session..."); + try { + session = sessionFactory.openSession(); + THREAD_LOCAL_STATISTIC.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 StatisicLogger hibernate session..."); + Session session = (Session) THREAD_LOCAL_STATISTIC.get(); + THREAD_LOCAL_STATISTIC.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 = StatisticLogDBUtils.getCurrentSession(); + + synchronized (session) { + tx = session.beginTransaction(); + session.saveOrUpdate(dbo); + tx.commit(); + } + + Logger.info("Insert advanced statistic log entry into database"); + return true; + + } catch(HibernateException e) { + Logger.warn("Error during StatisicLogger database saveOrUpdate. Rollback.", e); + tx.rollback(); + throw new MOADatabaseException(e); + } + } + + public static boolean delete(Object dbo) { + Transaction tx = null; + try { + Session session = StatisticLogDBUtils.getCurrentSession(); + + synchronized (session) { + tx = session.beginTransaction(); + session.delete(dbo); + tx.commit(); + } + + return true; + + } catch(HibernateException e) { + Logger.warn("Error during StatisicLogger 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/dao/session/OASessionStore.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/OASessionStore.java index 6e0f47805..3872397f7 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/OASessionStore.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/OASessionStore.java @@ -11,14 +11,9 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; -import javax.persistence.PrePersist; import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.annotations.NamedQueries; -import org.hibernate.annotations.NamedQuery; @Entity @DynamicUpdate(value=true) diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/statistic/StatisticLog.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/statistic/StatisticLog.java new file mode 100644 index 000000000..643136e70 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/statistic/StatisticLog.java @@ -0,0 +1,237 @@ +package at.gv.egovernment.moa.id.commons.db.dao.statistic; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.annotations.DynamicUpdate; + + + +@Entity +@DynamicUpdate(value=true) +@Table(name = "statisiclog") +//@NamedQueries({ +// @NamedQuery(name="getAssertionWithArtifact", query = "select assertionstore from AssertionStore assertionstore where assertionstore.artifact = :artifact"), +// @NamedQuery(name="getAssertionWithTimeOut", query = "select assertionstore from AssertionStore assertionstore where assertionstore.timestamp < :timeout") +//}) + +public class StatisticLog implements Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique=true, nullable=false) + private long id; + + @Column(name = "timestamp", nullable=false) + Date timestamp; + + @Column(name = "OAURLPrefix", unique=false) + private String oaurlprefix; + + @Column(name = "BKUURL", unique=false) + private String bkuurl; + + @Column(name = "isSSOLogin", unique=false) + private boolean ssosession; + + @Column(name = "isBusinessService", unique=false) + private boolean businessservice; + + @Column(name = "isMandateLogin", unique=false) + private boolean mandatelogin; + + @Column(name = "MandateType", unique=false) + private String mandatetype; + + @Column(name = "ProtocolSubType", unique=false) + private String protocolsubtype; + + @Column(name = "ProtocolType", unique=false) + private String protocoltype; + + @Column(name = "ExceptionCode", unique=false) + private String errorcode; + + @Column(name = "ExceptionMessage", unique=false) + private String errormessage; + + /** + * @return the id + */ + public long getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(long id) { + this.id = id; + } + + /** + * @return the timestamp + */ + public Date getTimestamp() { + return timestamp; + } + + /** + * @param timestamp the timestamp to set + */ + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + /** + * @return the oaurlprefix + */ + public String getOaurlprefix() { + return oaurlprefix; + } + + /** + * @param oaurlprefix the oaurlprefix to set + */ + public void setOaurlprefix(String oaurlprefix) { + this.oaurlprefix = oaurlprefix; + } + + /** + * @return the bkuurl + */ + public String getBkuurl() { + return bkuurl; + } + + /** + * @param bkuurl the bkuurl to set + */ + public void setBkuurl(String bkuurl) { + this.bkuurl = bkuurl; + } + + /** + * @return the ssosession + */ + public boolean isSsosession() { + return ssosession; + } + + /** + * @param ssosession the ssosession to set + */ + public void setSsosession(boolean ssosession) { + this.ssosession = ssosession; + } + + /** + * @return the mandatelogin + */ + public boolean isMandatelogin() { + return mandatelogin; + } + + /** + * @param mandatelogin the mandatelogin to set + */ + public void setMandatelogin(boolean mandatelogin) { + this.mandatelogin = mandatelogin; + } + + /** + * @return the mandatetype + */ + public String getMandatetype() { + return mandatetype; + } + + /** + * @param mandatetype the mandatetype to set + */ + public void setMandatetype(String mandatetype) { + this.mandatetype = mandatetype; + } + + /** + * @return the mandatesubtype + */ + public String getProtocolsubtype() { + return protocolsubtype; + } + + /** + * @param mandatesubtype the mandatesubtype to set + */ + public void setProtocolsubtype(String mandatesubtype) { + this.protocolsubtype = mandatesubtype; + } + + /** + * @return the protocoltype + */ + public String getProtocoltype() { + return protocoltype; + } + + /** + * @param protocoltype the protocoltype to set + */ + public void setProtocoltype(String protocoltype) { + this.protocoltype = protocoltype; + } + + /** + * @return the errorcode + */ + public String getErrorcode() { + return errorcode; + } + + /** + * @param errorcode the errorcode to set + */ + public void setErrorcode(String errorcode) { + this.errorcode = errorcode; + } + + /** + * @return the errormessage + */ + public String getErrormessage() { + return errormessage; + } + + /** + * @param errormessage the errormessage to set + */ + public void setErrormessage(String errormessage) { + this.errormessage = errormessage; + } + + /** + * @return the businessservice + */ + public boolean isBusinessservice() { + return businessservice; + } + + /** + * @param businessservice the businessservice to set + */ + public void setBusinessservice(boolean businessservice) { + this.businessservice = businessservice; + } + + + +} -- cgit v1.2.3