| package org.eclipse.osbp.persistence.fragment; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.osbp.core.api.persistence.IPersistenceException; |
| import org.eclipse.osbp.core.api.persistence.PersistenceExceptionEvent; |
| import org.eclipse.persistence.exceptions.DatabaseException; |
| import org.eclipse.persistence.exceptions.ExceptionHandler; |
| import org.eclipse.persistence.queries.SQLCall; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class PersistenceExceptionHandler implements IPersistenceException, ExceptionHandler { |
| |
| private List<IPersistenceException.Receiver> exceptionReceiver = new ArrayList<>(); |
| private static Logger log = LoggerFactory.getLogger(PersistenceExceptionHandler.class); |
| |
| @Override |
| public Object handleException(RuntimeException exception) { |
| log.debug("{}", exception); |
| // in case of a |
| if(exception instanceof DatabaseException) { |
| if(((DatabaseException)exception).getCall() instanceof SQLCall) { |
| SQLCall call = (SQLCall)((DatabaseException)exception).getCall(); |
| if(call.getQueryString().toUpperCase().startsWith("CREATE TABLE")) { |
| return null; |
| } |
| } |
| |
| } |
| String message = exception.getMessage(); |
| if(exception.getCause() != null) { |
| message = exception.getCause().getMessage(); |
| } |
| log.debug("the number of registrated receivers is: {}", exceptionReceiver.size()); |
| PersistenceExceptionEvent event = new PersistenceExceptionEvent(message, true); |
| exceptionReceiver.forEach(it -> it.exceptionEvent(event)); |
| throw exception; |
| } |
| |
| @Override |
| public void registerNotification(IPersistenceException.Receiver receiver) { |
| log.debug("a receiver was registrated: {}", receiver.getClass().getCanonicalName()); |
| exceptionReceiver.add(receiver); |
| } |
| } |