aboutsummaryrefslogtreecommitdiff
path: root/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java')
-rw-r--r--id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java153
1 files changed, 153 insertions, 0 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java
new file mode 100644
index 000000000..13919a13c
--- /dev/null
+++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/DBExceptionStoreImpl.java
@@ -0,0 +1,153 @@
+package at.gv.egovernment.moa.id.storage;
+
+import java.util.Date;
+import java.util.List;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.hibernate.HibernateException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import at.gv.egovernment.moa.id.commons.db.MOASessionDBUtils;
+import at.gv.egovernment.moa.id.commons.db.dao.session.ExceptionStore;
+import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException;
+import at.gv.egovernment.moa.id.util.Random;
+import at.gv.egovernment.moa.logging.Logger;
+import at.gv.egovernment.moa.util.MiscUtil;
+
+public class DBExceptionStoreImpl implements IExceptionStore {
+
+ private static DBExceptionStoreImpl store;
+
+ public static DBExceptionStoreImpl getStore() {
+ if(store == null) {
+ store = new DBExceptionStoreImpl();
+ }
+ return store;
+ }
+
+ public String storeException(Throwable e) {
+ String id = Random.nextRandom();
+
+ Logger.debug("Store Exception with ID " + id);
+
+ ExceptionStore dbexception = new ExceptionStore();
+ dbexception.setExid(id);
+
+ byte[] data = SerializationUtils.serialize(e);
+ dbexception.setException(data);
+
+ dbexception.setTimestamp(new Date());
+
+ try {
+ MOASessionDBUtils.saveOrUpdate(dbexception);
+
+ } catch (MOADatabaseException e1) {
+ Logger.warn("Exception can not be stored in Database.", e);
+ return null;
+ }
+
+ return id;
+ }
+
+ public Throwable fetchException(String id) {
+
+ try {
+ Logger.debug("Fetch Exception with ID " + id);
+
+ ExceptionStore ex = searchInDatabase(id);
+
+ Object data = SerializationUtils.deserialize(ex.getException());
+ if (data instanceof Throwable)
+ return (Throwable) data;
+
+ else {
+ Logger.warn("Exeption is not of classtype Throwable");
+ return null;
+ }
+
+
+ } catch (MOADatabaseException e) {
+ Logger.info("No Exception found with ID=" + id);
+ return null;
+
+ } catch (Exception e) {
+ Logger.warn("Exception can not deserialized from Database.",e);
+ return null;
+ }
+
+ }
+
+ public void removeException(String id) {
+ try {
+ ExceptionStore ex = searchInDatabase(id);
+ MOASessionDBUtils.delete(ex);
+
+ Logger.debug("Delete Execption with ID " + id);
+
+ } catch (MOADatabaseException e) {
+ Logger.info("No Exception found with ID=" + id);
+ }
+
+
+ }
+
+ public void clean(long now, long exceptionTimeOut) {
+ Date expioredate = new Date(now - exceptionTimeOut);
+
+ List<ExceptionStore> results;
+ Session session = MOASessionDBUtils.getCurrentSession();
+
+ synchronized (session) {
+ session.beginTransaction();
+ Query query = session.getNamedQuery("getExceptionWithTimeOut");
+ query.setTimestamp("timeout", expioredate);
+ results = query.list();
+ session.getTransaction().commit();
+ }
+
+ if (results.size() != 0) {
+ for(ExceptionStore result : results) {
+ try {
+ MOASessionDBUtils.delete(result);
+ Logger.info("Remove Exception with ID=" + result.getExid()
+ + " after timeout.");
+
+ } catch (HibernateException e){
+ Logger.warn("Exception with ID=" + result.getExid()
+ + " not removed after timeout! (Error during Database communication)", e);
+ }
+
+ }
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private ExceptionStore searchInDatabase(String id) throws MOADatabaseException {
+ MiscUtil.assertNotNull(id, "exceptionID");
+ Logger.trace("Getting Exception with ID " + id + " from database.");
+ Session session = MOASessionDBUtils.getCurrentSession();
+ List result;
+
+ synchronized (session) {
+ session.beginTransaction();
+ Query query = session.getNamedQuery("getExceptionWithID");
+ query.setString("id", id);
+ result = query.list();
+
+ //send transaction
+ session.getTransaction().commit();
+ }
+
+ Logger.trace("Found entries: " + result.size());
+
+ //Assertion requires an unique artifact
+ if (result.size() != 1) {
+ Logger.trace("No entries found.");
+ throw new MOADatabaseException("No Exception found with ID " + id);
+ }
+
+ return (ExceptionStore) result.get(0);
+ }
+
+}