KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > Main


1 package hudson;
2
3 import hudson.model.ExternalJob;
4 import hudson.model.ExternalRun;
5 import hudson.model.Hudson;
6 import hudson.model.Result;
7 import hudson.model.TopLevelItem;
8 import hudson.util.DualOutputStream;
9 import hudson.util.EncodingStream;
10
11 import java.io.File JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.OutputStreamWriter JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.net.HttpURLConnection JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLEncoder JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Entry point to Hudson from command line.
23  *
24  * <p>
25  * This tool runs another process and sends its result to Hudson.
26  *
27  * @author Kohsuke Kawaguchi
28  */

29 public class Main {
30     public static void main(String JavaDoc[] args) {
31         try {
32             System.exit(run(args));
33         } catch (Exception JavaDoc e) {
34             e.printStackTrace();
35             System.exit(-1);
36         }
37     }
38
39     public static int run(String JavaDoc[] args) throws Exception JavaDoc {
40         String JavaDoc home = getHudsonHome();
41         if(home==null) {
42             System.err.println("HUDSON_HOME is not set.");
43             return -1;
44         }
45
46         if(home.startsWith("http")) {
47             return remotePost(args);
48         } else {
49             return localPost(args);
50         }
51     }
52
53     private static String JavaDoc getHudsonHome() {
54         return EnvVars.masterEnvVars.get("HUDSON_HOME");
55     }
56
57     /**
58      * Run command and place the result directly into the local installation of Hudson.
59      */

60     public static int localPost(String JavaDoc[] args) throws Exception JavaDoc {
61         Hudson app = new Hudson(new File JavaDoc(getHudsonHome()),null);
62
63         TopLevelItem item = app.getItem(args[0]);
64         if(!(item instanceof ExternalJob)) {
65             System.err.println(args[0]+" is not a valid external job name in Hudson");
66             return -1;
67         }
68         ExternalJob ejob = (ExternalJob) item;
69
70         ExternalRun run = ejob.newBuild();
71
72         // run the command
73
List JavaDoc<String JavaDoc> cmd = new ArrayList JavaDoc<String JavaDoc>();
74         for( int i=1; i<args.length; i++ )
75             cmd.add(args[i]);
76         run.run(cmd.toArray(new String JavaDoc[cmd.size()]));
77
78         return run.getResult()==Result.SUCCESS?0:1;
79     }
80
81     /**
82      * Run command and place the result to a remote Hudson installation
83      */

84     public static int remotePost(String JavaDoc[] args) throws Exception JavaDoc {
85         String JavaDoc projectName = args[0];
86
87         String JavaDoc home = getHudsonHome();
88         if(!home.endsWith("/")) home = home + '/'; // make sure it ends with '/'
89

90         {// check if the home is set correctly
91
HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(home).openConnection();
92             con.connect();
93             if(con.getResponseCode()!=200
94             || con.getHeaderField("X-Hudson")==null) {
95                 System.err.println(home+" is not Hudson ("+con.getResponseMessage()+")");
96                 return -1;
97             }
98         }
99
100         String JavaDoc projectNameEnc = URLEncoder.encode(projectName,"UTF-8").replaceAll("\\+","%20");
101
102         {// check if the job name is correct
103
HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(home+"job/"+projectNameEnc+"/acceptBuildResult").openConnection();
104             con.connect();
105             if(con.getResponseCode()!=200) {
106                 System.err.println(projectName+" is not a valid job name on "+home+" ("+con.getResponseMessage()+")");
107                 return -1;
108             }
109         }
110
111         // start a remote connection
112
HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc) new URL JavaDoc(home+"job/"+projectNameEnc+"/postBuildResult").openConnection();
113         con.setDoOutput(true);
114         con.connect();
115         OutputStream os = con.getOutputStream();
116         Writer JavaDoc w = new OutputStreamWriter JavaDoc(os,"UTF-8");
117         w.write("<?xml version='1.0' encoding='UTF-8'?>");
118         w.write("<run><log encoding='hexBinary'>");
119         w.flush();
120
121         // run the command
122
long start = System.currentTimeMillis();
123
124         List JavaDoc<String JavaDoc> cmd = new ArrayList JavaDoc<String JavaDoc>();
125         for( int i=1; i<args.length; i++ )
126             cmd.add(args[i]);
127         Proc proc = new Proc.LocalProc(cmd.toArray(new String JavaDoc[0]),(String JavaDoc[])null,System.in,
128             new DualOutputStream(System.out,new EncodingStream(os)));
129
130         int ret = proc.join();
131
132         w.write("</log><result>"+ret+"</result><duration>"+(System.currentTimeMillis()-start)+"</duration></run>");
133         w.close();
134
135         if(con.getResponseCode()!=200) {
136             Util.copyStream(con.getErrorStream(),System.err);
137         }
138
139         return ret;
140     }
141 }
142
Popular Tags