Search in sources :

Example 1 with Groovysh

use of org.codehaus.groovy.tools.shell.Groovysh in project hudson-2.x by hudson.

the class GroovyshCommand method main.

@Override
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
    // this allows the caller to manipulate the JVM state, so require the admin privilege.
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
    // TODO: ^as this class overrides main() (which has authentication stuff),
    // how to get ADMIN permission for this command?
    // this being remote means no jline capability is available
    System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
    Terminal.resetTerminal();
    Groovysh shell = createShell(stdin, stdout, stderr);
    return shell.run(args.toArray(new String[args.size()]));
}
Also used : Groovysh(org.codehaus.groovy.tools.shell.Groovysh) UnsupportedTerminal(jline.UnsupportedTerminal)

Example 2 with Groovysh

use of org.codehaus.groovy.tools.shell.Groovysh in project hudson-2.x by hudson.

the class GroovyshCommand method createShell.

protected Groovysh createShell(InputStream stdin, PrintStream stdout, PrintStream stderr) {
    Binding binding = new Binding();
    // redirect "println" to the CLI
    binding.setProperty("out", new PrintWriter(stdout, true));
    binding.setProperty("hudson", hudson.model.Hudson.getInstance());
    IO io = new IO(new BufferedInputStream(stdin), stdout, stderr);
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Closure registrar = new Closure(null, null) {

        public Object doCall(Object[] args) {
            assert (args.length == 1);
            assert (args[0] instanceof Shell);
            Shell shell = (Shell) args[0];
            XmlCommandRegistrar r = new XmlCommandRegistrar(shell, cl);
            r.register(GroovyshCommand.class.getResource("commands.xml"));
            return null;
        }
    };
    Groovysh shell = new Groovysh(cl, binding, io, registrar);
    shell.getImports().add("import hudson.model.*");
    // defaultErrorHook doesn't re-throw IOException, so ShellRunner in
    // Groovysh will keep looping forever if we don't terminate when the
    // channel is closed
    final Closure originalErrorHook = shell.getErrorHook();
    shell.setErrorHook(new Closure(shell, shell) {

        public Object doCall(Object[] args) throws ChannelClosedException {
            if (args.length == 1 && args[0] instanceof ChannelClosedException) {
                throw (ChannelClosedException) args[0];
            }
            return originalErrorHook.call(args);
        }
    });
    return shell;
}
Also used : Binding(groovy.lang.Binding) Closure(groovy.lang.Closure) IO(org.codehaus.groovy.tools.shell.IO) ChannelClosedException(hudson.remoting.ChannelClosedException) Shell(org.codehaus.groovy.tools.shell.Shell) Groovysh(org.codehaus.groovy.tools.shell.Groovysh) BufferedInputStream(java.io.BufferedInputStream) XmlCommandRegistrar(org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar) PrintWriter(java.io.PrintWriter)

Aggregations

Groovysh (org.codehaus.groovy.tools.shell.Groovysh)2 Binding (groovy.lang.Binding)1 Closure (groovy.lang.Closure)1 ChannelClosedException (hudson.remoting.ChannelClosedException)1 BufferedInputStream (java.io.BufferedInputStream)1 PrintWriter (java.io.PrintWriter)1 UnsupportedTerminal (jline.UnsupportedTerminal)1 IO (org.codehaus.groovy.tools.shell.IO)1 Shell (org.codehaus.groovy.tools.shell.Shell)1 XmlCommandRegistrar (org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar)1