Search in sources :

Example 46 with Binding

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();
    }
}
Also used : Binding(groovy.lang.Binding) JobDataMap(org.quartz.JobDataMap) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) JobExecutionException(org.quartz.JobExecutionException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyShell(groovy.lang.GroovyShell) JobExecutionException(org.quartz.JobExecutionException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 47 with Binding

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"));
}
Also used : Path(org.apache.hadoop.fs.Path) Binding(groovy.lang.Binding) FsPermission(org.apache.hadoop.fs.permission.FsPermission) Matchers.containsString(org.hamcrest.Matchers.containsString) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) File(java.io.File) URL(java.net.URL) GroovyShell(groovy.lang.GroovyShell)

Example 48 with Binding

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();
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 49 with Binding

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));
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 50 with Binding

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();
    }
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

Aggregations

Binding (groovy.lang.Binding)213 GroovyShell (groovy.lang.GroovyShell)76 Script (groovy.lang.Script)55 Test (org.junit.Test)41 IOException (java.io.IOException)29 File (java.io.File)24 HashMap (java.util.HashMap)23 Closure (groovy.lang.Closure)22 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)22 Map (java.util.Map)20 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 StringWriter (java.io.StringWriter)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InputStreamReader (java.io.InputStreamReader)9 Writer (java.io.Writer)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9