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));
}
}
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);
}
}
}
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();
}
}
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");
}
}
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);
}
}
Aggregations