You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/01/13 11:06:36 UTC

svn commit: r611555 - in /myfaces/core/trunk/api/src: main/java/javax/faces/component/_MessageUtils.java main/java/javax/faces/webapp/_ErrorPageWriter.java test/java/javax/faces/component/_MessageUtilsTest.java

Author: skitching
Date: Sun Jan 13 02:06:35 2008
New Revision: 611555

URL: http://svn.apache.org/viewvc?rev=611555&view=rev
Log:
MYFACES-1802 Remove use of java1.4 method Throwable.getCause()

Added:
    myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java   (with props)
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/_MessageUtils.java
    myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/_MessageUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/_MessageUtils.java?rev=611555&r1=611554&r2=611555&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/_MessageUtils.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/_MessageUtils.java Sun Jan 13 02:06:35 2008
@@ -20,6 +20,8 @@
 
 import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
+
+import java.lang.reflect.Method;
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -32,6 +34,7 @@
 class _MessageUtils
 {
     private static final String DETAIL_SUFFIX = "_detail";
+    private static final Class[] NO_ARGS = new Class[0];
 
 	static void addErrorMessage(FacesContext facesContext,
 								UIComponent component,
@@ -62,20 +65,46 @@
     {
         StringBuffer buf = new StringBuffer();
 
-        while(cause != null && cause.getCause()!=cause)
+        while(cause != null)
         {
-            if(buf.length()>0)
+        	Throwable parentCause = getCause(cause);
+        	if (parentCause == cause)
+        	{
+        		break;
+        	}
+
+        	if(buf.length()>0)
+        	{
                 buf.append(", ");
+        	}
             
             buf.append(cause.getLocalizedMessage());
 
-            cause = cause.getCause();
+            cause = parentCause;
         }
 
         facesContext.addMessage(component.getClientId(facesContext),
                 new FacesMessage(FacesMessage.SEVERITY_ERROR, buf.toString(), buf.toString()));
     }
 
+
+    /**
+     * Get the cause of an exception, if available. Reflection must be used because
+     * JSF11 supports java1.3 but Throwable.getCause was added in java1.4.
+     */
+    static Throwable getCause(Throwable ex)
+    {
+        try
+        {
+            Method causeGetter = ex.getClass().getMethod("getCause", NO_ARGS);
+            Throwable cause = (Throwable) causeGetter.invoke(ex, NO_ARGS);
+            return cause;
+        }
+        catch (Exception e1)
+        {
+            return null;
+        }
+    }
     
     static FacesMessage getMessage(FacesContext facesContext,
                                    Locale locale,

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java?rev=611555&r1=611554&r2=611555&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java Sun Jan 13 02:06:35 2008
@@ -18,11 +18,6 @@
  */
 package javax.faces.webapp;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import javax.faces.context.FacesContext;
-import java.io.Writer;
 import java.beans.BeanInfo;
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
@@ -32,19 +27,31 @@
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
 import java.text.DateFormat;
-import java.util.*;
-import java.util.regex.Pattern;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
 import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
 import javax.faces.el.MethodBinding;
 import javax.faces.el.ValueBinding;
-import javax.servlet.http.HttpServletResponse;
 import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * @author Jacob Hookom (ICLA with ASF filed)
@@ -52,6 +59,7 @@
 class _ErrorPageWriter {
 
     private static final Log log = LogFactory.getLog(_ErrorPageWriter.class);
+    private static final Class[] NO_ARGS = new Class[0];
 
     private final static String TS = "<";
 
@@ -111,8 +119,12 @@
 
     public static void writeCause(Writer writer, Throwable ex) throws IOException {
         String msg = ex.getMessage();
-        while (ex.getCause()!=null){
-            ex=ex.getCause();
+        for(;;) {
+        	Throwable t = getCause(ex);
+        	if (t == null)
+        		break;
+        	
+        	ex = t;
             if (ex.getMessage()!=null) msg = ex.getMessage();
         }
 
@@ -381,13 +393,27 @@
            initCausePerReflection(ex,"getCause");
         }
 
-        prepareExceptionStack(ex.getCause());
+        prepareExceptionStack(getCause(ex));
     }
 
+    /**
+     * Get the cause of an exception, if available. Reflection must be used because
+     * JSF11 supports java1.3 but Throwable.getCause was added in java1.4.
+     */
+    private static Throwable getCause(Throwable ex) {
+        try {
+            Method causeGetter = ex.getClass().getMethod("getCause", NO_ARGS);
+            Throwable cause = (Throwable) causeGetter.invoke(ex, NO_ARGS);
+            return cause;
+        } catch (Exception e1) {
+            return null;
+        }
+    }
+    
     private static boolean initCausePerReflection(Throwable ex, String methodName) {
         try {
-            Method causeGetter = ex.getClass().getMethod(methodName,new Class[]{});
-            Throwable rootCause = (Throwable) causeGetter.invoke(ex,new Class[]{});
+            Method causeGetter = ex.getClass().getMethod(methodName, NO_ARGS);
+            Throwable rootCause = (Throwable) causeGetter.invoke(ex, NO_ARGS);
             return initCauseIfAvailable(ex,rootCause);
         } catch (Exception e1) {
             return false;

Added: myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java?rev=611555&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java (added)
+++ myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java Sun Jan 13 02:06:35 2008
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package javax.faces.component;
+
+import javax.faces.FacesException;
+
+import junit.framework.TestCase;
+
+public class _MessageUtilsTest extends TestCase
+{
+
+  public static void main(String[] args)
+  {
+    junit.textui.TestRunner.run(_MessageUtilsTest.class);
+  }
+
+  public _MessageUtilsTest(String name)
+  {
+    super(name);
+  }
+
+  protected void setUp() throws Exception
+  {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception
+  {
+    super.tearDown();
+  }
+
+  public void testGetCause() throws Exception
+  {
+	  IllegalStateException e1 = new IllegalStateException("e1");
+	  FacesException e2 = new FacesException("e2");
+	  FacesException e3 = new FacesException("e3", e1);
+	  
+	  Throwable t1 = _MessageUtils.getCause(e1);
+	  assertNull(t1);
+	  
+	  Throwable t2 = _MessageUtils.getCause(e2);
+	  assertNull(t2);
+	  
+	  Throwable t3 = _MessageUtils.getCause(e3);
+	  assertSame(e1, t3);
+  }
+}

Propchange: myfaces/core/trunk/api/src/test/java/javax/faces/component/_MessageUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native