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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
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.tasks;
25
26 import hudson.FilePath;
27 import hudson.Launcher;
28 import hudson.Util;
29 import hudson.EnvVars;
30 import hudson.model.AbstractBuild;
31 import hudson.model.BuildListener;
32 import hudson.model.Node;
33 import hudson.model.TaskListener;
34
35 import java.io.IOException;
36 import java.util.Map;
37 import javax.annotation.Nonnull;
38
39 /**
40 * Common part between {@link Shell} and {@link BatchFile}.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44 public abstract class CommandInterpreter extends Builder {
45 /**
46 * Command to execute. The format depends on the actual {@link CommandInterpreter} implementation.
47 */
48 protected final String command;
49
50 public CommandInterpreter(String command) {
51 this.command = command;
52 }
53
54 public final String getCommand() {
55 return command;
56 }
57
58 @Override
59 public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
60 return perform(build,launcher,(TaskListener)listener);
61 }
62
63 public boolean perform(AbstractBuild<?,?> build, Launcher launcher, TaskListener listener) throws InterruptedException {
64 FilePath ws = build.getWorkspace();
65 if (ws == null) {
66 Node node = build.getBuiltOn();
67 if (node == null) {
68 throw new NullPointerException("no such build node: " + build.getBuiltOnStr());
69 }
70 throw new NullPointerException("no workspace from node " + node + " which is computer " + node.toComputer() + " and has channel " + node.getChannel());
71 }
72 FilePath script=null;
73 try {
74 try {
75 script = createScriptFile(ws);
76 } catch (IOException e) {
77 Util.displayIOException(e,listener);
78 e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_UnableToProduceScript()));
79 return false;
80 }
81
82 int r;
83 try {
84 EnvVars envVars = build.getEnvironment(listener);
85 // on Windows environment variables are converted to all upper case,
86 // but no such conversions are done on Unix, so to make this cross-platform,
87 // convert variables to all upper cases.
88 for(Map.Entry<String,String> e : build.getBuildVariables().entrySet())
89 envVars.put(e.getKey(),e.getValue());
90
91 r = launcher.launch().cmds(buildCommandLine(script)).envs(envVars).stdout(listener).pwd(ws).join();
92 } catch (IOException e) {
93 Util.displayIOException(e,listener);
94 e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_CommandFailed()));
95 r = -1;
96 }
97 return r==0;
98 } finally {
99 try {
100 if(script!=null)
101 script.delete();
102 } catch (IOException e) {
103 Util.displayIOException(e,listener);
104 e.printStackTrace( listener.fatalError(Messages.CommandInterpreter_UnableToDelete(script)) );
105 } catch (Exception e) {
106 e.printStackTrace( listener.fatalError(Messages.CommandInterpreter_UnableToDelete(script)) );
107 }
108 }
109 }
110
111 /**
112 * Creates a script file in a temporary name in the specified directory.
113 */
114 public FilePath createScriptFile(@Nonnull FilePath dir) throws IOException, InterruptedException {
115 return dir.createTextTempFile("hudson", getFileExtension(), getContents(), false);
116 }
117
118 public abstract String[] buildCommandLine(FilePath script);
119
120 protected abstract String getContents();
121
122 protected abstract String getFileExtension();
123 }
124