Java Code Examples for groovy.lang.Script#setProperty()

The following examples show how to use groovy.lang.Script#setProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCaseRun.java    From mdw with Apache License 2.0 5 votes vote down vote up
public void run() {
    startExecution();
    try {
        if (testCase.getAsset().getExtension().equals("postman")) {
            String runnerClass = NODE_PACKAGE + ".TestRunner";
            Package pkg = PackageCache.getPackage(testCase.getPackage());
            Class<?> testRunnerClass = CompiledJavaCache.getResourceClass(runnerClass, getClass().getClassLoader(), pkg);
            Object runner = testRunnerClass.newInstance();
            Method runMethod = testRunnerClass.getMethod("run", TestCase.class);
            runMethod.invoke(runner, testCase);
            finishExecution(null);
        }
        else {
            String groovyScript = testCase.getText();
            CompilerConfiguration compilerConfig = new CompilerConfiguration();
            compilerConfig.setScriptBaseClass(TestCaseScript.class.getName());

            Binding binding = new Binding();
            binding.setVariable("testCaseRun", this);

            ClassLoader classLoader = this.getClass().getClassLoader();
            Package testPkg = PackageCache.getPackage(testCase.getPackage());
            if (testPkg != null)
                classLoader = testPkg.getClassLoader();

            GroovyShell shell = new GroovyShell(classLoader, binding, compilerConfig);
            Script gScript = shell.parse(groovyScript);
            gScript.setProperty("out", log);
            gScript.run();
            finishExecution(null);
        }
    }
    catch (Throwable ex) {
        finishExecution(ex);
    }
}
 
Example 2
Source File: PuzzleEventDispatcher.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * parses a Groovy expression
 *
 * @param buildingBlock BuildingBlock on which the expression is defined
 * @param expression expression to parse
 * @return Groovy script
 */
public Script parseExpression(PuzzleBuildingBlock buildingBlock, String expression) {
	try {
		Script script = shell.parse(expression);
		script.setProperty("buildingBlock", buildingBlock);
		return script;
	} catch (CompilationFailedException e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example 3
Source File: XmlTemplateEngine.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Writer writeTo(Writer out) {
    Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
    PrintWriter pw = new PrintWriter(out);
    scriptObject.setProperty("out", pw);
    scriptObject.run();
    pw.flush();
    return out;
}
 
Example 4
Source File: OperationGroovy.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
/**
 * inject each groovy class in the groovy operation script
 *
 * @param script : the groovy operation script
 */
private void injectScriptProperties(Script script) {
    for (Object ScriptName : injectedScripts.keySet()) {
        script.setProperty((String) ScriptName, injectedScripts.get(ScriptName));
    }
}