You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2019/02/22 23:09:21 UTC

svn commit: r1854180 - in /velocity/engine/branches/VELOCITY-892/velocity-engine-core/src: main/java/org/apache/velocity/runtime/ main/java/org/apache/velocity/util/introspection/ test/java/org/apache/velocity/test/util/introspection/

Author: cbrisson
Date: Fri Feb 22 23:09:21 2019
New Revision: 1854180

URL: http://svn.apache.org/viewvc?rev=1854180&view=rev
Log:
[VELOCITY-892] Allow user to specity an instance of a conversion handler

Modified:
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=1854180&r1=1854179&r2=1854180&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java Fri Feb 22 23:09:21 2019
@@ -252,22 +252,16 @@ public interface RuntimeConstants
 
     /*
      * ----------------------------------------------------------------------
-     * G E N E R A L  R U N T I M E  C O N F I G U R A T I O N
+     * I N T R O S P E C T I O N  C O N F I G U R A T I O N
      * ----------------------------------------------------------------------
      */
 
-    /** Switch for the interpolation facility for string literals. */
-    String INTERPOLATE_STRINGLITERALS = "runtime.interpolate.string.literals";
-
-    /** The character encoding for the templates. Used by the parser in processing the input streams. */
-    String INPUT_ENCODING = "input.encoding";
-
-    /** Default Encoding is UTF-8. */
-    String ENCODING_DEFAULT = "UTF-8";
-
     /** key name for uberspector. Multiple classnames can be specified,in which case uberspectors will be chained. */
     String UBERSPECT_CLASSNAME = "runtime.introspector.uberspect";
 
+    /** key for Conversion Manager instance */
+    String CONVERSION_HANDLER_INSTANCE = "runtime.conversion.handler.instance";
+
     /** key for Conversion Manager class */
     String CONVERSION_HANDLER_CLASS = "runtime.conversion.handler.class";
 
@@ -277,6 +271,22 @@ public interface RuntimeConstants
     /** A comma separated list of classes to restrict access to in the SecureIntrospector. */
     String INTROSPECTOR_RESTRICT_CLASSES = "introspector.restrict.classes";
 
+
+    /*
+     * ----------------------------------------------------------------------
+     * G E N E R A L  R U N T I M E  C O N F I G U R A T I O N
+     * ----------------------------------------------------------------------
+     */
+
+    /** Switch for the interpolation facility for string literals. */
+    String INTERPOLATE_STRINGLITERALS = "runtime.interpolate.string.literals";
+
+    /** The character encoding for the templates. Used by the parser in processing the input streams. */
+    String INPUT_ENCODING = "input.encoding";
+
+    /** Default Encoding is UTF-8. */
+    String ENCODING_DEFAULT = "UTF-8";
+
     /** Switch for ignoring nulls in math equations vs throwing exceptions. */
     String STRICT_MATH = "runtime.strict.math";
 

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java?rev=1854180&r1=1854179&r2=1854180&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java Fri Feb 22 23:09:21 2019
@@ -110,7 +110,7 @@ public interface RuntimeServices
      * @param key
      * @param value
      */
- void addProperty(String key, Object value);
+    void addProperty(String key, Object value);
 
     /**
      * Clear the values pertaining to a particular

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=1854180&r1=1854179&r2=1854180&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java Fri Feb 22 23:09:21 2019
@@ -103,39 +103,40 @@ public class UberspectImpl implements Ub
         rsvc = rs;
         log = rsvc.getLog("introspection");
 
-        String conversionHandlerClass = rs.getString(RuntimeConstants.CONVERSION_HANDLER_CLASS);
-        if (conversionHandlerClass == null || conversionHandlerClass.equals("none"))
+        Object conversionHandlerInstance = rs.getProperty(RuntimeConstants.CONVERSION_HANDLER_INSTANCE);
+        if (conversionHandlerInstance == null)
         {
-            conversionHandler = null;
-        }
-        else
-        {
-            Object o = null;
-
-            try
-            {
-                o = ClassUtils.getNewInstance(conversionHandlerClass);
-            }
-            catch (ClassNotFoundException cnfe )
+            String conversionHandlerClass = rs.getString(RuntimeConstants.CONVERSION_HANDLER_CLASS);
+            if (conversionHandlerClass != null && !conversionHandlerClass.equals("none"))
             {
-                String err = "The specified class for ConversionHandler (" + conversionHandlerClass
+                try
+                {
+                    conversionHandlerInstance = ClassUtils.getNewInstance(conversionHandlerClass);
+                }
+                catch (ClassNotFoundException cnfe )
+                {
+                    String err = "The specified class for ConversionHandler (" + conversionHandlerClass
                         + ") does not exist or is not accessible to the current classloader.";
-                log.error(err);
-                throw new VelocityException(err, cnfe);
-            }
-            catch (InstantiationException ie)
-            {
-                throw new VelocityException("Could not instantiate class '" + conversionHandlerClass + "'", ie);
-            }
-            catch (IllegalAccessException ae)
-            {
-                throw new VelocityException("Cannot access class '" + conversionHandlerClass + "'", ae);
+                    log.error(err);
+                    throw new VelocityException(err, cnfe);
+                }
+                catch (InstantiationException ie)
+                {
+                    throw new VelocityException("Could not instantiate class '" + conversionHandlerClass + "'", ie);
+                }
+                catch (IllegalAccessException ae)
+                {
+                    throw new VelocityException("Cannot access class '" + conversionHandlerClass + "'", ae);
+                }
             }
+        }
 
-            if (o instanceof ConversionHandler)
+        if (conversionHandlerInstance != null)
+        {
+            if (conversionHandlerInstance instanceof ConversionHandler)
             {
                 log.warn("The ConversionHandler interface is deprecated - see the TypeConversionHandler interface");
-                final ConversionHandler ch = (ConversionHandler)o;
+                final ConversionHandler ch = (ConversionHandler)conversionHandlerInstance;
                 conversionHandler = new TypeConversionHandler()
                 {
                     @Override
@@ -163,17 +164,19 @@ public class UberspectImpl implements Ub
                     }
                 };
             }
-            else if (!(o instanceof TypeConversionHandler))
+            else if (!(conversionHandlerInstance instanceof TypeConversionHandler))
             {
-                String err = "The specified class for ResourceManager (" + conversionHandlerClass
+                String err = "The specified class or provided instance for the conversion handler (" + conversionHandlerInstance.getClass().getName()
                         + ") does not implement " + TypeConversionHandler.class.getName()
                         + "; Velocity is not initialized correctly.";
 
                 log.error(err);
                 throw new VelocityException(err);
             }
-
-            conversionHandler = (TypeConversionHandler) o;
+            else
+            {
+                conversionHandler = (TypeConversionHandler)conversionHandlerInstance;
+            }
         }
     }
 

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java?rev=1854180&r1=1854179&r2=1854180&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java Fri Feb 22 23:09:21 2019
@@ -43,6 +43,7 @@ import java.io.FileOutputStream;
 import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.lang.reflect.Type;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -156,6 +157,60 @@ public class ConversionHandlerTestCase e
         assertEquals("correct", writer.toString());
     }
 
+    /* converts *everything* to string "foo" */
+    public static class MyCustomConverter implements TypeConversionHandler
+    {
+        Converter<String> myCustomConverter = new Converter<String>()
+        {
+
+            @Override
+            public String convert(Object o)
+            {
+                return "foo";
+            }
+        };
+
+        @Override
+        public boolean isExplicitlyConvertible(Type formal, Class actual, boolean possibleVarArg)
+        {
+            return true;
+        }
+
+        @Override
+        public Converter getNeededConverter(Type formal, Class actual)
+        {
+            return myCustomConverter;
+        }
+
+        @Override
+        public void addConverter(Type formal, Class actual, Converter converter)
+        {
+            throw new RuntimeException("not implemented");
+        }
+    }
+
+    public void testCustomConversionHandlerInstance()
+    {
+        RuntimeInstance ve = new RuntimeInstance();
+        ve.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
+        ve.setProperty(Velocity.RUNTIME_LOG_INSTANCE, log);
+        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
+        ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TEST_COMPARE_DIR + "/conversion");
+        ve.setProperty(RuntimeConstants.CONVERSION_HANDLER_INSTANCE, new MyCustomConverter());
+        ve.init();
+        Uberspect uberspect = ve.getUberspect();
+        assertTrue(uberspect instanceof UberspectImpl);
+        UberspectImpl ui = (UberspectImpl)uberspect;
+        TypeConversionHandler ch = ui.getConversionHandler();
+        assertTrue(ch != null);
+        assertTrue(ch instanceof MyCustomConverter);
+        VelocityContext context = new VelocityContext();
+        context.put("obj", new Obj());
+        Writer writer = new StringWriter();
+        ve.evaluate(context, writer, "test", "$obj.objectString(1.0)");
+        assertEquals("String ok: foo", writer.toString());
+    }
+
     /**
      * Test conversions
      * @param ve