Search in sources :

Example 21 with Binding

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());
}
Also used : Binding(groovy.lang.Binding) URISyntaxException(java.net.URISyntaxException) ArgsException(com.disney.groovity.ArgsException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 22 with Binding

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);
}
Also used : Binding(groovy.lang.Binding) Test(org.junit.Test)

Example 23 with Binding

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);
}
Also used : Binding(groovy.lang.Binding) Test(org.junit.Test)

Example 24 with Binding

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();
    }
}
Also used : Binding(groovy.lang.Binding) Closeable(java.io.Closeable) PrintWriter(java.io.PrintWriter)

Example 25 with Binding

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;
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

Binding (groovy.lang.Binding)219 GroovyShell (groovy.lang.GroovyShell)77 Script (groovy.lang.Script)58 Test (org.junit.Test)39 IOException (java.io.IOException)30 File (java.io.File)24 HashMap (java.util.HashMap)24 Closure (groovy.lang.Closure)23 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)23 Map (java.util.Map)21 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 InputStreamReader (java.io.InputStreamReader)10 StringWriter (java.io.StringWriter)10 Writer (java.io.Writer)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9