/******************************************************************************* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ 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 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.setParameter("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); } }