KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > ExternalRun


1 package hudson.model;
2
3 import hudson.Proc;
4 import hudson.util.DecodingStream;
5 import hudson.util.DualOutputStream;
6 import org.xmlpull.mxp1.MXParser;
7 import org.xmlpull.v1.XmlPullParser;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.PrintStream JavaDoc;
12 import java.io.Reader JavaDoc;
13
14 /**
15  * {@link Run} for {@link ExternalJob}.
16  *
17  * @author Kohsuke Kawaguchi
18  */

19 public class ExternalRun extends Run<ExternalJob,ExternalRun> {
20     /**
21      * Loads a run from a log file.
22      */

23     ExternalRun(ExternalJob owner, File JavaDoc runDir) throws IOException JavaDoc {
24         super(owner,runDir);
25     }
26
27     /**
28      * Creates a new run.
29      */

30     ExternalRun(ExternalJob project) throws IOException JavaDoc {
31         super(project);
32     }
33
34     /**
35      * Instead of performing a build, run the specified command,
36      * record the log and its exit code, then call it a build.
37      */

38     public void run(final String JavaDoc[] cmd) {
39         run(new Runner() {
40             public Result run(BuildListener listener) throws Exception JavaDoc {
41                 Proc proc = new Proc.LocalProc(cmd,getEnvVars(),System.in,new DualOutputStream(System.out,listener.getLogger()));
42                 return proc.join()==0?Result.SUCCESS:Result.FAILURE;
43             }
44
45             public void post(BuildListener listener) {
46                 // do nothing
47
}
48         });
49     }
50
51     /**
52      * Instead of performing a build, accept the log and the return code
53      * from a remote machine in an XML format of:
54      *
55      * <pre><xmp>
56      * <run>
57      * <log>...console output...</log>
58      * <result>exit code</result>
59      * </run>
60      * </xmp></pre>
61      */

62     public void acceptRemoteSubmission(final Reader JavaDoc in) {
63         final long[] duration = new long[1];
64         run(new Runner() {
65             public Result run(BuildListener listener) throws Exception JavaDoc {
66                 PrintStream JavaDoc logger = new PrintStream JavaDoc(new DecodingStream(listener.getLogger()));
67
68                 XmlPullParser xpp = new MXParser();
69                 xpp.setInput(in);
70                 xpp.nextTag(); // get to the <run>
71
xpp.nextTag(); // get to the <log>
72
while(xpp.nextToken()!=XmlPullParser.END_TAG) {
73                     int type = xpp.getEventType();
74                     if(type==XmlPullParser.TEXT
75                     || type==XmlPullParser.CDSECT)
76                         logger.print(xpp.getText());
77                 }
78                 xpp.nextTag(); // get to <result>
79

80                 Result r = Integer.parseInt(xpp.nextText())==0?Result.SUCCESS:Result.FAILURE;
81
82                 xpp.nextTag(); // get to <duration> (optional)
83
if(xpp.getEventType()==XmlPullParser.START_TAG
84                 && xpp.getName().equals("duration")) {
85                     duration[0] = Long.parseLong(xpp.nextText());
86                 }
87
88                 return r;
89             }
90
91             public void post(BuildListener listener) {
92                 // do nothing
93
}
94         });
95
96         if(duration[0]!=0)
97             super.duration = duration[0];
98     }
99
100 }
101
Popular Tags