Author: adrianc
Date: Wed Mar 24 20:22:38 2010
New Revision: 927172
URL:
http://svn.apache.org/viewvc?rev=927172&view=revLog:
Simplified the getBinding method in GroovyUtil.java, plus added a JavaDoc to describe what it does.
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=927172&r1=927171&r2=927172&view=diff==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Wed Mar 24 20:22:38 2010
@@ -28,6 +28,8 @@ import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
+import javolution.util.FastMap;
+
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.ofbiz.base.location.FlexibleLocation;
@@ -60,19 +62,25 @@ public class GroovyUtil {
return new GroovyClassLoader().parseClass(UtilIO.readString(in), location);
}
+ /** Returns a <code>Binding</code> instance initialized with the
+ * variables contained in <code>context</code>. If <code>context</code>
+ * is <code>null</code>, an empty <code>Binding</code> is returned.
+ * <p>The <code>context Map</code> is added to the <code>Binding</code>
+ * as a variable called "context" so that variables can be passed
+ * back to the caller. Any variables that are created in the script
+ * are lost when the script ends unless they are copied to the
+ * "context" <code>Map</code>.</p>
+ *
+ * @param context A <code>Map</code> containing initial variables
+ * @return A <code>Binding</code> instance
+ */
public static Binding getBinding(Map<String, ? extends Object> context) {
- Binding binding = new Binding();
+ Map<String, Object> vars = FastMap.newInstance();
if (context != null) {
- Set<String> keySet = context.keySet();
- for (Object key : keySet) {
- binding.setVariable((String) key, context.get(key));
- }
-
- // include the context itself in for easier access in the scripts
- binding.setVariable("context", context);
+ vars.putAll(context);
+ vars.put("context", context);
}
-
- return binding;
+ return new Binding(vars);
}
/**