]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java
renamed variable as no shortening takes place here + changed navigation outcome to...
[jcore-utils.git] / src / org / mxchange / jcoreee / exceptions / CustomExceptionHandler.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcoreee.exceptions;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.Map;
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;
34
35 /**
36  * A custom exception handler for nice output. This code is heavily based on
37  * this [1] example.
38  * <p>
39  * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 public class CustomExceptionHandler extends ExceptionHandlerWrapper {
44
45         /**
46          * Logger instance
47          */
48         @Log
49         private LoggerBeanLocal loggerBeanLocal;
50
51         /**
52          * Exception handler
53          */
54         private ExceptionHandler wrapped = null;
55
56         /**
57          * Constructor with exception handler to be wrapped
58          * <p>
59          * @param exceptionHandler Wrapped exception handler
60          */
61         public CustomExceptionHandler (final ExceptionHandler exceptionHandler) {
62                 // Call default constructor
63                 this();
64
65                 // Set handler here
66                 this.wrapped = exceptionHandler;
67         }
68
69         /**
70          * Default constructor
71          */
72         public CustomExceptionHandler () {
73                 try {
74                         // Get initial context
75                         Context context = new InitialContext();
76
77                         // Lookup logger
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) {
80                         // Continue to throw
81                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
82                 }
83         }
84
85         @Override
86         public ExceptionHandler getWrapped () {
87                 return this.wrapped;
88         }
89
90         @Override
91         public void handle () throws FacesException {
92
93                 final Iterator<ExceptionQueuedEvent> iterator = this.getUnhandledExceptionQueuedEvents().iterator();
94
95                 while (iterator.hasNext()) {
96                         ExceptionQueuedEvent event = iterator.next();
97                         ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
98
99                         // get the exception from context
100                         Throwable t = context.getException();
101
102                         final FacesContext facesContext = FacesContext.getCurrentInstance();
103                         final Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
104                         final NavigationHandler nav = facesContext.getApplication().getNavigationHandler();
105
106                         //here you do what ever you want with exception
107                         try {
108
109                                 //log error ?
110                                 this.loggerBeanLocal.logFatal("Critical Exception.", t); //NOI18N
111
112                                 //redirect error page
113                                 requestMap.put("exceptionMessage", t.getMessage()); //NOI18N
114                                 nav.handleNavigation(facesContext, null, "exception"); //NOI18N
115                                 facesContext.renderResponse();
116
117                                 // remove the comment below if you want to report the error in a jsf error message
118                                 // @TODO: JsfUtil.addErrorMessage(t.getMessage());
119                         } finally {
120                                 //remove it from queue
121                                 iterator.remove();
122                         }
123                 }
124
125                 //parent hanle
126                 this.getWrapped().handle();
127         }
128
129 }