From: Roland Haeder Date: Mon, 11 Apr 2016 20:22:58 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2da470acc690169502a678371193f6f68df5e362;p=jcoreee.git Continued a bit: - added custom exception handler (thank you to the original author) - updated jar(s) --- diff --git a/lib/jcore.jar b/lib/jcore.jar index 11f3a6a..5071278 100644 Binary files a/lib/jcore.jar and b/lib/jcore.jar differ diff --git a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java new file mode 100644 index 0000000..3df048b --- /dev/null +++ b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java @@ -0,0 +1,129 @@ +/* + * 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 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; + } + +}