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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, 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 groovy.lang.GroovyShell;
27 import groovy.lang.Binding;
28 import hudson.cli.util.ScriptLoader;
29 import hudson.model.AbstractProject;
30 import jenkins.model.Jenkins;
31 import hudson.model.Item;
32 import hudson.model.Run;
33 import hudson.remoting.Callable;
34 import hudson.AbortException;
35 import hudson.Extension;
36 import org.kohsuke.args4j.Argument;
37 import org.kohsuke.args4j.CmdLineException;
38 import org.apache.commons.io.IOUtils;
39 import org.apache.commons.io.FileUtils;
40
41 import java.io.IOException;
42 import java.io.Serializable;
43 import java.io.File;
44 import java.io.InputStream;
45 import java.io.PrintWriter;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.net.URL;
49 import java.net.MalformedURLException;
50
51 /**
52 * Executes the specified groovy script.
53 *
54 * @author Kohsuke Kawaguchi
55 */
56 @Extension
57 public class GroovyCommand extends CLICommand {
58 @Override
59 public String getShortDescription() {
60 return Messages.GroovyCommand_ShortDescription();
61 }
62
63 @Argument(metaVar="SCRIPT",usage="Script to be executed. File, URL or '=' to represent stdin.")
64 public String script;
65
66 /**
67 * Remaining arguments.
68 */
69 @Argument(index=1)
70 public List<String> remaining = new ArrayList<String>();
71
72 protected int run() throws Exception {
73 // this allows the caller to manipulate the JVM state, so require the execute script privilege.
74 Jenkins.getInstance().checkPermission(Jenkins.RUN_SCRIPTS);
75
76 Binding binding = new Binding();
77 binding.setProperty("out",new PrintWriter(stdout,true));
78 binding.setProperty("stdin",stdin);
79 binding.setProperty("stdout",stdout);
80 binding.setProperty("stderr",stderr);
81 binding.setProperty("channel",channel);
82 String j = getClientEnvironmentVariable("JOB_NAME");
83 if (j!=null) {
84 Item job = Jenkins.getInstance().getItemByFullName(j);
85 binding.setProperty("currentJob", job);
86 String b = getClientEnvironmentVariable("BUILD_NUMBER");
87 if (b!=null && job instanceof AbstractProject) {
88 Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
89 binding.setProperty("currentBuild", r);
90 }
91 }
92
93 GroovyShell groovy = new GroovyShell(Jenkins.getInstance().getPluginManager().uberClassLoader, binding);
94 groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()]));
95 return 0;
96 }
97
98 /**
99 * Loads the script from the argument.
100 */
101 private String loadScript() throws CmdLineException, IOException, InterruptedException {
102 if(script==null)
103 throw new CmdLineException(null, "No script is specified");
104 if (script.equals("="))
105 return IOUtils.toString(stdin);
106
107 return checkChannel().call(new ScriptLoader(script));
108 }
109 }
110
111