From 2da470acc690169502a678371193f6f68df5e362 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 11 Apr 2016 22:22:58 +0200 Subject: [PATCH] Continued a bit: - added custom exception handler (thank you to the original author) - updated jar(s) --- lib/jcore.jar | Bin 13656 -> 19479 bytes .../exceptions/CustomExceptionHandler.java | 129 ++++++++++++++++++ .../CustomExceptionHandlerFactory.java | 50 +++++++ 3 files changed, 179 insertions(+) create mode 100644 src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java create mode 100644 src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java diff --git a/lib/jcore.jar b/lib/jcore.jar index 11f3a6a56cf7e027a8bd970a4eec384505b79a56..5071278de7c81e47c70f3294c42f3d61b989623f 100644 GIT binary patch delta 5967 zcmbtYYj9NM8Gg>*&SrB0xou!`fh7qNHsnGG5yLe^fnc}@h$1R1$st+X?1s&TfZDED zZB?ulPf_SiYqeNw6))VxLHyAl?bPY$)DGjcb=2uNW3?Usbf%0oectbEb~liT&NP!f z-+R8xdwst5d%tsW&l5+rf{oWZT|U4IG(30ca6z4O9-Bo^Izw#UpQ)NJ>!h*fXpO49 zS!-8qo}<=kcInmy&8Obq)~-qSFH~)ZqfNDUI#w2O{Tk%_RazHH%E|`ALqj9+L~m18 z2~K?VO~>yHS^4UrsjD4|c(3SvO%bn@d~6d({hv5h&%J>^+dbPWYV64es~)t8g5U<5 z*irquJ*TpEolQh%_juIuck2&nfiVw=mLnf6z^PIl``8x7+_%sysnaRw&Aq}p&x!XH66ta6=B$hkktee}V!M8a+oaixE zMWUvLz{VU&g&9$M!u!G+nytO%C6$vYWj$J6+Tqcl!J%C4aA|PD1+aC)!#)i3di2nV^ov8h;vKME!gr0n@GObMBFSYM7HW0Y!5O7hlNp<|-U_x)cPni)7+o>y z*C{bE$!(!5pLfOb%Zyk|bq2*64K1=@@`!V`PtBGfQpW!k>9Vm%XH;t36P~*-Y+wrN z4OF7afPn%7<*3jx&p-nj4fq6SO&S&m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_fV#%@udCY{Nv-62ld1vIBr5-21y`LnwO*wuU<(oBBRV51#8Y{5beK%o*@ zs1Wz$0AcCLH#~&EQg4A$3<_{5El)^li1w6b3Q__K_j)RWy{CEi-d-M{*Fabe#ZCMw zV1>F&?33rSuucBt2?>vn`dkd0Yz$y*kD?)7b@D_n&B^yvCA86fejwWYsu|D*5OzY* bbae6rFLx1Epn(hw5. + */ +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. + *

+ * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/ + *

+ * @author Roland Haeder + */ +public class CustomExceptionHandler extends ExceptionHandlerWrapper { + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * Exception handler + */ + private ExceptionHandler wrapped = null; + + /** + * Constructor with exception handler to be wrapped + *

+ * @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 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 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(); + } + +} diff --git a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java new file mode 100644 index 0000000..38b7bee --- /dev/null +++ b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java @@ -0,0 +1,50 @@ +/* + * 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 . + */ +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. + *

+ * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/ + *

+ * @author Roland Haeder + */ +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; + } + +} -- 2.39.5