org.jpos.core.handlers.exception.ExceptionHandlerAware Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpos Show documentation
Show all versions of jpos Show documentation
jPOS is an ISO-8583 based financial transaction
library/framework that can be customized and
extended in order to implement financial interchanges.
The newest version!
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2023 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.jpos.core.handlers.exception;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Interface that modifies an implementing class to add an exception handling pipeline.
*
*
* The main pipeline consists of multiple sub-pipelines:
*
* - A pipeline for which handlers are called regardless of the type of exception handled. Stored under a null key.
* - A pipeline per targeted exception type.
*
* The targeted pipeline always executes before the default pipeline.
*
*
* In the event that both of the pipelines are empty, the default behavior is to rethrow the initial exception.
*
*
* There is no need to implement the methods unless you override the default behavior.
*
*
* @author Alwyn Schoeman
* @since 2.1.2
*/
public interface ExceptionHandlerAware {
/**
*
* @return A map of exception classes to exception handlers. These handlers only execute if the exception matches.
*/
Map, List> getExceptionHandlers();
/**
* Add a handler to the default pipeline.
* @param handler ExceptionHandler to add.
*/
default void addHandler(ExceptionHandler handler) {
addHandler(handler, null);
}
/**
* Add a handler to an exception specific pipeline.
* @param handler ExceptionHandler to add.
* @param clazz Exception handler pipeline to add it to.
*/
default void addHandler(ExceptionHandler handler, Class clazz) {
List handlers = getExceptionHandlers().computeIfAbsent(clazz, f -> new ArrayList<>() );
if (handler != null) {
handlers.add(handler);
}
}
/**
* Remove a handler from the default pipeline.
* @param handler ExceptionHandler to remove.
*/
default void removeHandler(ExceptionHandler handler) {
removeHandler(handler, null);
}
/**
* Remove a handler from an exception specific handler pipeline. The list of exception
* handlers is removed once the last handler has been removed.
*
* @param handler ExceptionHandler to remove.
* @param clazz Exception pipeline to remove it from.
*/
default void removeHandler(ExceptionHandler handler, Class clazz) {
final List exceptionHandlers = getExceptionHandlers().get(clazz);
if (exceptionHandlers != null) {
exceptionHandlers.remove(handler);
if (exceptionHandlers.isEmpty()) {
removeHandlers(clazz);
}
}
}
/**
* Remove all handler for a specific exception handling pipeline.
* @param clazz Exception pipeline to remove.
*/
default void removeHandlers(Class clazz) {
getExceptionHandlers().remove(clazz);
}
/**
* Execute the pipeline by starting with the specific pipeline for the exception
* followed by the default pipeline.
*
* In the event of both pipelines being empty, the original exception is rethrown.
* @param e Initial exception.
* @return Same, modified or new exception.
* @throws Exception In the event of a handler throwing an exception. Processing by further handlers would be cancelled.
*/
default Exception handle(Exception e) throws Exception {
Exception exception = e;
final List defaultExceptionHandlers = getExceptionHandlers().get(null);
final List targetedExceptionHandlers = getExceptionHandlers().get(e.getClass());
if (targetedExceptionHandlers == null && defaultExceptionHandlers == null) {
throw e;
}
if (targetedExceptionHandlers != null) {
for (ExceptionHandler handler : targetedExceptionHandlers) {
exception = handler.handle(exception);
}
}
if (defaultExceptionHandlers != null) {
for (ExceptionHandler handler : defaultExceptionHandlers) {
exception = handler.handle(exception);
}
}
return exception;
}
}