svn commit: r1299636 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/ScriptUtil.java service/src/org/ofbiz/service/engine/ScriptEngine.java webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1299636 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/ScriptUtil.java service/src/org/ofbiz/service/engine/ScriptEngine.java webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java

adrianc
Author: adrianc
Date: Mon Mar 12 11:41:03 2012
New Revision: 1299636

URL: http://svn.apache.org/viewvc?rev=1299636&view=rev
Log:
JSR-223 bug fix, added String constants for commonly used binding keys.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/ScriptEngine.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java?rev=1299636&r1=1299635&r2=1299636&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java Mon Mar 12 11:41:03 2012
@@ -24,6 +24,7 @@ import java.io.FileReader;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -55,6 +56,12 @@ import org.ofbiz.base.util.cache.UtilCac
 public final class ScriptUtil {
 
     public static final String module = ScriptUtil.class.getName();
+    /** The screen widget context map bindings key. */
+    public static final String WIDGET_CONTEXT_KEY = "widget";
+    /** The service/servlet/request parameters map bindings key. */
+    public static final String PARAMETERS_KEY = "parameters";
+    /** The result map bindings key. */
+    public static final String RESULT_KEY = "result";
     private static final UtilCache<String, CompiledScript> parsedScripts = UtilCache.createUtilCache("script.ParsedScripts", 0, 0, false);
     private static final Object[] EMPTY_ARGS = {};
 
@@ -176,6 +183,7 @@ public final class ScriptUtil {
      */
     public static ScriptContext createScriptContext(Map<String, Object> context) {
         Assert.notNull("context", context);
+        context.put(WIDGET_CONTEXT_KEY, context);
         context.put("context", context);
         ScriptContext scriptContext = new SimpleScriptContext();
         Bindings bindings = new SimpleBindings(context);
@@ -195,9 +203,10 @@ public final class ScriptUtil {
      */
     public static ScriptContext createScriptContext(Map<String, Object> context, Set<String> protectedKeys) {
         Assert.notNull("context", context, "protectedKeys", protectedKeys);
+        context.put(WIDGET_CONTEXT_KEY, context);
         context.put("context", context);
         ScriptContext scriptContext = new SimpleScriptContext();
-        Bindings bindings = new ProtectedBindings(context, protectedKeys);
+        Bindings bindings = new ProtectedBindings(context, Collections.unmodifiableSet(protectedKeys));
         scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
         return scriptContext;
     }
@@ -407,7 +416,9 @@ public final class ScriptUtil {
         public Object put(String key, Object value) {
             Assert.notNull("key", key);
             if (protectedKeys.contains(key)) {
-                throw new UnsupportedOperationException();
+                UnsupportedOperationException e = new UnsupportedOperationException("Variable " + key + " is read-only");
+                Debug.logWarning(e, module);
+                throw e;
             }
             return bindings.put(key, value);
         }
@@ -421,7 +432,9 @@ public final class ScriptUtil {
         }
         public Object remove(Object key) {
             if (protectedKeys.contains(key)) {
-                throw new UnsupportedOperationException();
+                UnsupportedOperationException e = new UnsupportedOperationException("Variable " + key + " is read-only");
+                Debug.logWarning(e, module);
+                throw e;
             }
             return bindings.remove(key);
         }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/ScriptEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/ScriptEngine.java?rev=1299636&r1=1299635&r2=1299636&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/ScriptEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/ScriptEngine.java Mon Mar 12 11:41:03 2012
@@ -57,7 +57,9 @@ public final class ScriptEngine extends
 
     private static Set<String> createProtectedKeys() {
         Set<String> newSet = new HashSet<String>();
-        newSet.add("parameters");
+        /* Commenting out for now because some scripts write to the parameters Map - which should not be allowed.
+        newSet.add(ScriptUtil.PARAMETERS_KEY);
+        */
         newSet.add("dctx");
         newSet.add("dispatcher");
         newSet.add("delegator");
@@ -73,7 +75,7 @@ public final class ScriptEngine extends
         Assert.notNull("localName", localName, "modelService.location", modelService.location, "context", context);
         Map<String, Object> params = FastMap.newInstance();
         params.putAll(context);
-        context.put("parameters", params);
+        context.put(ScriptUtil.PARAMETERS_KEY, params);
         DispatchContext dctx = dispatcher.getLocalContext(localName);
         context.put("dctx", dctx);
         context.put("dispatcher", dctx.getDispatcher());
@@ -82,7 +84,7 @@ public final class ScriptEngine extends
             ScriptContext scriptContext = ScriptUtil.createScriptContext(context, protectedKeys);
             Object resultObj = ScriptUtil.executeScript(getLocation(modelService), modelService.invoke, scriptContext, new Object[] { context });
             if (resultObj == null) {
-                resultObj = scriptContext.getAttribute("result");
+                resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
             }
             if (resultObj != null && resultObj instanceof Map<?, ?>) {
                 return cast(resultObj);

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java?rev=1299636&r1=1299635&r2=1299636&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ScriptEventHandler.java Mon Mar 12 11:41:03 2012
@@ -71,7 +71,9 @@ public final class ScriptEventHandler im
         newSet.add("locale");
         newSet.add("timeZone");
         newSet.add("userLogin");
-        newSet.add("parameters");
+        /* Commenting out for now because some scripts write to the parameters Map - which should not be allowed.
+        newSet.add(ScriptUtil.PARAMETERS_KEY);
+        */
         return Collections.unmodifiableSet(newSet);
     }
 
@@ -93,13 +95,13 @@ public final class ScriptEventHandler im
             context.put("locale", UtilHttp.getLocale(request));
             context.put("timeZone", UtilHttp.getTimeZone(request));
             context.put("userLogin", session.getAttribute("userLogin"));
-            context.put("parameters", UtilHttp.getCombinedMap(request, UtilMisc.toSet("delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")));
+            context.put(ScriptUtil.PARAMETERS_KEY, UtilHttp.getCombinedMap(request, UtilMisc.toSet("delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")));
             Object result = null;
             try {
                 ScriptContext scriptContext = ScriptUtil.createScriptContext(context, protectedKeys);
                 result = ScriptUtil.executeScript(event.path, event.invoke, scriptContext, new Object[] { context });
                 if (result == null) {
-                    result = scriptContext.getAttribute("result");
+                    result = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
                 }
             } catch (Exception e) {
                 Debug.logWarning(e, "Error running event " + event.path + ": ", module);