File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\cli\GroovyshCommand.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.cli;
25
26 import hudson.Extension;
27 import jenkins.model.Jenkins;
28 import hudson.remoting.ChannelClosedException;
29 import groovy.lang.Binding;
30 import groovy.lang.Closure;
31 import org.codehaus.groovy.tools.shell.Groovysh;
32 import org.codehaus.groovy.tools.shell.IO;
33 import org.codehaus.groovy.tools.shell.Shell;
34 import org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar;
35
36 import java.util.List;
37 import java.util.Locale;
38 import java.io.PrintStream;
39 import java.io.InputStream;
40 import java.io.BufferedInputStream;
41 import java.io.PrintWriter;
42
43 import jline.UnsupportedTerminal;
44 import jline.Terminal;
45
46 /**
47 * Executes Groovy shell.
48 *
49 * @author Kohsuke Kawaguchi
50 */
51 @Extension
52 public class GroovyshCommand extends CLICommand {
53 @Override
54 public String getShortDescription() {
55 return Messages.GroovyshCommand_ShortDescription();
56 }
57
58 @Override
59 public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
60 // this allows the caller to manipulate the JVM state, so require the admin privilege.
61 Jenkins.getInstance().checkPermission(Jenkins.RUN_SCRIPTS);
62 // TODO: ^as this class overrides main() (which has authentication stuff),
63 // how to get ADMIN permission for this command?
64
65 // this being remote means no jline capability is available
66 System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
67 Terminal.resetTerminal();
68
69 Groovysh shell = createShell(stdin, stdout, stderr);
70 return shell.run(args.toArray(new String[args.size()]));
71 }
72
73 @SuppressWarnings({"unchecked","rawtypes"})
74 protected Groovysh createShell(InputStream stdin, PrintStream stdout,
75 PrintStream stderr) {
76
77 Binding binding = new Binding();
78 // redirect "println" to the CLI
79 binding.setProperty("out", new PrintWriter(stdout,true));
80 binding.setProperty("hudson", Jenkins.getInstance()); // backward compatibility
81 binding.setProperty("jenkins", Jenkins.getInstance());
82
83 IO io = new IO(new BufferedInputStream(stdin),stdout,stderr);
84
85 final ClassLoader cl = Jenkins.getInstance().pluginManager.uberClassLoader;
86 Closure registrar = new Closure(null, null) {
87 private static final long serialVersionUID = 1L;
88
89 @SuppressWarnings("unused")
90 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS",justification="Closure invokes this via reflection")
91 public Object doCall(Object[] args) {
92 assert(args.length == 1);
93 assert(args[0] instanceof Shell);
94
95 Shell shell = (Shell)args[0];
96 XmlCommandRegistrar r = new XmlCommandRegistrar(shell, cl);
97 r.register(GroovyshCommand.class.getResource("commands.xml"));
98
99 return null;
100 }
101 };
102 Groovysh shell = new Groovysh(cl, binding, io, registrar);
103 shell.getImports().add("import hudson.model.*");
104
105 // defaultErrorHook doesn't re-throw IOException, so ShellRunner in
106 // Groovysh will keep looping forever if we don't terminate when the
107 // channel is closed
108 final Closure originalErrorHook = shell.getErrorHook();
109 shell.setErrorHook(new Closure(shell, shell) {
110 private static final long serialVersionUID = 1L;
111
112 @SuppressWarnings("unused")
113 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS",justification="Closure invokes this via reflection")
114 public Object doCall(Object[] args) throws ChannelClosedException {
115 if (args.length == 1 && args[0] instanceof ChannelClosedException) {
116 throw (ChannelClosedException)args[0];
117 }
118
119 return originalErrorHook.call(args);
120 }
121 });
122
123 return shell;
124 }
125
126 protected int run() {
127 throw new UnsupportedOperationException();
128 }
129 }
130