File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\cli\InstallToolCommand.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 hudson.AbortException;
28 import hudson.EnvVars;
29 import jenkins.model.Jenkins;
30 import hudson.model.AbstractProject;
31 import hudson.model.Run;
32 import hudson.model.Executor;
33 import hudson.model.Node;
34 import hudson.model.EnvironmentSpecific;
35 import hudson.model.Item;
36 import hudson.remoting.Callable;
37 import hudson.slaves.NodeSpecific;
38 import hudson.util.EditDistance;
39 import hudson.util.StreamTaskListener;
40 import hudson.tools.ToolDescriptor;
41 import hudson.tools.ToolInstallation;
42
43 import java.util.List;
44 import java.util.ArrayList;
45 import java.io.IOException;
46
47 import org.kohsuke.args4j.Argument;
48
49 /**
50 * Performs automatic tool installation on demand.
51 *
52 * @author Kohsuke Kawaguchi
53 */
54 @Extension
55 public class InstallToolCommand extends CLICommand {
56 @Argument(index=0,metaVar="KIND",usage="The type of the tool to install, such as 'Ant'")
57 public String toolType;
58
59 @Argument(index=1,metaVar="NAME",usage="The name of the tool to install, as you've entered in the Jenkins system configuration")
60 public String toolName;
61
62 public String getShortDescription() {
63 return Messages.InstallToolCommand_ShortDescription();
64 }
65
66 protected int run() throws Exception {
67 Jenkins h = Jenkins.getInstance();
68 h.checkPermission(Jenkins.READ);
69
70 // where is this build running?
71 BuildIDs id = checkChannel().call(new BuildIDs());
72
73 if (!id.isComplete())
74 throw new AbortException("This command can be only invoked from a build executing inside Hudson");
75
76 AbstractProject p = Jenkins.getInstance().getItemByFullName(id.job, AbstractProject.class);
77 if (p==null)
78 throw new AbortException("No such job found: "+id.job);
79 p.checkPermission(Item.CONFIGURE);
80
81 List<String> toolTypes = new ArrayList<String>();
82 for (ToolDescriptor<?> d : ToolInstallation.all()) {
83 toolTypes.add(d.getDisplayName());
84 if (d.getDisplayName().equals(toolType)) {
85 List<String> toolNames = new ArrayList<String>();
86 for (ToolInstallation t : d.getInstallations()) {
87 toolNames.add(t.getName());
88 if (t.getName().equals(toolName))
89 return install(t, id, p);
90 }
91
92 // didn't find the right tool name
93 error(toolNames, toolName, "name");
94 }
95 }
96
97 // didn't find the tool type
98 error(toolTypes, toolType, "type");
99
100 // will never be here
101 throw new AssertionError();
102 }
103
104 private int error(List<String> candidates, String given, String noun) throws AbortException {
105 if (given ==null)
106 throw new AbortException("No tool "+ noun +" was specified. Valid values are "+candidates.toString());
107 else
108 throw new AbortException("Unrecognized tool "+noun+". Perhaps you meant '"+ EditDistance.findNearest(given,candidates)+"'?");
109 }
110
111 /**
112 * Performs an installation.
113 */
114 private int install(ToolInstallation t, BuildIDs id, AbstractProject p) throws IOException, InterruptedException {
115
116 Run b = p.getBuildByNumber(Integer.parseInt(id.number));
117 if (b==null)
118 throw new AbortException("No such build: "+id.number);
119
120 Executor exec = b.getExecutor();
121 if (exec==null)
122 throw new AbortException(b.getFullDisplayName()+" is not building");
123
124 Node node = exec.getOwner().getNode();
125
126 t = t.translate(node, EnvVars.getRemote(checkChannel()), new StreamTaskListener(stderr));
127 stdout.println(t.getHome());
128 return 0;
129 }
130
131 private static final class BuildIDs implements Callable<BuildIDs, IOException> {
132 String job,number,id;
133
134 public BuildIDs call() throws IOException {
135 job = System.getenv("JOB_NAME");
136 number = System.getenv("BUILD_NUMBER");
137 id = System.getenv("BUILD_ID");
138 return this;
139 }
140
141 boolean isComplete() {
142 return job!=null && number!=null && id!=null;
143 }
144
145 private static final long serialVersionUID = 1L;
146 }
147 }
148