You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2023/02/08 15:32:49 UTC

[commons-jexl] branch master updated: JEXL-392: tidy module processing; - javadoc, release notes and changes;

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

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new 5ef24c71 JEXL-392: tidy module processing; - javadoc, release notes and changes;
5ef24c71 is described below

commit 5ef24c71895dfd90752df4bbba31e18d18771094
Author: henrib <he...@apache.org>
AuthorDate: Wed Feb 8 16:32:44 2023 +0100

    JEXL-392: tidy module processing;
    - javadoc, release notes and changes;
---
 RELEASE-NOTES.txt                                  |  1 +
 src/changes/changes.xml                            |  3 ++
 .../org/apache/commons/jexl3/internal/Engine.java  | 13 ++++--
 .../commons/jexl3/ComposePermissionsTest.java      |  2 +-
 .../java/org/apache/commons/jexl3/PragmaTest.java  | 49 +++++++++++++++-------
 5 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 7a5ae01f..1021cb07 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -40,6 +40,7 @@ the skill set required to write scripts.
 
 New Features in 3.3:
 ====================
+* JEXL-392:     Enable namespace declaration based on scripts
 * JEXL-391:     Improve in/=~ operator when arguments are arrays and collections
 * JEXL-390:     Pragmas should not be statements
 * JEXL-389:     Improve parsing timings
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ed1f06bc..51ade2f1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,6 +27,9 @@
     <body>
         <release version="3.3" date="YYYY-MM-DD">
             <!-- ADD -->
+            <action dev="henrib" type="add" issue="JEXL-392">
+                Enable namespace declaration based on scripts
+            </action>
             <action dev="henrib" type="add" issue="JEXL-391">
                 Improve in/=~ operator when arguments are arrays and collections
             </action>
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Engine.java b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
index 2c3467ad..fba342d8 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Engine.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
@@ -522,9 +522,16 @@ public class Engine extends JexlEngine {
                     logger.warn(module + ": unable to define module from " + value);
                 } else {
                     final String moduleSrc = o.toString();
-                    final Object functor = context instanceof JexlContext.ModuleProcessor
-                            ? ((JexlContext.ModuleProcessor) context).processModule(this, info, module, moduleSrc)
-                            : createExpression(info, moduleSrc).evaluate(context);
+                    final Object functor;
+                    if (context instanceof JexlContext.ModuleProcessor) {
+                        final JexlContext.ModuleProcessor processor = (JexlContext.ModuleProcessor) context;
+                        functor = processor.processModule(this, info, module, moduleSrc);
+                    } else {
+                        final Object moduleObject = createExpression(info, moduleSrc).evaluate(context);
+                        functor =  moduleObject instanceof JexlScript
+                                ? ((JexlScript) moduleObject).execute(context)
+                                : moduleObject;
+                    }
                     if (functor != null) {
                         ns.put(module, functor);
                     } else {
diff --git a/src/test/java/org/apache/commons/jexl3/ComposePermissionsTest.java b/src/test/java/org/apache/commons/jexl3/ComposePermissionsTest.java
index 5f1a66b6..6f4de5e3 100644
--- a/src/test/java/org/apache/commons/jexl3/ComposePermissionsTest.java
+++ b/src/test/java/org/apache/commons/jexl3/ComposePermissionsTest.java
@@ -69,7 +69,7 @@ public class ComposePermissionsTest extends JexlTestCase {
       Assert.assertEquals("pageInfo", xproperty.getProperty());
     }
 
-    // will not fail since gson package is not explicitely allowed
+    // will not fail since gson objects
     j1 = createEngine(false, JexlPermissions.RESTRICTED);
     s1 = j1.createScript("json.pageInfo.pagePic", "json");
     Object r1 = s1.execute(null, json);
diff --git a/src/test/java/org/apache/commons/jexl3/PragmaTest.java b/src/test/java/org/apache/commons/jexl3/PragmaTest.java
index 40ba6ea8..e8519fb5 100644
--- a/src/test/java/org/apache/commons/jexl3/PragmaTest.java
+++ b/src/test/java/org/apache/commons/jexl3/PragmaTest.java
@@ -118,23 +118,28 @@ public class PragmaTest extends JexlTestCase {
         }
     }
 
-    public static class ModuleContext extends MapContext implements JexlContext.ModuleProcessor {
-        private final ConcurrentMap<String, Object> modules;
-        private final Map<String, JexlScript> sources;
-        private final AtomicInteger count = new AtomicInteger(0);
-
-        ModuleContext(Map<String, JexlScript> sources , ConcurrentMap<String, Object> modules) {
-            this.sources = sources;
-            this.modules = modules;
-        }
+    public static class ModuleContext extends MapContext {
+        protected final Map<String, JexlScript> sources = new TreeMap<>();
 
+        ModuleContext() {  }
         public Object script(String name) {
             return sources.get(name);
         }
 
+        void script(String name, JexlScript script) { sources.put(name, script); }
+    }
+
+    public static class CachingModuleContext extends ModuleContext implements JexlContext.ModuleProcessor {
+        private final ConcurrentMap<String, Object> modules = new ConcurrentHashMap<>();
+        private final AtomicInteger count = new AtomicInteger(0);
+
         public int getCountCompute() {
             return count.get();
         }
+
+        CachingModuleContext() {
+        }
+
         @Override
         public Object processModule(JexlEngine engine, JexlInfo info, String name, final String body) {
             if (body.isEmpty()) {
@@ -152,24 +157,36 @@ public class PragmaTest extends JexlTestCase {
         }
     }
 
-    @Test public void testPragmaModule() {
-        Map<String, JexlScript> msrcs = new TreeMap<>();
-        msrcs.put("module0", JEXL.createScript("function f42(x) { 42 + x; } function f43(x) { 43 + x; }; { 'f42' : f42, 'f43' : f43 }"));
+    @Test public void testPragmaModuleNoCache() {
+        ModuleContext ctxt = new ModuleContext();
+        runPragmaModule(ctxt, null);
+    }
+    @Test public void testPragmaModuleCache() {
+        CachingModuleContext ctxt = new CachingModuleContext();
+        runPragmaModule(ctxt, ctxt);
+    }
+    void runPragmaModule(ModuleContext ctxt, CachingModuleContext cmCtxt) {
+        ctxt.script("module0", JEXL.createScript("function f42(x) { 42 + x; } function f43(x) { 43 + x; }; { 'f42' : f42, 'f43' : f43 }"));
         ConcurrentMap<String, Object> modules = new ConcurrentHashMap<>();
-        ModuleContext ctxt = new ModuleContext(msrcs, modules);
         JexlScript script ;
         Object result ;
         script = JEXL.createScript("#pragma jexl.module.m0 \"script('module0')\"\n m0:f42(10);");
         result = script.execute(ctxt);
         Assert.assertEquals(52, result);
-        Assert.assertEquals(1, ctxt.getCountCompute());
+        if (cmCtxt != null) {
+            Assert.assertEquals(1, cmCtxt.getCountCompute());
+        }
         result = script.execute(ctxt);
         Assert.assertEquals(52, result);
-        Assert.assertEquals(1, ctxt.getCountCompute());
+        if (cmCtxt != null) {
+            Assert.assertEquals(1, cmCtxt.getCountCompute());
+        }
         script = JEXL.createScript("#pragma jexl.module.m0 \"script('module0')\"\n m0:f43(10);");
         result = script.execute(ctxt);
         Assert.assertEquals(53, result);
-        Assert.assertEquals(1, ctxt.getCountCompute());
+        if (cmCtxt != null) {
+            Assert.assertEquals(1, cmCtxt.getCountCompute());
+        }
         try {
             script = JEXL.createScript("#pragma jexl.module.m0 ''\n#pragma jexl.module.m0 \"fubar('module0')\"\n m0:f43(10);");
             result = script.execute(ctxt);