KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > CommandInterpreter


1 package hudson.tasks;
2
3 import hudson.model.Build;
4 import hudson.model.BuildListener;
5 import hudson.model.Project;
6 import hudson.Launcher;
7 import hudson.FilePath;
8 import hudson.Util;
9
10 import java.io.IOException JavaDoc;
11
12 /**
13  * Common part between {@link Shell} and {@link BatchFile}.
14  *
15  * @author Kohsuke Kawaguchi
16  */

17 public abstract class CommandInterpreter extends Builder {
18     /**
19      * Command to execute. The format depends on the actual {@link CommandInterpreter} implementation.
20      */

21     protected final String JavaDoc command;
22
23     public CommandInterpreter(String JavaDoc command) {
24         this.command = command;
25     }
26
27     public final String JavaDoc getCommand() {
28         return command;
29     }
30
31     public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException JavaDoc {
32         Project proj = build.getProject();
33         FilePath ws = proj.getWorkspace();
34         FilePath script=null;
35         try {
36             try {
37                 script = ws.createTextTempFile("hudson", getFileExtension(), getContents(), false);
38             } catch (IOException JavaDoc e) {
39                 Util.displayIOException(e,listener);
40                 e.printStackTrace( listener.fatalError("Unable to produce a script file") );
41                 return false;
42             }
43
44             String JavaDoc[] cmd = buildCommandLine(script);
45
46             int r;
47             try {
48                 r = launcher.launch(cmd,build.getEnvVars(),listener.getLogger(),ws).join();
49             } catch (IOException JavaDoc e) {
50                 Util.displayIOException(e,listener);
51                 e.printStackTrace( listener.fatalError("command execution failed") );
52                 r = -1;
53             }
54             return r==0;
55         } finally {
56             try {
57                 if(script!=null)
58                 script.delete();
59             } catch (IOException JavaDoc e) {
60                 Util.displayIOException(e,listener);
61                 e.printStackTrace( listener.fatalError("Unable to delete script file "+script) );
62             }
63         }
64     }
65
66     protected abstract String JavaDoc[] buildCommandLine(FilePath script);
67
68     protected abstract String JavaDoc getContents();
69
70     protected abstract String JavaDoc getFileExtension();
71 }
72
Popular Tags