use of groovy.lang.Binding in project xwiki-platform by xwiki.
the class GroovyJob method executeJob.
/**
* Executes the Groovy script passed in the <code>script</code> property of the
* {@link com.xpn.xwiki.plugin.scheduler.SchedulerPlugin#XWIKI_JOB_CLASS} object extracted from the XWiki context
* passed in the Quartz's Job execution context. The XWiki Task object is looked for in the current document that
* was set in the context at the time the Job was scheduled.
*
* @param jobContext the Quartz execution context containing the XWiki context from which the script to execute is
* retrieved
* @throws JobExecutionException if the script fails to execute or if the user didn't have programming rights when
* the Job was scheduled
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
protected void executeJob(JobExecutionContext jobContext) throws JobExecutionException {
try {
JobDataMap data = jobContext.getJobDetail().getJobDataMap();
// Get the job XObject to be executed
BaseObject object = (BaseObject) data.get("xjob");
// Force context document
XWikiDocument jobDocument = getXWikiContext().getWiki().getDocument(object.getName(), getXWikiContext());
getXWikiContext().setDoc(jobDocument);
getXWikiContext().put("sdoc", jobDocument);
if (getXWikiContext().getWiki().getRightService().hasProgrammingRights(getXWikiContext())) {
// Make the Job execution data available to the Groovy script
Binding binding = new Binding(data.getWrappedMap());
// Set the right instance of XWikiContext
binding.setProperty("context", getXWikiContext());
binding.setProperty("xcontext", getXWikiContext());
data.put("xwiki", new com.xpn.xwiki.api.XWiki(getXWikiContext().getWiki(), getXWikiContext()));
// Execute the Groovy script
GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(), binding);
shell.evaluate(object.getLargeStringValue("script"));
} else {
throw new JobExecutionException("The user [" + getXWikiContext().getUser() + "] didn't have " + "programming rights when the job [" + jobContext.getJobDetail().getKey() + "] was scheduled.");
}
} catch (CompilationFailedException e) {
throw new JobExecutionException("Failed to execute script for job [" + jobContext.getJobDetail().getKey() + "]", e, true);
} catch (Exception e) {
e.printStackTrace();
}
}
use of groovy.lang.Binding in project knox by apache.
the class ShellTest method testPutGetScript.
private void testPutGetScript(String script) throws IOException, URISyntaxException {
setupLogging();
DistributedFileSystem fileSystem = miniDFSCluster.getFileSystem();
Path dir = new Path("/user/guest/example");
fileSystem.delete(dir, true);
fileSystem.mkdirs(dir, new FsPermission("777"));
fileSystem.setOwner(dir, "guest", "users");
Binding binding = new Binding();
binding.setProperty("gateway", driver.getClusterUrl());
URL readme = driver.getResourceUrl("README");
File file = new File(readme.toURI());
System.out.println(file.exists());
binding.setProperty("file", file.getAbsolutePath());
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(driver.getResourceUrl(script).toURI());
String status = (String) binding.getProperty("status");
assertNotNull(status);
System.out.println(status);
String fetchedFile = (String) binding.getProperty("fetchedFile");
assertNotNull(fetchedFile);
System.out.println(fetchedFile);
assertThat(fetchedFile, containsString("README"));
}
use of groovy.lang.Binding in project spf4j by zolyfarkas.
the class ZelBenchmark method testGroovy.
@Benchmark
public Object testGroovy() {
Binding binding = new Binding();
binding.setVariable("a", 3);
binding.setVariable("b", 2);
binding.setVariable("c", " ");
binding.setVariable("d", "bla");
Script script = GROOVY_PROG.get();
script.setBinding(binding);
return script.run();
}
use of groovy.lang.Binding in project herddb by diennea.
the class HerdDBCLI method executeScript.
private static void executeScript(final Connection connection, final HerdDBDataSource datasource, final Statement statement, String script) throws IOException, CompilationFailedException {
Map<String, Object> variables = new HashMap<>();
variables.put("connection", connection);
variables.put("datasource", datasource);
variables.put("statement", statement);
GroovyShell shell = new GroovyShell(new Binding(variables));
shell.evaluate(new File(script));
}
use of groovy.lang.Binding in project maven-scm by apache.
the class IntegrityTagCommand method evalGroovyExpression.
public String evalGroovyExpression(String expression) {
Binding binding = new Binding();
binding.setVariable("env", System.getenv());
binding.setVariable("sys", System.getProperties());
CompilerConfiguration config = new CompilerConfiguration();
GroovyShell shell = new GroovyShell(binding, config);
Object result = shell.evaluate("return \"" + expression + "\"");
if (result == null) {
return "";
} else {
return result.toString().trim();
}
}
Aggregations