]> git.mxchange.org Git - jcore-utils.git/commitdiff
Continued a bit:
authorRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 20:22:58 +0000 (22:22 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 20:34:11 +0000 (22:34 +0200)
- added custom exception handler (thank you to the original author)
- updated jar(s)

lib/jcore.jar
src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java [new file with mode: 0644]
src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java [new file with mode: 0644]

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