KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > Project


1 package hudson.model;
2
3 import hudson.model.Descriptor.FormException;
4 import hudson.tasks.BuildStep;
5 import hudson.tasks.BuildTrigger;
6 import hudson.tasks.BuildWrapper;
7 import hudson.tasks.BuildWrappers;
8 import hudson.tasks.Builder;
9 import hudson.tasks.Fingerprinter;
10 import hudson.tasks.Publisher;
11 import hudson.triggers.Trigger;
12 import hudson.FilePath;
13 import org.kohsuke.stapler.StaplerRequest;
14 import org.kohsuke.stapler.StaplerResponse;
15
16 import javax.servlet.ServletException JavaDoc;
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * Buildable software project.
29  *
30  * @author Kohsuke Kawaguchi
31  */

32 public class Project extends AbstractProject<Project,Build> implements TopLevelItem, SCMedItem {
33
34     /**
35      * List of active {@link Builder}s configured for this project.
36      */

37     private List JavaDoc<Builder> builders = new Vector JavaDoc<Builder>();
38
39     /**
40      * List of active {@link Publisher}s configured for this project.
41      */

42     private List JavaDoc<Publisher> publishers = new Vector JavaDoc<Publisher>();
43
44     /**
45      * List of active {@link BuildWrapper}s configured for this project.
46      */

47     private List JavaDoc<BuildWrapper> buildWrappers = new Vector JavaDoc<BuildWrapper>();
48
49     /**
50      * {@link Action}s contributed from {@link #triggers}, {@link #builders},
51      * and {@link #publishers}.
52      *
53      * We don't want to persist them separately, and these actions
54      * come and go as configuration change, so it's kept separate.
55      */

56     private transient /*final*/ List JavaDoc<Action> transientActions = new Vector JavaDoc<Action>();
57
58     /**
59      * Creates a new project.
60      */

61     public Project(Hudson parent,String JavaDoc name) {
62         super(parent,name);
63     }
64
65     public void onLoad(ItemGroup<? extends Item> parent, String JavaDoc name) throws IOException JavaDoc {
66         super.onLoad(parent, name);
67
68         if(buildWrappers==null)
69             // it didn't exist in < 1.64
70
buildWrappers = new Vector JavaDoc<BuildWrapper>();
71
72         updateTransientActions();
73     }
74
75     public AbstractProject<?, ?> asProject() {
76         return this;
77     }
78
79     @Override JavaDoc
80     public Hudson getParent() {
81         return Hudson.getInstance();
82     }
83
84     @Override JavaDoc
85     public FilePath getWorkspace() {
86         Node node = getLastBuiltOn();
87         if(node==null) node = getParent();
88         return node.getWorkspaceFor(this);
89     }
90
91     @Override JavaDoc
92     public BallColor getIconColor() {
93         if(isDisabled())
94             // use grey to indicate that the build is disabled
95
return BallColor.GREY;
96         else
97             return super.getIconColor();
98     }
99
100     public synchronized Map JavaDoc<Descriptor<Builder>,Builder> getBuilders() {
101         return Descriptor.toMap(builders);
102     }
103
104     public synchronized Map JavaDoc<Descriptor<Publisher>,Publisher> getPublishers() {
105         return Descriptor.toMap(publishers);
106     }
107
108     public synchronized Map JavaDoc<Descriptor<BuildWrapper>,BuildWrapper> getBuildWrappers() {
109         return Descriptor.toMap(buildWrappers);
110     }
111
112     /**
113      * Adds a new {@link BuildStep} to this {@link Project} and saves the configuration.
114      */

115     private void addPublisher(Publisher buildStep) throws IOException JavaDoc {
116         addToList(buildStep,publishers);
117     }
118
119     /**
120      * Removes a publisher from this project, if it's active.
121      */

122     private void removePublisher(Descriptor<Publisher> descriptor) throws IOException JavaDoc {
123         removeFromList(descriptor, publishers);
124     }
125
126     @Override JavaDoc
127     public Build newBuild() throws IOException JavaDoc {
128         Build lastBuild = new Build(this);
129         builds.put(lastBuild);
130         return lastBuild;
131     }
132
133     @Override JavaDoc
134     protected Build loadBuild(File JavaDoc dir) throws IOException JavaDoc {
135         return new Build(this,dir);
136     }
137
138     protected void buildDependencyGraph(DependencyGraph graph) {
139         BuildTrigger buildTrigger = (BuildTrigger) getPublishers().get(BuildTrigger.DESCRIPTOR);
140         if(buildTrigger!=null)
141              graph.addDependency(this,buildTrigger.getChildProjects());
142     }
143
144     @Override JavaDoc
145     public boolean isFingerprintConfigured() {
146         synchronized(publishers) {
147             for (Publisher p : publishers) {
148                 if(p instanceof Fingerprinter)
149                     return true;
150             }
151         }
152         return false;
153     }
154
155
156
157 //
158
//
159
// actions
160
//
161
//
162
/**
163      * Accepts submission from the configuration page.
164      */

165     public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
166         Set JavaDoc<AbstractProject> upstream = Collections.emptySet();
167         if(req.getParameter("pseudoUpstreamTrigger")!=null) {
168             upstream = new HashSet JavaDoc<AbstractProject>(Items.fromNameList(req.getParameter("upstreamProjects"),AbstractProject.class));
169         }
170
171         synchronized(this) {
172             try {
173                 if(!Hudson.adminCheck(req,rsp))
174                     return;
175
176                 req.setCharacterEncoding("UTF-8");
177
178                 buildDescribable(req, BuildWrappers.WRAPPERS, buildWrappers, "wrapper");
179                 buildDescribable(req, BuildStep.BUILDERS, builders, "builder");
180                 buildDescribable(req, BuildStep.PUBLISHERS, publishers, "publisher");
181
182                 super.doConfigSubmit(req,rsp);
183
184                 updateTransientActions();
185             } catch (FormException e) {
186                 sendError(e,req,rsp);
187             }
188         }
189
190         // dependency setting might have been changed by the user, so rebuild.
191
Hudson.getInstance().rebuildDependencyGraph();
192
193         // reflect the submission of the pseudo 'upstream build trriger'.
194
// this needs to be done after we release the lock on 'this',
195
// or otherwise we could dead-lock
196

197         for (Project p : Hudson.getInstance().getProjects()) {
198             boolean isUpstream = upstream.contains(p);
199             synchronized(p) {
200                 List JavaDoc<AbstractProject> newChildProjects = new ArrayList JavaDoc<AbstractProject>(p.getDownstreamProjects());
201
202                 if(isUpstream) {
203                     if(!newChildProjects.contains(this))
204                         newChildProjects.add(this);
205                 } else {
206                     newChildProjects.remove(this);
207                 }
208
209                 if(newChildProjects.isEmpty()) {
210                     p.removePublisher(BuildTrigger.DESCRIPTOR);
211                 } else {
212                     p.addPublisher(new BuildTrigger(newChildProjects));
213                 }
214             }
215         }
216
217         save();
218
219         // notify the queue as the project might be now tied to different node
220
Hudson.getInstance().getQueue().scheduleMaintenance();
221
222         // this is to reflect the upstream build adjustments done above
223
Hudson.getInstance().rebuildDependencyGraph();
224     }
225
226     private void updateTransientActions() {
227         if(transientActions==null)
228             transientActions = new Vector JavaDoc<Action>(); // happens when loaded from disk
229
synchronized(transientActions) {
230             transientActions.clear();
231             for (BuildStep step : builders) {
232                 Action a = step.getProjectAction(this);
233                 if(a!=null)
234                     transientActions.add(a);
235             }
236             for (BuildStep step : publishers) {
237                 Action a = step.getProjectAction(this);
238                 if(a!=null)
239                     transientActions.add(a);
240             }
241             for (Trigger trigger : triggers) {
242                 Action a = trigger.getProjectAction();
243                 if(a!=null)
244                     transientActions.add(a);
245             }
246         }
247     }
248
249     public synchronized List JavaDoc<Action> getActions() {
250         // add all the transient actions, too
251
List JavaDoc<Action> actions = new Vector JavaDoc<Action>(super.getActions());
252         actions.addAll(transientActions);
253         return actions;
254     }
255
256     public List JavaDoc<ProminentProjectAction> getProminentActions() {
257         List JavaDoc<Action> a = getActions();
258         List JavaDoc<ProminentProjectAction> pa = new Vector JavaDoc<ProminentProjectAction>();
259         for (Action action : a) {
260             if(action instanceof ProminentProjectAction)
261                 pa.add((ProminentProjectAction) action);
262         }
263         return pa;
264     }
265
266     /**
267      * @deprecated
268      * left for legacy config file compatibility
269      */

270     @Deprecated JavaDoc
271     private transient String JavaDoc slave;
272
273     public TopLevelItemDescriptor getDescriptor() {
274         return DESCRIPTOR;
275     }
276
277     public static final TopLevelItemDescriptor DESCRIPTOR = new DescriptorImpl();
278
279     public static final class DescriptorImpl extends TopLevelItemDescriptor {
280         private DescriptorImpl() {
281             super(Project.class);
282         }
283
284         public String JavaDoc getDisplayName() {
285             return "Build a free-style software project";
286         }
287
288         public Project newInstance(String JavaDoc name) {
289             return new Project(Hudson.getInstance(),name);
290         }
291     }
292 }
Popular Tags