You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/08/02 17:21:58 UTC

[groovy] branch GROOVY-10715 updated (c579a4620d -> 8845969a86)

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a change to branch GROOVY-10715
in repository https://gitbox.apache.org/repos/asf/groovy.git


 discard c579a4620d GROOVY-10715: Make StreamingTemplateEngine support reusing ClassLoader
     new 8845969a86 GROOVY-10715: Make StreamingTemplateEngine support reusing ClassLoader

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (c579a4620d)
            \
             N -- N -- N   refs/heads/GROOVY-10715 (8845969a86)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/groovy/text/StreamingTemplateEngineTest.groovy | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)


[groovy] 01/01: GROOVY-10715: Make StreamingTemplateEngine support reusing ClassLoader

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-10715
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 8845969a86ad587432a0f023654026b78398e9b0
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Aug 3 01:11:44 2022 +0800

    GROOVY-10715: Make StreamingTemplateEngine support reusing ClassLoader
---
 .../groovy/text/StreamingTemplateEngine.java       | 10 +++-
 .../groovy/text/StreamingTemplateEngineTest.groovy | 61 +++++++++++++++++++++-
 2 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/subprojects/groovy-templates/src/main/groovy/groovy/text/StreamingTemplateEngine.java b/subprojects/groovy-templates/src/main/groovy/groovy/text/StreamingTemplateEngine.java
index 326fc783a5..f3c78dc343 100644
--- a/subprojects/groovy-templates/src/main/groovy/groovy/text/StreamingTemplateEngine.java
+++ b/subprojects/groovy-templates/src/main/groovy/groovy/text/StreamingTemplateEngine.java
@@ -25,6 +25,7 @@ import groovy.lang.GroovyCodeSource;
 import groovy.lang.GroovyObject;
 import groovy.lang.GroovyRuntimeException;
 import groovy.lang.Writable;
+import org.apache.groovy.util.SystemUtil;
 import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.control.ErrorCollector;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
@@ -148,6 +149,9 @@ import java.util.concurrent.atomic.AtomicInteger;
 public class StreamingTemplateEngine extends TemplateEngine {
     private static final String TEMPLATE_SCRIPT_PREFIX = "StreamingTemplateScript";
     private static final AtomicInteger COUNTER = new AtomicInteger(0);
+
+    private static final boolean REUSE_CLASS_LOADER = SystemUtil.getBooleanSafe("groovy.StreamingTemplateEngine.reuseClassLoader");
+
     private final ClassLoader parentLoader;
 
     /**
@@ -594,13 +598,15 @@ public class StreamingTemplateEngine extends TemplateEngine {
         }
 
         private Closure createTemplateClosure(List<StringSection> sections, final ClassLoader parentLoader, StringBuilder target) throws ClassNotFoundException {
-            final GroovyClassLoader loader = createClassLoader(parentLoader);
+            final GroovyClassLoader loader =
+                    REUSE_CLASS_LOADER && parentLoader instanceof GroovyClassLoader
+                            ? (GroovyClassLoader) parentLoader
+                            : createClassLoader(parentLoader);
             final Class<?> groovyClass;
             try {
                 groovyClass = loader.parseClass(new GroovyCodeSource(target.toString(), TEMPLATE_SCRIPT_PREFIX + COUNTER.incrementAndGet() + ".groovy", "x"));
             } catch (MultipleCompilationErrorsException e) {
                 throw mangleMultipleCompilationErrorsException(e, sections);
-
             } catch (Exception e) {
                 throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
             }
diff --git a/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy b/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
index e62b34a5a8..08e1000e42 100644
--- a/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
+++ b/subprojects/groovy-templates/src/test/groovy/groovy/text/StreamingTemplateEngineTest.groovy
@@ -18,8 +18,10 @@
  */
 package groovy.text
 
-import org.junit.Test
 import org.junit.Before
+import org.junit.Test
+
+import java.util.concurrent.ConcurrentHashMap
 
 class StreamingTemplateEngineTest {
   TemplateEngine engine
@@ -459,4 +461,61 @@ class StreamingTemplateEngineTest {
     assert "Hi Alice" == result
   }
 
+  @Test
+  void reuseClassLoader1() {
+    final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
+    System.setProperty(reuseOption, 'true')
+    def steClass = reloadClass('groovy.text.StreamingTemplateEngine')
+    // reload class to initialize static field from the beginning
+    try {
+      GroovyClassLoader gcl = new GroovyClassLoader()
+      def engine = steClass.newInstance(gcl)
+      assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
+      assert gcl.loadedClasses.length > 0
+      def cloned = gcl.loadedClasses.clone()
+      assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
+      assert cloned == gcl.loadedClasses
+    } finally {
+      System.clearProperty(reuseOption)
+    }
+  }
+
+  @Test
+  void reuseClassLoader2() {
+    final reuseOption = 'groovy.StreamingTemplateEngine.reuseClassLoader'
+    System.setProperty(reuseOption, 'true')
+    def steClass = reloadClass('groovy.text.StreamingTemplateEngine')
+    // reload class to initialize static field from the beginning
+    try {
+      GroovyClassLoader gcl = new GroovyClassLoader()
+      def engine = steClass.newInstance(gcl)
+      assert 'Hello, Daniel' == engine.createTemplate('Hello, ${name}').make([name: 'Daniel']).toString()
+      assert gcl.loadedClasses.length > 0
+      def cloned = gcl.loadedClasses.clone()
+      engine = steClass.newInstance(gcl)
+      assert 'Hello, Paul' == engine.createTemplate('Hello, ${name}').make([name: 'Paul']).toString()
+      assert cloned == gcl.loadedClasses
+    } finally {
+      System.clearProperty(reuseOption)
+    }
+  }
+
+  private static Class reloadClass(String className) {
+    def clazz =
+            new GroovyClassLoader() {
+              private final Map<String, Class> loadedClasses = new ConcurrentHashMap<String, Class>()
+
+              @Override
+              Class loadClass(String name) {
+                if (name ==~ ('^' + className + '([$].+)?$')) {
+                  return loadedClasses.computeIfAbsent(name, n -> {
+                    def clazz = defineClass(n, GroovyClassLoader.class.getResourceAsStream('/' + n.replace('.', '/') + '.class').bytes)
+                    return clazz
+                  })
+                }
+                return super.loadClass(name)
+              }
+            }.loadClass(className)
+    return clazz
+  }
 }