Search in sources :

Example 96 with Script

use of groovy.lang.Script in project groovy by apache.

the class GroovySocketServer method run.

/**
 * Runs this server. There is typically no need to call this method, as the object's constructor
 * creates a new thread and runs this object automatically.
 */
@Override
public void run() {
    try (ServerSocket serverSocket = new ServerSocket(url.getPort())) {
        while (true) {
            // Create one script per socket connection.
            // This is purposefully not caching the Script
            // so that the script source file can be changed on the fly,
            // as each connection is made to the server.
            // FIXME: Groovy has other mechanisms specifically for watching to see if source code changes.
            // We should probably be using that here.
            // See also the comment about the fact we recompile a script that can't change.
            Script script = groovy.parse(source);
            new GroovyClientConnection(script, autoOutput, serverSocket.accept());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Script(groovy.lang.Script) ServerSocket(java.net.ServerSocket) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 97 with Script

use of groovy.lang.Script in project groovy by apache.

the class TestSupport method assertScriptFile.

protected void assertScriptFile(String fileName) throws Exception {
    log.info("About to execute script: " + fileName);
    Class<?> groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
    Script script = InvokerHelper.createScript(groovyClass, new Binding());
    script.run();
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyCodeSource(groovy.lang.GroovyCodeSource) File(java.io.File)

Example 98 with Script

use of groovy.lang.Script in project groovy by apache.

the class GroovyTestSuite method loadTestSuite.

public void loadTestSuite() throws Exception {
    String fileName = System.getProperty("test", file);
    if (fileName == null) {
        throw new RuntimeException("No filename given in the 'test' system property so cannot run a Groovy unit test");
    }
    System.out.println("Compiling: " + fileName);
    Class type = compile(fileName);
    String[] args = {};
    if (!Test.class.isAssignableFrom(type) && Script.class.isAssignableFrom(type)) {
        // let's treat the script as a Test
        addTest(new ScriptTestAdapter(type, args));
    } else {
        addTestSuite(type);
    }
}
Also used : Script(groovy.lang.Script) ScriptTestAdapter(org.apache.groovy.test.ScriptTestAdapter)

Example 99 with Script

use of groovy.lang.Script in project groovy by apache.

the class InvokerHelper method createScript.

public static Script createScript(Class scriptClass, Binding context) {
    Script script;
    if (scriptClass == null) {
        script = new NullScript(context);
    } else {
        try {
            if (Script.class.isAssignableFrom(scriptClass)) {
                script = newScript(scriptClass, context);
            } else {
                // wrap call "ScriptClass.main(args)" with a Script instance
                script = new Script(context) {

                    @Override
                    public Object run() {
                        Object[] mainArgs = { new String[0] };
                        try {
                            Object args = getProperty("args");
                            if (args instanceof String[]) {
                                mainArgs[0] = args;
                            }
                        } catch (MissingPropertyException mpe) {
                        // call with empty array
                        }
                        return InvokerHelper.invokeStaticMethod(scriptClass, MAIN_METHOD_NAME, mainArgs);
                    }
                };
                MetaClass smc = getMetaClass(scriptClass);
                ((Map<?, ?>) context.getVariables()).forEach((key, value) -> {
                    String name = key.toString();
                    if (!name.startsWith("_")) {
                        // assume underscore variables are for the wrapper
                        setPropertySafe(scriptClass, smc, name, value);
                    }
                });
            }
        } catch (Exception e) {
            throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e, e);
        }
    }
    return script;
}
Also used : Script(groovy.lang.Script) MetaClass(groovy.lang.MetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SpreadMap(groovy.lang.SpreadMap) SpreadMapEvaluatingException(groovy.lang.SpreadMapEvaluatingException) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 100 with Script

use of groovy.lang.Script in project groovy by apache.

the class InvokerHelper method newScript.

public static Script newScript(Class<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException {
    Script script;
    try {
        Constructor constructor = scriptClass.getConstructor(Binding.class);
        script = (Script) constructor.newInstance(context);
    } catch (NoSuchMethodException e) {
        // Fallback for non-standard "Script" classes.
        script = (Script) scriptClass.newInstance();
        script.setBinding(context);
    }
    return script;
}
Also used : Script(groovy.lang.Script) Constructor(java.lang.reflect.Constructor)

Aggregations

Script (groovy.lang.Script)123 Binding (groovy.lang.Binding)59 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 File (java.io.File)12 Map (java.util.Map)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 Closure (groovy.lang.Closure)8 PrintWriter (java.io.PrintWriter)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 MissingMethodException (groovy.lang.MissingMethodException)7