2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcoreee.exceptions;
19 import java.text.MessageFormat;
20 import java.util.Iterator;
22 import javax.faces.FacesException;
23 import javax.faces.application.NavigationHandler;
24 import javax.faces.context.ExceptionHandler;
25 import javax.faces.context.ExceptionHandlerWrapper;
26 import javax.faces.context.FacesContext;
27 import javax.faces.event.ExceptionQueuedEvent;
28 import javax.faces.event.ExceptionQueuedEventContext;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
33 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
36 * A custom exception handler for nice output. This code is heavily based on
39 * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/
41 * @author Roland Häder<roland@mxchange.org>
43 public class CustomExceptionHandler extends ExceptionHandlerWrapper {
49 private LoggerBeanLocal loggerBeanLocal;
54 private ExceptionHandler wrapped = null;
57 * Constructor with exception handler to be wrapped
59 * @param exceptionHandler Wrapped exception handler
61 public CustomExceptionHandler (final ExceptionHandler exceptionHandler) {
62 // Call default constructor
66 this.wrapped = exceptionHandler;
72 public CustomExceptionHandler () {
74 // Get initial context
75 Context context = new InitialContext();
78 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
79 } catch (final NamingException ex) {
81 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
86 public ExceptionHandler getWrapped () {
91 public void handle () throws FacesException {
93 final Iterator<ExceptionQueuedEvent> iterator = this.getUnhandledExceptionQueuedEvents().iterator();
95 while (iterator.hasNext()) {
96 ExceptionQueuedEvent event = iterator.next();
97 ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
99 // get the exception from context
100 Throwable t = context.getException();
102 final FacesContext facesContext = FacesContext.getCurrentInstance();
103 final Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
104 final NavigationHandler nav = facesContext.getApplication().getNavigationHandler();
106 //here you do what ever you want with exception
110 this.loggerBeanLocal.logFatal("Critical Exception.", t); //NOI18N
112 //redirect error page
113 requestMap.put("exceptionMessage", t.getMessage()); //NOI18N
114 nav.handleNavigation(facesContext, null, "exception"); //NOI18N
115 facesContext.renderResponse();
117 // remove the comment below if you want to report the error in a jsf error message
118 // @TODO: JsfUtil.addErrorMessage(t.getMessage());
120 //remove it from queue
126 this.getWrapped().handle();