Author: craigmcc Date: Thu May 25 21:22:06 2006 New Revision: 409553 URL: http://svn.apache.org/viewvc?rev=409553&view=rev Log: Cache exceptions from calls to prerender() and preprocess() callbacks, to avoid having the preprocess() method called twice. This is further progress towards coherent exception handling (SHALE-125), although it surfaces a need to refactor the multiple cases where exceptions are being cached into a common strategy-pattern based implementation.
JIRA Issue: SHALE-30 Reported By: Darren Boyd <darren AT bitcraft.ca> Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/component/Subview.java struts/shale/trunk/core-library/src/java/org/apache/shale/view/faces/ViewPhaseListener.java Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/component/Subview.java URL: http://svn.apache.org/viewvc/struts/shale/trunk/core-library/src/java/org/apache/shale/component/Subview.java?rev=409553&r1=409552&r2=409553&view=diff ============================================================================== --- struts/shale/trunk/core-library/src/java/org/apache/shale/component/Subview.java (original) +++ struts/shale/trunk/core-library/src/java/org/apache/shale/component/Subview.java Thu May 25 21:22:06 2006 @@ -70,7 +70,11 @@ ViewController vc = getViewController(context, false); if (vc != null) { - vc.prerender(); + try { + vc.prerender(); + } catch (Exception e) { + cacheException(e); + } } super.processDecodes(context); @@ -88,7 +92,11 @@ ViewController vc = getViewController(context, true); if (vc != null) { - vc.preprocess(); + try { + vc.preprocess(); + } catch (Exception e) { + cacheException(e); + } } super.processDecodes(context); @@ -158,6 +166,74 @@ // Return the initialized ViewController return vc; + + } + + + /** + * <p>Log the specified exception, and cache it on the list of exceptions + * to be reported after all of the appropriate <code>destroy()</code> calls + * have been completed.</p> + * + * @param exception Exception to be cached + */ + private void cacheException(Exception exception) { + + // Log the exception unconditionally + log(exception.getMessage(), exception); + + // Are we within the context of a JavaServer Faces request? + // If so, accumulate this exception to the list that can be + // reported at the completion of the request. + FacesContext context = FacesContext.getCurrentInstance(); + if (context == null) { + return; + } + List list = (List) context.getExternalContext().getRequestMap(). + get(Constants.EXCEPTIONS_LIST); + if (list == null) { + list = new ArrayList(4); + context.getExternalContext().getRequestMap(). + put(Constants.EXCEPTIONS_LIST, list); + } + list.add(exception); + + } + + + /** + * <p>Log the specified message via <code>FacesContext</code> if it is + * not null, or directly to the container otherwise.</p> + * + * @param message Message to be logged + */ + private void log(String message) { + + FacesContext context = FacesContext.getCurrentInstance(); + if (context != null) { + context.getExternalContext().log(message); + } else { + System.out.println(message); + } + + } + + + /** + * <p>Log the specified message and exception via <code>FacesContext</code> + * if it is not null, or directly to the container otherwise.</p> + * + * @param message Message to be logged + * @param throwable Exception to be logged + */ + private void log(String message, Throwable throwable) { + + FacesContext context = FacesContext.getCurrentInstance(); + if (context != null) { + context.getExternalContext().log(message); + } else { + System.out.println(message); + } } Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/view/faces/ViewPhaseListener.java URL: http://svn.apache.org/viewvc/struts/shale/trunk/core-library/src/java/org/apache/shale/view/faces/ViewPhaseListener.java?rev=409553&r1=409552&r2=409553&view=diff ============================================================================== --- struts/shale/trunk/core-library/src/java/org/apache/shale/view/faces/ViewPhaseListener.java (original) +++ struts/shale/trunk/core-library/src/java/org/apache/shale/view/faces/ViewPhaseListener.java Thu May 25 21:22:06 2006 @@ -16,6 +16,7 @@ package org.apache.shale.view.faces; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -161,7 +162,11 @@ Iterator vcs = list.iterator(); while (vcs.hasNext()) { Object vc = vcs.next(); - viewControllerCallbacks(event.getFacesContext()).preprocess(vc); + try { + viewControllerCallbacks(event.getFacesContext()).preprocess(vc); + } catch (Exception e) { + cacheException(e); + } } } @@ -181,8 +186,80 @@ if (vc == null) { return; } - viewControllerCallbacks(event.getFacesContext()).prerender(vc); + try { + viewControllerCallbacks(event.getFacesContext()).prerender(vc); + } catch (Exception e) { + cacheException(e); + } map.remove(Constants.VIEW_RENDERED); + + } + + + /** + * <p>Log the specified exception, and cache it on the list of exceptions + * to be reported after all of the appropriate <code>destroy()</code> calls + * have been completed.</p> + * + * @param exception Exception to be cached + */ + private void cacheException(Exception exception) { + + // Log the exception unconditionally + log(exception.getMessage(), exception); + + // Are we within the context of a JavaServer Faces request? + // If so, accumulate this exception to the list that can be + // reported at the completion of the request. + FacesContext context = FacesContext.getCurrentInstance(); + if (context == null) { + return; + } + List list = (List) context.getExternalContext().getRequestMap(). + get(Constants.EXCEPTIONS_LIST); + if (list == null) { + list = new ArrayList(4); + context.getExternalContext().getRequestMap(). + put(Constants.EXCEPTIONS_LIST, list); + } + list.add(exception); + + } + + + /** + * <p>Log the specified message via <code>FacesContext</code> if it is + * not null, or directly to the container otherwise.</p> + * + * @param message Message to be logged + */ + private void log(String message) { + + FacesContext context = FacesContext.getCurrentInstance(); + if (context != null) { + context.getExternalContext().log(message); + } else { + System.out.println(message); + } + + } + + + /** + * <p>Log the specified message and exception via <code>FacesContext</code> + * if it is not null, or directly to the container otherwise.</p> + * + * @param message Message to be logged + * @param throwable Exception to be logged + */ + private void log(String message, Throwable throwable) { + + FacesContext context = FacesContext.getCurrentInstance(); + if (context != null) { + context.getExternalContext().log(message); + } else { + System.out.println(message); + } }