svn commit: r1300954 - /ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java

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

svn commit: r1300954 - /ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java

adrianc
Author: adrianc
Date: Thu Mar 15 12:26:50 2012
New Revision: 1300954

URL: http://svn.apache.org/viewvc?rev=1300954&view=rev
Log:
Simplified ContextHelper.java code by adding auto-creation of some commonly-used bindings.

Modified:
    ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java

Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java?rev=1300954&r1=1300953&r2=1300954&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/scripting/ContextHelper.java Thu Mar 15 12:26:50 2012
@@ -18,7 +18,6 @@
  *******************************************************************************/
 package org.ofbiz.common.scripting;
 
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
@@ -29,6 +28,8 @@ import javax.script.ScriptEngine;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import javolution.util.FastMap;
+
 import org.ofbiz.base.util.Assert;
 import org.ofbiz.base.util.ScriptUtil;
 import org.ofbiz.base.util.UtilGenerics;
@@ -120,12 +121,16 @@ public final class ContextHelper {
     }
 
     public Object getParameter(String key) {
-        Map<?, ?> parameters = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.PARAMETERS_KEY));
-        return parameters != null ? parameters.get(key) : null;
+        return getParameters().get(key);
     }
 
     public Map<String, Object> getParameters() {
-        return UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.PARAMETERS_KEY));
+        Map<String, Object> parameters = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.PARAMETERS_KEY));
+        if (parameters == null) {
+            parameters = FastMap.newInstance();
+            this.context.setAttribute(ScriptUtil.PARAMETERS_KEY, parameters, ScriptContext.ENGINE_SCOPE);
+        }
+        return parameters;
     }
 
     public HttpServletRequest getRequest() {
@@ -137,12 +142,17 @@ public final class ContextHelper {
     }
 
     public Object getResult(String key) {
-        Map<?, ?> results = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.RESULT_KEY));
+        Map<?, ?> results = getResults();
         return results != null ? results.get(key) : null;
     }
 
     public Map<String, Object> getResults() {
-        return UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.RESULT_KEY));
+        Map<String, Object> results = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.RESULT_KEY));
+        if (results == null) {
+            results = FastMap.newInstance();
+            this.context.setAttribute(ScriptUtil.RESULT_KEY, results, ScriptContext.ENGINE_SCOPE);
+        }
+        return results;
     }
 
     public String getScriptName() {
@@ -206,30 +216,15 @@ public final class ContextHelper {
     }
 
     public void putParameter(String key, Object value) {
-        Map<String, Object> parameters = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.PARAMETERS_KEY));
-        if (parameters == null) {
-            parameters = new HashMap<String, Object>();
-            this.context.setAttribute(ScriptUtil.PARAMETERS_KEY, parameters, ScriptContext.ENGINE_SCOPE);
-        }
-        parameters.put(key, value);
+        getParameters().put(key, value);
     }
 
     public void putResult(String key, Object value) {
-        Map<String, Object> results = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.RESULT_KEY));
-        if (results == null) {
-            results = new HashMap<String, Object>();
-            this.context.setAttribute(ScriptUtil.RESULT_KEY, results, ScriptContext.ENGINE_SCOPE);
-        }
-        results.put(key, value);
+        getResults().put(key, value);
     }
 
     public void putResults(Map<String, Object> results) {
-        Map<String, Object> existingResults = UtilGenerics.checkMap(this.context.getAttribute(ScriptUtil.RESULT_KEY));
-        if (existingResults == null) {
-            existingResults = new HashMap<String, Object>();
-            this.context.setAttribute(ScriptUtil.RESULT_KEY, results, ScriptContext.ENGINE_SCOPE);
-        }
-        existingResults.putAll(results);
+        getResults().putAll(results);
     }
 
     public Object removeBinding(String key) {