--- /dev/null
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * 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 Haeder<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> i = this.getUnhandledExceptionQueuedEvents().iterator();
+
+ while (i.hasNext()) {
+ ExceptionQueuedEvent event = i.next();
+ ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
+
+ // get the exception from context
+ Throwable t = context.getException();
+
+ final FacesContext fc = FacesContext.getCurrentInstance();
+ final Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
+ final NavigationHandler nav = fc.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(fc, null, "/error"); //NOI18N
+ fc.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
+ i.remove();
+ }
+ }
+
+ //parent hanle
+ this.getWrapped().handle();
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (C) 2016 Roland Haeder<roland@mxchange.org>
+ *
+ * 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 javax.faces.context.ExceptionHandler;
+import javax.faces.context.ExceptionHandlerFactory;
+
+/**
+ * A custom factory for exception handler. This class 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 Haeder<roland@mxchange.org>
+ */
+public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
+
+ /**
+ * Parent exception handler
+ */
+ private final ExceptionHandlerFactory parent;
+
+ public CustomExceptionHandlerFactory (final ExceptionHandlerFactory parent) {
+ // Set it here
+ this.parent = parent;
+ }
+
+ @Override
+ public ExceptionHandler getExceptionHandler () {
+
+ ExceptionHandler handler = new CustomExceptionHandler(this.parent.getExceptionHandler());
+
+ return handler;
+ }
+
+}