+++ /dev/null
-/*
- * Copyright (C) 2016 - 2018 Free Software Foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcoreee.exceptions;
-
-import java.text.MessageFormat;
-import java.util.Iterator;
-import java.util.Map;
-import javax.faces.FacesException;
-import javax.faces.application.NavigationHandler;
-import javax.faces.context.ExceptionHandler;
-import javax.faces.context.ExceptionHandlerWrapper;
-import javax.faces.context.FacesContext;
-import javax.faces.event.ExceptionQueuedEvent;
-import javax.faces.event.ExceptionQueuedEventContext;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
-
-/**
- * A custom exception handler for nice output. This code is heavily based on
- * this [1] example.
- * <p>
- * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public class CustomExceptionHandler extends ExceptionHandlerWrapper {
-
- /**
- * Logger instance
- */
- @Log
- private LoggerBeanLocal loggerBeanLocal;
-
- /**
- * Exception handler
- */
- private ExceptionHandler wrapped = null;
-
- /**
- * Constructor with exception handler to be wrapped
- * <p>
- * @param exceptionHandler Wrapped exception handler
- */
- public CustomExceptionHandler (final ExceptionHandler exceptionHandler) {
- // Call default constructor
- this();
-
- // Set handler here
- this.wrapped = exceptionHandler;
- }
-
- /**
- * Default constructor
- */
- public CustomExceptionHandler () {
- try {
- // Get initial context
- Context context = new InitialContext();
-
- // Lookup logger
- this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
- } catch (final NamingException ex) {
- // Continue to throw
- throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
- }
- }
-
- @Override
- public ExceptionHandler getWrapped () {
- return this.wrapped;
- }
-
- @Override
- public void handle () throws FacesException {
-
- final Iterator<ExceptionQueuedEvent> iterator = this.getUnhandledExceptionQueuedEvents().iterator();
-
- while (iterator.hasNext()) {
- ExceptionQueuedEvent event = iterator.next();
- ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
-
- // get the exception from context
- Throwable t = context.getException();
-
- final FacesContext facesContext = FacesContext.getCurrentInstance();
- final Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
- final NavigationHandler nav = facesContext.getApplication().getNavigationHandler();
-
- //here you do what ever you want with exception
- try {
-
- //log error ?
- this.loggerBeanLocal.logFatal("Critical Exception.", t); //NOI18N
-
- //redirect error page
- requestMap.put("exceptionMessage", t.getMessage()); //NOI18N
- nav.handleNavigation(facesContext, null, "exception"); //NOI18N
- facesContext.renderResponse();
-
- // remove the comment below if you want to report the error in a jsf error message
- // @TODO: JsfUtil.addErrorMessage(t.getMessage());
- } finally {
- //remove it from queue
- iterator.remove();
- }
- }
-
- //parent hanle
- this.getWrapped().handle();
- }
-
-}