Author: wesleyhales
Date: 2007-12-14 22:03:34 -0500 (Fri, 14 Dec 2007)
New Revision: 4869
Modified:
trunk/extensions/portletbridge/portletbridge-impl/src/main/java/org/ajax4jsf/portlet/ExceptionHandlerImpl.java
Log:
initial implementation of seam portlet error handling
Modified:
trunk/extensions/portletbridge/portletbridge-impl/src/main/java/org/ajax4jsf/portlet/ExceptionHandlerImpl.java
===================================================================
---
trunk/extensions/portletbridge/portletbridge-impl/src/main/java/org/ajax4jsf/portlet/ExceptionHandlerImpl.java 2007-12-14
19:21:13 UTC (rev 4868)
+++
trunk/extensions/portletbridge/portletbridge-impl/src/main/java/org/ajax4jsf/portlet/ExceptionHandlerImpl.java 2007-12-15
03:03:34 UTC (rev 4869)
@@ -1,34 +1,185 @@
/**
- *
+ *
*/
package org.ajax4jsf.portlet;
+import org.ajax4jsf.portlet.application.PortletViewState;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+
import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
import javax.portlet.faces.BridgeException;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathExpressionException;
+import java.io.IOException;
+import java.io.InputStream;
-import org.ajax4jsf.portlet.application.PortletViewState;
-
/**
* @author asmirnov
- *
+ * @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
*/
-public class ExceptionHandlerImpl implements ExceptionHandler {
- /* (non-Javadoc)
- * @see
org.ajax4jsf.portlet.ExceptionHandler#processActionException(javax.faces.context.FacesContext,
org.ajax4jsf.portlet.application.PortletViewState, java.lang.Exception)
- */
- public void processActionException(FacesContext context,
- PortletViewState windowState, Exception e) throws BridgeException {
- throw new BridgeException("Error processing action lifecycle",e);
+public class ExceptionHandlerImpl implements ExceptionHandler
+{
+ protected static final Log log = LogFactory.getLog(ExceptionHandlerImpl.class);
- }
+ private static final String seamPages = "WEB-INF/pages.xml";
- /* (non-Javadoc)
- * @see
org.ajax4jsf.portlet.ExceptionHandler#processRenderException(javax.faces.context.FacesContext,
org.ajax4jsf.portlet.application.PortletViewState, java.lang.Exception)
- */
- public void processRenderException(FacesContext context,
- PortletViewState windowState, Exception e) throws BridgeException {
- throw new BridgeException("Error processing render lifecycle",e);
- }
+ private Document getPagesDocument(String file)
+ {
+ Document document = null;
+ InputStream stream = getResourceAsStream(file);
+ if (stream == null)
+ {
+ log.info("no pages.xml file found: " + file);
+ }
+ else
+ {
+ log.debug("reading pages.xml file: " + file);
+ try
+ {
+ //get the dom from pages.xml
+ document = parse(stream);
+ }
+ catch (IOException ioe)
+ {
+ log.error("error reading pages.xml file:" + ioe);
+ }
+ catch (SAXException sax)
+ {
+ log.error("sax exception:" + sax);
+ Exception embed = sax.getException();
+ embed.printStackTrace();
+ }
+ catch (ParserConfigurationException pce)
+ {
+ log.error("parser configuration issue while reading pages.xml
file:" + pce);
+ }
+ }
+ return document;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.ajax4jsf.portlet.ExceptionHandler#processActionException(javax.faces.context.FacesContext,
org.ajax4jsf.portlet.application.PortletViewState, java.lang.Exception)
+ */
+ public void processActionException(FacesContext context,
+ PortletViewState windowState, Exception e) throws
BridgeException
+ {
+ throw new BridgeException("Error processing action lifecycle", e);
+
+ }
+
+ /* (non-Javadoc)
+ * @see
org.ajax4jsf.portlet.ExceptionHandler#processRenderException(javax.faces.context.FacesContext,
org.ajax4jsf.portlet.application.PortletViewState, java.lang.Exception)
+ */
+ public void processRenderException(FacesContext context,
+ PortletViewState windowState, Exception e) throws
BridgeException
+ {
+ Document seamPagesDoc = getPagesDocument(seamPages);
+
+ if (seamPagesDoc != null)
+ {
+ try
+ {
+ //TODO
+ context.getExternalContext().redirect(getExceptionViewId(seamPagesDoc, e));
+ }
+ catch (IOException e1)
+ {
+ e1.printStackTrace();
+ }
+ catch (XPathExpressionException e1)
+ {
+ e1.printStackTrace();
+ }
+
+ }
+ else
+ {
+ log.info("seam pages.xml not installed");
+ }
+ throw new BridgeException("Error processing render lifecycle", e);
+ }
+
+ private Document parse(InputStream stream) throws IOException, SAXException,
ParserConfigurationException
+ {
+ DocumentBuilderFactory factory =
+ DocumentBuilderFactory.newInstance();
+
+ DocumentBuilder builder = factory.newDocumentBuilder();
+
+ return builder.parse(stream);
+ }
+
+ private String getExceptionViewId(Document document, Exception e) throws
XPathExpressionException
+ {
+ XPathFactory xfactory = XPathFactory.newInstance();
+ XPath xpath = xfactory.newXPath();
+ NodeList nodes = document.getElementsByTagName("exception");
+ String redirectViewId = null;
+ boolean exceptionMatch = false;
+
+ //atleast one exception is defined in xml
+ if (nodes != null)
+ {
+ for (int i = 0; i < nodes.getLength(); i++)
+ {
+ Node node = nodes.item(i);
+ String className = xpath.evaluate("@class", node);
+ String loadedClassName = null;
+
+
+ if (className.length() > 0)
+ {
+ //compare exception class names
+ if (e.getClass().getName().equals(className))
+ {
+ return xpath.evaluate("redirect/@view-id", node);
+ }
+ }
+ else
+ {
+ //no class was defined in attribute so check for general exception
handler
+ redirectViewId = xpath.evaluate("redirect/@view-id", node);
+ }
+
+ }
+ }
+ return redirectViewId;
+ }
+
+ static InputStream getResourceAsStream(String resource)
+ {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ InputStream inputStream = null;
+ if (classLoader != null)
+ {
+ inputStream = classLoader.getResourceAsStream(resource);
+ }
+ return inputStream;
+ }
+
+ public static Class classForName(String name) throws ClassNotFoundException
+ {
+ try
+ {
+ return Thread.currentThread().getContextClassLoader().loadClass(name);
+ }
+ catch (Exception e)
+ {
+ return Class.forName(name);
+ }
+ }
+
}