Search in sources :

Example 76 with Binding

use of groovy.lang.Binding in project groovy-core by groovy.

the class FileSystemCompiler method generateCompilerConfigurationFromOptions.

public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli) throws IOException {
    // 
    // Setup the configuration data
    CompilerConfiguration configuration = new CompilerConfiguration();
    if (cli.hasOption("classpath")) {
        configuration.setClasspath(cli.getOptionValue("classpath"));
    }
    if (cli.hasOption('d')) {
        configuration.setTargetDirectory(cli.getOptionValue('d'));
    }
    if (cli.hasOption("encoding")) {
        configuration.setSourceEncoding(cli.getOptionValue("encoding"));
    }
    if (cli.hasOption("basescript")) {
        configuration.setScriptBaseClass(cli.getOptionValue("basescript"));
    }
    // joint compilation parameters
    if (cli.hasOption('j')) {
        Map<String, Object> compilerOptions = new HashMap<String, Object>();
        String[] opts = cli.getOptionValues("J");
        compilerOptions.put("namedValues", opts);
        opts = cli.getOptionValues("F");
        compilerOptions.put("flags", opts);
        configuration.setJointCompilationOptions(compilerOptions);
    }
    if (cli.hasOption("indy")) {
        configuration.getOptimizationOptions().put("int", false);
        configuration.getOptimizationOptions().put("indy", true);
    }
    if (cli.hasOption("configscript")) {
        String path = cli.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        Binding binding = new Binding();
        binding.setVariable("configuration", configuration);
        CompilerConfiguration configuratorConfig = new CompilerConfiguration();
        ImportCustomizer customizer = new ImportCustomizer();
        customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
        configuratorConfig.addCompilationCustomizers(customizer);
        GroovyShell shell = new GroovyShell(binding, configuratorConfig);
        shell.evaluate(groovyConfigurator);
    }
    return configuration;
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell)

Example 77 with Binding

use of groovy.lang.Binding in project groovy-core by groovy.

the class SecurityTestSupport method executeScript.

protected void executeScript(Class scriptClass, Permission missingPermission) {
    try {
        Script script = InvokerHelper.createScript(scriptClass, new Binding());
        script.run();
    // InvokerHelper.runScript(scriptClass, null);
    } catch (AccessControlException ace) {
        if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
            return;
        } else {
            fail(ace.toString());
        }
    }
    if (missingPermission != null) {
        fail("Should catch an AccessControlException");
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script)

Example 78 with Binding

use of groovy.lang.Binding in project groovy-core by groovy.

the class PlatformLineWriterTest method testPlatformLineWriter.

public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
    String LS = System.getProperty("line.separator");
    Binding binding = new Binding();
    binding.setVariable("first", "Tom");
    binding.setVariable("last", "Adams");
    StringWriter stringWriter = new StringWriter();
    Writer platformWriter = new PlatformLineWriter(stringWriter);
    GroovyShell shell = new GroovyShell(binding);
    platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
    stringWriter = new StringWriter();
    platformWriter = new PlatformLineWriter(stringWriter);
    platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
Also used : Binding(groovy.lang.Binding) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) GroovyShell(groovy.lang.GroovyShell)

Example 79 with Binding

use of groovy.lang.Binding in project groovy-core by groovy.

the class Groovy1567_Bug method testGroovyScriptEngineVsGroovyShell.

public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
    // @todo refactor this path
    File currentDir = new File("./src/test/groovy/bugs");
    String file = "bug1567_script.groovy";
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    String[] test = null;
    Object result = shell.run(new File(currentDir, file), test);
    String[] roots = new String[] { currentDir.getAbsolutePath() };
    GroovyScriptEngine gse = new GroovyScriptEngine(roots);
    binding = new Binding();
    // a MME was ensued here stating no 't.start()' was available
    // in the script
    gse.run(file, binding);
}
Also used : Binding(groovy.lang.Binding) GroovyScriptEngine(groovy.util.GroovyScriptEngine) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 80 with Binding

use of groovy.lang.Binding in project groovy-core by groovy.

the class ScriptLauncher method run.

public void run() {
    final long id = Thread.currentThread().getId();
    // run the script numIter times
    for (int i = 0; i < numIter; i++) {
        Builder builder = new Builder();
        Binding binding = new Binding();
        binding.setVariable("builder", builder);
        script = InvokerHelper.createScript(scriptClass, binding);
        script.run();
    }
    latch.countDown();
}
Also used : Binding(groovy.lang.Binding)

Aggregations

Binding (groovy.lang.Binding)213 GroovyShell (groovy.lang.GroovyShell)76 Script (groovy.lang.Script)55 Test (org.junit.Test)41 IOException (java.io.IOException)29 File (java.io.File)24 HashMap (java.util.HashMap)23 Closure (groovy.lang.Closure)22 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)22 Map (java.util.Map)20 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 StringWriter (java.io.StringWriter)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InputStreamReader (java.io.InputStreamReader)9 Writer (java.io.Writer)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9