Author: wglass
Date: Wed Nov 30 22:36:49 2005
New Revision: 350188

URL: http://svn.apache.org/viewcvs?rev=350188&view=rev
Log:
Set the cause for thrown exceptions when run under JDK 1.4+.  VELOCITY-425.

Added:
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
   (with props)
    
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
   (with props)
Modified:
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/MethodInvocationException.java
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/log/Log4JLogChute.java
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.java
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
    
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/provider/TestProvider.java

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/MethodInvocationException.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/MethodInvocationException.java?rev=350188&r1=350187&r2=350188&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/MethodInvocationException.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/MethodInvocationException.java
 Wed Nov 30 22:36:49 2005
@@ -1,5 +1,7 @@
 package org.apache.velocity.exception;
 
+import org.apache.velocity.util.ExceptionUtils;
+
 /*
  * Copyright 2001-2004 The Apache Software Foundation.
  *
@@ -50,6 +52,7 @@
     {
         super(message);
         this.wrapped = e;
+        ExceptionUtils.setCause(this, e);
         this.methodName = methodName;
     }       
 

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/log/Log4JLogChute.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/log/Log4JLogChute.java?rev=350188&r1=350187&r2=350188&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/log/Log4JLogChute.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/log/Log4JLogChute.java
 Wed Nov 30 22:36:49 2005
@@ -18,12 +18,13 @@
 
 import java.io.IOException;
 import java.lang.reflect.Field;
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
-import org.apache.log4j.RollingFileAppender;
 import org.apache.log4j.PatternLayout;
-import org.apache.log4j.Level;
+import org.apache.log4j.RollingFileAppender;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.util.ExceptionUtils;
 
 /**
  * Implementation of a simple log4j system that will either latch onto
@@ -118,7 +119,7 @@
         catch (IOException ioe)
         {
             rsvc.getLog().warn("Could not create file appender '"+file+'\'', 
ioe);
-            throw new Exception("Error configuring Log4JLogChute : " + ioe);
+            throw ExceptionUtils.createRuntimeException("Error configuring 
Log4JLogChute : ", ioe);
         }
     }
 

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.java?rev=350188&r1=350187&r2=350188&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.java
 Wed Nov 30 22:36:49 2005
@@ -20,6 +20,7 @@
 
 import org.apache.velocity.runtime.resource.Resource;
 import org.apache.velocity.util.ClassUtils;
+import org.apache.velocity.util.ExceptionUtils;
 import org.apache.velocity.exception.ResourceNotFoundException;
 
 import org.apache.commons.collections.ExtendedProperties;
@@ -113,11 +114,7 @@
         }
         catch( Exception fnfe )
         {
-            /*
-             *  log and convert to a general Velocity ResourceNotFoundException
-             */
-
-            throw new ResourceNotFoundException( fnfe.getMessage() );
+            throw (ResourceNotFoundException) 
ExceptionUtils.createWithCause(ResourceNotFoundException.class, "problem with 
template: " + name, fnfe );
         }
 
         if (result == null)

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java?rev=350188&r1=350187&r2=350188&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
 Wed Nov 30 22:36:49 2005
@@ -30,6 +30,7 @@
 import org.apache.commons.collections.ExtendedProperties;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.util.ExceptionUtils;
 
 /**
  * <P>This is a simple template file loader that loads templates
@@ -263,15 +264,17 @@
      *  @param i_operation string for logging, indicating caller's intention
      *
      *  @return timestamp as long
+     * @throws ResourceNotFoundException 
      */
-     private long readLastModified(Resource resource, String i_operation)
+     private long readLastModified(Resource resource, String i_operation) 
      {
          /*
           *  get the template name from the resource
           */
 
          String name = resource.getName();
-         try
+         
+        try
          {
              Connection conn = openDbConnection();
 
@@ -303,15 +306,19 @@
          }
          catch(SQLException e)
          {
-             log.error("DataSourceResourceLoader Error: error while "
+             String msg = "DataSourceResourceLoader Error: error while "
                  + i_operation + " when trying to load resource "
-                 + name, e);
-         }
+                 + name;
+             log.error(msg, e);
+             throw ExceptionUtils.createRuntimeException(msg, e);
+      }
          catch(NamingException e)
          {
-             log.error("DataSourceResourceLoader Error: error while "
+             String msg = "DataSourceResourceLoader Error: error while "
                  + i_operation + " when trying to load resource "
-                 + name, e);
+                 + name;
+             log.error(msg, e);
+             throw ExceptionUtils.createRuntimeException(msg, e);
          }
 
          return 0;
@@ -355,8 +362,10 @@
          }
          catch (Exception e)
          {
-             log.info("DataSourceResourceLoader Quirk: problem when closing 
connection", e);
-         }
+            String msg = "DataSourceResourceLoader Quirk: problem when closing 
connection";
+            log.info(msg, e);
+            throw  ExceptionUtils.createRuntimeException(msg, e);
+        }
      }
 
     /**

Added: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java?rev=350188&view=auto
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
 (added)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
 Wed Nov 30 22:36:49 2005
@@ -0,0 +1,100 @@
+package org.apache.velocity.util;
+
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+/**
+ * Use this to create a new Exception.  This will run under JDK 1.3 or greater.
+ * However, it running under JDK 1.4 it will set the cause.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Llewellyn Falco</a>
+ */
+public class ExceptionUtils
+{
+    private static boolean causesAllowed = true;
+
+    /**
+     * Create a new RuntimeException, setting the cause if possible.
+     */
+    public static RuntimeException createRuntimeException(
+            String message, Throwable cause)
+    {
+        return (RuntimeException) createWithCause(
+                RuntimeException.class, message, cause);
+    }
+
+    /**
+     * Create a new Exception, setting the cause if possible.
+     */
+    public static Throwable createWithCause(Class clazz,
+            String message, Throwable cause)
+    {
+        Throwable re = null;
+        if (causesAllowed)
+        {
+            try
+            {
+                Constructor constructor = clazz
+                        .getConstructor(new Class[]{String.class,
+                                Throwable.class});
+                re = (Throwable) constructor
+                        .newInstance(new Object[]{message, cause});
+            }
+            catch (Exception e)
+            {
+                causesAllowed = false;
+            }
+        }
+        if (re == null)
+        {
+            try
+            {
+                Constructor constructor = clazz
+                        .getConstructor(new Class[]{String.class});
+                re = (Throwable) constructor
+                        .newInstance(new Object[]{message
+                                + " caused by " + cause});
+            }
+            catch (Exception e)
+            {
+                throw new RuntimeException("Error caused " + e); // should be 
impossible
+            }
+        }
+        return re;
+    }
+
+    /**
+     * Set the cause of the Exception.  Will detect if this is not allowed.
+     */
+    public static void setCause(Throwable onObject, Throwable cause)
+    {
+        if (causesAllowed)
+        {
+            try
+            {
+                Method method = onObject.getClass().getMethod("initCause", new 
Class[]{Throwable.class});
+                method.invoke(onObject, new Object[]{cause});
+            }
+            catch (Exception e)
+            {
+                causesAllowed = false;
+            }
+        }
+    }
+}

Propchange: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/ExceptionUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java?rev=350188&view=auto
==============================================================================
--- 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
 (added)
+++ 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
 Wed Nov 30 22:36:49 2005
@@ -0,0 +1,88 @@
+package org.apache.velocity.test;
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+
+import java.io.StringWriter;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.test.provider.TestProvider;
+import org.apache.velocity.util.ExceptionUtils;
+
+
+
+/**
+ * Test that thrown exceptions include (under JDK 1.4+ a proper cause.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a>
+ * @version $Id$
+ */
+public class WrappedExceptionTestCase extends BaseTestCase implements 
TemplateTestBase
+{
+    VelocityEngine ve;
+    
+    /**
+     * Default constructor.
+     */
+    public WrappedExceptionTestCase(String name)
+    {
+        super(name);
+    }
+    
+    public static Test suite ()
+    {
+        return new TestSuite(WrappedExceptionTestCase.class);
+    }
+
+    public void setUp() throws Exception
+    {
+        ve = new VelocityEngine();
+        ve.init();
+    }
+
+
+    public void testMethodException() throws Exception
+    {
+
+        // accumulate a list of invalid references
+        Context context = new VelocityContext();
+        StringWriter writer = new StringWriter();
+        context.put("test",new TestProvider());
+        
+        try 
+        {   
+            ve.evaluate(context,writer,"test","$test.getThrow()");
+            fail ("expected an exception");
+        } 
+        catch (MethodInvocationException E)
+        {
+            assertEquals(Exception.class,E.getCause().getClass());
+            assertEquals("From getThrow()",E.getCause().getMessage());
+        }
+
+    }
+    public void testExceptionUtils()
+    {
+        Error e = new Error("Inside");
+        RuntimeException re = ExceptionUtils.createRuntimeException("Outside", 
e);
+        assertEquals("cause was set", e,re.getCause());
+    }
+
+}

Propchange: 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/WrappedExceptionTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Modified: 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/provider/TestProvider.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/provider/TestProvider.java?rev=350188&r1=350187&r2=350188&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/provider/TestProvider.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/provider/TestProvider.java
 Wed Nov 30 22:36:49 2005
@@ -349,15 +349,12 @@
     public String getFoo()
         throws Exception
     {
-        System.out.println("Hello from getfoo");
-
         throw new Exception("From getFoo()");
     }
 
     public String getThrow()
         throws Exception
     {
-        System.out.println("Hello from geThrow");
        throw new Exception("From getThrow()");
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to