Search in sources :

Example 11 with Script

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

the class Groovy2365Bug method testDeadlock.

public void testDeadlock() {
    String path = createData();
    try {
        System.out.println("Test started");
        for (int i = 0; i != 100; ++i) {
            System.out.println("Iter " + i);
            final GroovyClassLoader groovyLoader = new GroovyClassLoader();
            groovyLoader.addClasspath(path);
            Class _script1Class = null;
            try {
                _script1Class = groovyLoader.loadClass("Script1", true, true);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            final Class script1Class = _script1Class;
            // setup two threads to try a deadlock
            // thread one: newInstance script foo
            final boolean[] completed = new boolean[2];
            Thread thread1 = new Thread() {

                public void run() {
                    try {
                        Script script = (Script) script1Class.newInstance();
                        script.run();
                        completed[0] = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            Thread thread2 = new Thread() {

                public void run() {
                    try {
                        Class cls = groovyLoader.loadClass("Script2", true, true);
                        Script script = (Script) cls.newInstance();
                        script.run();
                        completed[1] = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            // let's see if we get a deadlock
            thread2.start();
            thread1.start();
            try {
                thread1.join(5000);
                thread2.join(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            assertTrue("Potentially deadlock", completed[0] && completed[1]);
        }
    } finally {
        ResourceGroovyMethods.deleteDir(new File(path));
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Script(groovy.lang.Script) File(java.io.File)

Example 12 with Script

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

the class GroovyMain method processFiles.

/**
     * Process the input files.
     */
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    setupContextClassLoader(groovy);
    Script s = groovy.parse(getScriptSource(isScriptFile, script));
    if (args.isEmpty()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(System.out);
        try {
            processReader(s, reader, writer);
        } finally {
            reader.close();
            writer.close();
        }
    } else {
        Iterator i = args.iterator();
        while (i.hasNext()) {
            String filename = (String) i.next();
            //TODO: These are the arguments for -p and -i.  Why are we searching using Groovy script extensions?
            // Where is this documented?
            File file = huntForTheScriptFile(filename);
            processFile(s, file);
        }
    }
}
Also used : Script(groovy.lang.Script) Iterator(java.util.Iterator) GroovyShell(groovy.lang.GroovyShell)

Example 13 with Script

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

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. 
    */
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 14 with Script

use of groovy.lang.Script 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 15 with Script

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

the class CachingGroovyEngine method eval.

/**
     * Evaluate an expression.
     */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        Class scriptClass = evalScripts.get(script);
        if (scriptClass == null) {
            scriptClass = loader.parseClass(script.toString(), source);
            evalScripts.put(script, scriptClass);
        } else {
            LOG.fine("eval() - Using cached script...");
        }
        //can't cache the script because the context may be different.
        //but don't bother loading parsing the class again
        Script s = InvokerHelper.createScript(scriptClass, context);
        return s.run();
    } catch (Exception e) {
        throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
    }
}
Also used : Script(groovy.lang.Script) BSFException(org.apache.bsf.BSFException) BSFException(org.apache.bsf.BSFException)

Aggregations

Script (groovy.lang.Script)37 Binding (groovy.lang.Binding)19 IOException (java.io.IOException)12 GroovyShell (groovy.lang.GroovyShell)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)8 MissingMethodException (groovy.lang.MissingMethodException)6 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)5 PrintWriter (java.io.PrintWriter)5 Closure (groovy.lang.Closure)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 MetaClass (groovy.lang.MetaClass)4 MissingPropertyException (groovy.lang.MissingPropertyException)4 File (java.io.File)4 UrlMapping (grails.web.mapping.UrlMapping)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 Tuple (groovy.lang.Tuple)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3