Search in sources :

Example 61 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class Groovy method execGroovy.

/**
     * Exec the statement.
     *
     * @param txt the groovy source to exec
     * @param out not used?
     */
protected void execGroovy(final String txt, final PrintStream out) {
    log.debug("execGroovy()");
    // Check and ignore empty statements
    if ("".equals(txt.trim())) {
        return;
    }
    log.verbose("Script: " + txt);
    if (classpath != null) {
        log.debug("Explicit Classpath: " + classpath.toString());
    }
    if (fork) {
        log.debug("Using fork mode");
        try {
            createClasspathParts();
            createNewArgs(txt);
            super.setFork(fork);
            super.setClassname(useGroovyShell ? "groovy.lang.GroovyShell" : "org.codehaus.groovy.ant.Groovy");
            configureCompiler();
            super.execute();
        } catch (Exception e) {
            StringWriter writer = new StringWriter();
            new ErrorReporter(e, false).write(new PrintWriter(writer));
            String message = writer.toString();
            throw new BuildException("Script Failed: " + message, e, getLocation());
        }
        return;
    }
    Object mavenPom = null;
    final Project project = getProject();
    final ClassLoader baseClassLoader;
    ClassLoader savedLoader = null;
    final Thread thread = Thread.currentThread();
    boolean maven = "org.apache.commons.grant.GrantProject".equals(project.getClass().getName());
    // treat the case Ant is run through Maven, and
    if (maven) {
        if (contextClassLoader) {
            throw new BuildException("Using setContextClassLoader not permitted when using Maven.", getLocation());
        }
        try {
            final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
            final Field contextField = propsHandler.getClass().getDeclaredField("context");
            contextField.setAccessible(true);
            final Object context = contextField.get(propsHandler);
            mavenPom = InvokerHelper.invokeMethod(context, "getProject", EMPTY_OBJECT_ARRAY);
        } catch (Exception e) {
            throw new BuildException("Impossible to retrieve Maven's Ant project: " + e.getMessage(), getLocation());
        }
        // load groovy into "root.maven" classloader instead of "root" so that
        // groovy script can access Maven classes
        baseClassLoader = mavenPom.getClass().getClassLoader();
    } else {
        baseClassLoader = GroovyShell.class.getClassLoader();
    }
    if (contextClassLoader || maven) {
        savedLoader = thread.getContextClassLoader();
        thread.setContextClassLoader(GroovyShell.class.getClassLoader());
    }
    final String scriptName = computeScriptName();
    final GroovyClassLoader classLoader = new GroovyClassLoader(baseClassLoader);
    addClassPathes(classLoader);
    configureCompiler();
    final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
    try {
        parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
    } finally {
        groovy.resetLoadedClasses();
        groovy.getClassLoader().clearCache();
        if (contextClassLoader || maven)
            thread.setContextClassLoader(savedLoader);
    }
}
Also used : Binding(groovy.lang.Binding) AntBuilder(groovy.util.AntBuilder) MissingMethodException(groovy.lang.MissingMethodException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyShell(groovy.lang.GroovyShell) GroovyClassLoader(groovy.lang.GroovyClassLoader) Project(org.apache.tools.ant.Project) Field(java.lang.reflect.Field) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Example 62 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class GroovyEngine method initialize.

/**
     * Initialize the engine.
     */
public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    // create a shell
    shell = new GroovyShell(mgr.getClassLoader());
    // register the mgr with object name "bsf"
    shell.setVariable("bsf", new BSFFunctions(mgr, this));
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
        declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
}
Also used : GroovyShell(groovy.lang.GroovyShell) BSFFunctions(org.apache.bsf.util.BSFFunctions)

Example 63 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class DocGeneratorMain method main.

public static void main(String[] args) {
    try {
        GroovyShell shell = new GroovyShell();
        //shell.run("src/main/org/codehaus/groovy/tools/DocGenerator.groovy", "org.codehaus.groovy.tools.DocGenerator.groovy", args);
        shell.run(new File("src/main/org/codehaus/groovy/tools/DocGenerator.groovy"), args);
    } catch (Exception e) {
        System.out.println("Failed: " + e);
        e.printStackTrace();
    }
}
Also used : File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 64 with GroovyShell

use of groovy.lang.GroovyShell in project hudson-2.x by hudson.

the class GroovyCommand method run.

protected int run() throws Exception {
    // this allows the caller to manipulate the JVM state, so require the admin privilege.
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
    Binding binding = new Binding();
    binding.setProperty("out", new PrintWriter(stdout, true));
    String j = getClientEnvironmentVariable("JOB_NAME");
    if (j != null) {
        Item job = Hudson.getInstance().getItemByFullName(j);
        binding.setProperty("currentJob", job);
        String b = getClientEnvironmentVariable("BUILD_NUMBER");
        if (b != null && job instanceof AbstractProject) {
            Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
            binding.setProperty("currentBuild", r);
        }
    }
    GroovyShell groovy = new GroovyShell(binding);
    groovy.run(loadScript(), "RemoteClass", remaining.toArray(new String[remaining.size()]));
    return 0;
}
Also used : Binding(groovy.lang.Binding) Item(hudson.model.Item) Run(hudson.model.Run) AbstractProject(hudson.model.AbstractProject) GroovyShell(groovy.lang.GroovyShell) PrintWriter(java.io.PrintWriter)

Example 65 with GroovyShell

use of groovy.lang.GroovyShell in project hudson-2.x by hudson.

the class BeanBuilder method loadBeans.

/**
	 * Loads a set of given beans
	 * @param resources The resources to load
	 * @throws IOException
	 */
public void loadBeans(Resource[] resources) throws IOException {
    Closure beans = new Closure(this) {

        @Override
        public Object call(Object[] args) {
            return beans((Closure) args[0]);
        }
    };
    Binding b = new Binding();
    b.setVariable("beans", beans);
    GroovyShell shell = classLoader != null ? new GroovyShell(classLoader, b) : new GroovyShell(b);
    for (Resource resource : resources) {
        shell.evaluate(resource.getInputStream());
    }
}
Also used : Binding(groovy.lang.Binding) Closure(groovy.lang.Closure) Resource(org.springframework.core.io.Resource) GroovyShell(groovy.lang.GroovyShell)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3