use of groovy.lang.Binding in project groovity by disney.
the class TestCoreGroovity method testCaseSensitive.
@Test
public void testCaseSensitive() throws Exception {
Binding binding = new Binding();
String result = run("/mixedCase", binding).trim();
Assert.assertEquals("OK", result);
Exception e = null;
try {
result = run("/mixedcase", binding).trim();
} catch (Exception x) {
e = x;
}
Assert.assertNotNull("Expected IllegalArgumentException", e);
Assert.assertEquals("Expected IllegalArgumentException", ClassNotFoundException.class, e.getClass());
}
use of groovy.lang.Binding in project groovity by disney.
the class TestCoreGroovity method testImmutable.
@Test
public void testImmutable() throws Exception {
String message = "Widget(widget1, one widget)";
Binding binding = new Binding();
String result = run("/immutable", binding);
Assert.assertEquals(message, result);
}
use of groovy.lang.Binding in project groovity by disney.
the class TestCoreGroovity method testModel.
@Test
public void testModel() throws Exception {
Binding binding = new Binding();
String result = run("/model", binding).trim();
Assert.assertEquals("{\"data\":{\"active\":true,\"flags\":\"xyz\",\"greeting\":\"Hello Joe\",\"inactive\":false,\"sig\":\"YWJj\",\"time\":1000000,\"timeSeconds\":1000},\"part1\":{\"subpartA\":\"subPartA\",\"subPartB\":\"subPartB\",\"subPartC\":\"subPartC\"},\"part2\":\"wheat\",\"part3\":null}{\"count\":2,\"images\":[{\"reverse\":\"gpj.oof\",\"name\":\"foo.jpg\"},{\"reverse\":\"gpj.rab\",\"name\":\"bar.jpg\"}],\"main\":{\"reverse\":\"gpj.zyx\",\"name\":\"xyz.jpg\"}}", result);
}
use of groovy.lang.Binding in project groovity by disney.
the class GroovityServletContainer method run.
public void run(String path) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
PrintWriter pw = new PrintWriter(System.out);
try {
pw.println("RUNNING " + path);
Binding binding = new Binding();
binding.setProperty("out", pw);
Object o = groovity.run(path, binding);
pw.println();
if (o != null && !(o instanceof Closeable)) {
pw.println("DONE " + path + ", return value was " + o);
} else {
pw.println("DONE " + path);
}
} finally {
pw.flush();
}
}
use of groovy.lang.Binding in project groovity by disney.
the class Groovity method load.
/**
* This method is used to load an instance of a script without executing it, for example in order to use it as a library,
* or to defer running it until a later time; instances are always Singletons within a binding scope, so repeated
* calls to load the same script name with the same binding will return the same instance of the script.
*
* @param scriptName the path of the script to execute, e.g. /myFolder/myScript
* @param binding the groovy binding to use as global variable scope
* @return an instance of a script tied to the provided binding
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public Script load(final String scriptName, final Binding binding) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
final String varName = GROOVITY_SCRIPT_BINDING_PREFIX.concat(fixCase(scriptName));
@SuppressWarnings("rawtypes") final Map variables = binding.getVariables();
Script script = (Script) variables.get(varName);
if (script == null) {
decorate(variables);
// register place holder to catch circular dependencies
variables.put(varName, PLACEHOLDER_SCRIPT);
try {
final Binding oldThreadBinding = ScriptHelper.THREAD_BINDING.get();
if (oldThreadBinding != binding) {
// register our binding during field init so it propgates
ScriptHelper.THREAD_BINDING.set(binding);
}
try {
final Class<Script> gsc = getScriptClass(scriptName);
if (gsc != null) {
// before we create a new instance, let's look for args
ArgsResolver abd = ((GroovityClassLoader) gsc.getClassLoader()).getArgsResolver();
if (abd != null) {
// now is the time to enforce any defaults, coercion, validation, BEFORE we create a new instance
// whose field loading might depend on the arg binding decorator
abd.resolve(variables, argsLookup);
}
script = gsc.newInstance();
if (script != null) {
script.setBinding(binding);
variables.put(varName, script);
if (script instanceof Loadable) {
((Loadable) script).load();
}
}
}
} finally {
if (oldThreadBinding == null) {
ScriptHelper.THREAD_BINDING.remove();
} else if (oldThreadBinding != binding) {
ScriptHelper.THREAD_BINDING.set(oldThreadBinding);
}
}
} finally {
if (script == null) {
variables.remove(varName);
}
}
}
if (script == null) {
throw new ClassNotFoundException("No grvt found for " + scriptName);
}
if (script == PLACEHOLDER_SCRIPT) {
throw new InstantiationException("Circular load dependency found leading back to " + scriptName);
}
return script;
}
Aggregations