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