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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 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
25 package hudson.tools;
26
27 import hudson.Extension;
28 import hudson.FilePath;
29 import hudson.model.Node;
30 import hudson.model.TaskListener;
31 import hudson.tasks.CommandInterpreter;
32 import hudson.util.FormValidation;
33 import java.io.IOException;
34 import org.kohsuke.stapler.DataBoundConstructor;
35 import org.kohsuke.stapler.QueryParameter;
36
37 /**
38 * Installs a tool by running an arbitrary shell command.
39 * @since 1.305
40 */
41 public class CommandInstaller extends ToolInstaller {
42
43 /**
44 * Command to execute, similar to {@link CommandInterpreter#command}.
45 */
46 private final String command;
47
48 /**
49 * Resulting tool home directory.
50 */
51 private final String toolHome;
52
53 @DataBoundConstructor
54 public CommandInstaller(String label, String command, String toolHome) {
55 super(label);
56 this.command = fixCrLf(command);
57 this.toolHome = toolHome;
58 }
59
60 /**
61 * Fix CR/LF and always make it Unix style.
62 */
63 private static String fixCrLf(String s) {
64 // eliminate CR
65 int idx;
66 while((idx=s.indexOf("\r\n"))!=-1)
67 s = s.substring(0,idx)+s.substring(idx+1);
68 return s;
69 }
70
71 public String getCommand() {
72 return command;
73 }
74
75 public String getToolHome() {
76 return toolHome;
77 }
78
79 public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
80 FilePath dir = preferredLocation(tool, node);
81 // XXX support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses)
82 FilePath script = dir.createTextTempFile("hudson", ".sh", command);
83 try {
84 String[] cmd = {"sh", "-e", script.getRemote()};
85 int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join();
86 if (r != 0) {
87 throw new IOException("Command returned status " + r);
88 }
89 } finally {
90 script.delete();
91 }
92 return dir.child(toolHome);
93 }
94
95 @Extension
96 public static class DescriptorImpl extends ToolInstallerDescriptor<CommandInstaller> {
97
98 public String getDisplayName() {
99 return Messages.CommandInstaller_DescriptorImpl_displayName();
100 }
101
102 public FormValidation doCheckCommand(@QueryParameter String value) {
103 if (value.length() > 0) {
104 return FormValidation.ok();
105 } else {
106 return FormValidation.error(Messages.CommandInstaller_no_command());
107 }
108 }
109
110 public FormValidation doCheckToolHome(@QueryParameter String value) {
111 if (value.length() > 0) {
112 return FormValidation.ok();
113 } else {
114 return FormValidation.error(Messages.CommandInstaller_no_toolHome());
115 }
116 }
117
118 }
119
120 }
121