KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > Build


1 package hudson.model;
2
3 import hudson.tasks.BuildStep;
4 import hudson.tasks.BuildWrapper;
5 import hudson.tasks.BuildWrapper.Environment;
6 import hudson.tasks.Builder;
7 import hudson.tasks.Publisher;
8 import hudson.tasks.test.AbstractTestResultAction;
9 import hudson.triggers.SCMTrigger;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * A build of a {@link Project}.
19  *
20  * @author Kohsuke Kawaguchi
21  */

22 public final class Build extends AbstractBuild<Project,Build> {
23
24     /**
25      * Creates a new build.
26      */

27     Build(Project project) throws IOException JavaDoc {
28         super(project);
29     }
30
31     /**
32      * Loads a build from a log file.
33      */

34     Build(Project project, File JavaDoc buildDir) throws IOException JavaDoc {
35         super(project,buildDir);
36     }
37
38     @Override JavaDoc
39     public AbstractTestResultAction getTestResultAction() {
40         return getAction(AbstractTestResultAction.class);
41     }
42     
43     /**
44      * During the build this field remembers {@link Environment}s created by
45      * {@link BuildWrapper}. This design is bit ugly but forced due to compatibility.
46      */

47     private transient List JavaDoc<Environment> buildEnvironments;
48
49     @Override JavaDoc
50     protected void onStartBuilding() {
51         SCMTrigger t = (SCMTrigger)project.getTriggers().get(SCMTrigger.DESCRIPTOR);
52         if(t==null) {
53             super.onStartBuilding();
54         } else {
55             synchronized(t) {
56                 try {
57                     t.abort();
58                 } catch (InterruptedException JavaDoc e) {
59                     // handle the interrupt later
60
Thread.currentThread().interrupt();
61                 }
62                 super.onStartBuilding();
63             }
64         }
65     }
66
67     @Override JavaDoc
68     protected void onEndBuilding() {
69         SCMTrigger t = (SCMTrigger)project.getTriggers().get(SCMTrigger.DESCRIPTOR);
70         if(t==null) {
71             super.onEndBuilding();
72         } else {
73             synchronized(t) {
74                 super.onEndBuilding();
75                 t.startPolling();
76             }
77         }
78     }
79
80     @Override JavaDoc
81     public Map JavaDoc<String JavaDoc,String JavaDoc> getEnvVars() {
82         Map JavaDoc<String JavaDoc,String JavaDoc> env = super.getEnvVars();
83
84         if(buildEnvironments!=null) {
85             for (Environment e : buildEnvironments)
86                 e.buildEnvVars(env);
87         }
88
89         return env;
90     }
91
92 //
93
//
94
// actions
95
//
96
//
97
@Override JavaDoc
98     public void run() {
99         run(new RunnerImpl());
100     }
101     
102     private class RunnerImpl extends AbstractRunner {
103         protected Result doRun(BuildListener listener) throws Exception JavaDoc {
104             if(!preBuild(listener,project.getBuilders()))
105                 return Result.FAILURE;
106             if(!preBuild(listener,project.getPublishers()))
107                 return Result.FAILURE;
108
109             buildEnvironments = new ArrayList JavaDoc<Environment>();
110             try {
111                 for( BuildWrapper w : project.getBuildWrappers().values() ) {
112                     Environment e = w.setUp(Build.this, launcher, listener);
113                     if(e==null)
114                         return Result.FAILURE;
115                     buildEnvironments.add(e);
116                 }
117
118
119                 if(!build(listener,project.getBuilders()))
120                     return Result.FAILURE;
121             } finally {
122                 // tear down in reverse order
123
for( int i=buildEnvironments.size()-1; i>=0; i-- )
124                     buildEnvironments.get(i).tearDown(Build.this,listener);
125                 buildEnvironments = null;
126             }
127
128             return null;
129         }
130
131         public void post(BuildListener listener) {
132             // run all of them even if one of them failed
133
try {
134                 for( Publisher bs : project.getPublishers().values() )
135                     bs.perform(Build.this, launcher, listener);
136             } catch (InterruptedException JavaDoc e) {
137                 e.printStackTrace(listener.fatalError("aborted"));
138                 setResult(Result.FAILURE);
139             } catch (IOException JavaDoc e) {
140                 e.printStackTrace(listener.fatalError("failed"));
141                 setResult(Result.FAILURE);
142             }
143         }
144
145         private boolean build(BuildListener listener, Map JavaDoc<?, Builder> steps) throws IOException JavaDoc, InterruptedException JavaDoc {
146             for( Builder bs : steps.values() )
147                 if(!bs.perform(Build.this, launcher, listener))
148                     return false;
149             return true;
150         }
151
152         private boolean preBuild(BuildListener listener,Map JavaDoc<?,? extends BuildStep> steps) {
153             for( BuildStep bs : steps.values() )
154                 if(!bs.prebuild(Build.this,listener))
155                     return false;
156             return true;
157         }
158     }
159 }
160
Popular Tags