KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > BuildTrigger


1 package hudson.tasks;
2
3 import hudson.Launcher;
4 import hudson.model.AbstractProject;
5 import hudson.model.Build;
6 import hudson.model.BuildListener;
7 import hudson.model.Descriptor;
8 import hudson.model.Hudson;
9 import hudson.model.Item;
10 import hudson.model.Items;
11 import hudson.model.Job;
12 import hudson.model.Result;
13 import hudson.util.FormFieldValidator;
14 import org.kohsuke.stapler.StaplerRequest;
15 import org.kohsuke.stapler.StaplerResponse;
16
17 import javax.servlet.ServletException JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * Triggers builds of other projects.
25  *
26  * @author Kohsuke Kawaguchi
27  */

28 public class BuildTrigger extends Publisher {
29
30     /**
31      * Comma-separated list of other projects to be scheduled.
32      */

33     private String JavaDoc childProjects;
34
35     public BuildTrigger(String JavaDoc childProjects) {
36         this.childProjects = childProjects;
37     }
38
39     public BuildTrigger(List JavaDoc<AbstractProject> childProjects) {
40         this(Items.toNameList(childProjects));
41     }
42
43     public String JavaDoc getChildProjectsValue() {
44         return childProjects;
45     }
46
47     public List JavaDoc<AbstractProject> getChildProjects() {
48         return Items.fromNameList(childProjects,AbstractProject.class);
49     }
50
51     public boolean perform(Build build, Launcher launcher, BuildListener listener) {
52         if(build.getResult()== Result.SUCCESS) {
53             PrintStream JavaDoc logger = listener.getLogger();
54             for (AbstractProject p : getChildProjects()) {
55                 if(p.isDisabled()) {
56                     logger.println(p.getName()+" is disabled. Triggering skiiped");
57                     continue;
58                 }
59
60                 // this is not completely accurate, as a new build might be triggered
61
// between these calls
62
String JavaDoc name = p.getName()+" #"+p.getNextBuildNumber();
63                 if(!p.scheduleBuild()) {
64                     logger.println("Triggering a new build of "+name);
65                 } else {
66                     logger.println(name+" is already in the queue");
67                 }
68             }
69         }
70
71         return true;
72     }
73
74     /**
75      * Called from {@link Job#renameTo(String)} when a job is renamed.
76      *
77      * @return true
78      * if this {@link BuildTrigger} is changed and needs to be saved.
79      */

80     public boolean onJobRenamed(String JavaDoc oldName, String JavaDoc newName) {
81         // quick test
82
if(!childProjects.contains(oldName))
83             return false;
84
85         boolean changed = false;
86
87         // we need to do this per string, since old Project object is already gone.
88
String JavaDoc[] projects = childProjects.split(",");
89         for( int i=0; i<projects.length; i++ ) {
90             if(projects[i].trim().equals(oldName)) {
91                 projects[i] = newName;
92                 changed = true;
93             }
94         }
95
96         if(changed) {
97             StringBuilder JavaDoc b = new StringBuilder JavaDoc();
98             for (String JavaDoc p : projects) {
99                 if(b.length()>0) b.append(',');
100                 b.append(p);
101             }
102             childProjects = b.toString();
103         }
104
105         return changed;
106     }
107
108     public Descriptor<Publisher> getDescriptor() {
109         return DESCRIPTOR;
110     }
111
112
113     public static final Descriptor<Publisher> DESCRIPTOR = new DescriptorImpl();
114
115     public static class DescriptorImpl extends Descriptor<Publisher> {
116         public DescriptorImpl() {
117             super(BuildTrigger.class);
118         }
119
120         public String JavaDoc getDisplayName() {
121             return "Build other projects";
122         }
123
124         public Publisher newInstance(StaplerRequest req) {
125             return new BuildTrigger(req.getParameter("childProjects"));
126         }
127
128         /**
129          * Form validation method.
130          */

131         public void doCheck( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
132             new FormFieldValidator(req,rsp,true) {
133                 protected void check() throws IOException JavaDoc, ServletException JavaDoc {
134                     String JavaDoc list = request.getParameter("value");
135
136                     StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(list,",");
137                     while(tokens.hasMoreTokens()) {
138                         String JavaDoc projectName = tokens.nextToken().trim();
139                         Item item = Hudson.getInstance().getItemByFullName(projectName,Item.class);
140                         if(item==null) {
141                             error("No such project '"+projectName+"'. Did you mean '"+
142                                 AbstractProject.findNearest(projectName).getName()+"'?");
143                             return;
144                         }
145                         if(!(item instanceof AbstractProject)) {
146                             error(projectName+" is not buildable");
147                             return;
148                         }
149                     }
150
151                     ok();
152                 }
153             }.process();
154         }
155     }
156 }
157
Popular Tags