Search in sources :

Example 1 with ChannelClosedException

use of hudson.remoting.ChannelClosedException 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

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 Groovysh (org.codehaus.groovy.tools.shell.Groovysh)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