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 ; 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.Vector ; 26 27 32 public class Project extends AbstractProject<Project,Build> implements TopLevelItem, SCMedItem { 33 34 37 private List <Builder> builders = new Vector <Builder>(); 38 39 42 private List <Publisher> publishers = new Vector <Publisher>(); 43 44 47 private List <BuildWrapper> buildWrappers = new Vector <BuildWrapper>(); 48 49 56 private transient List <Action> transientActions = new Vector <Action>(); 57 58 61 public Project(Hudson parent,String name) { 62 super(parent,name); 63 } 64 65 public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { 66 super.onLoad(parent, name); 67 68 if(buildWrappers==null) 69 buildWrappers = new Vector <BuildWrapper>(); 71 72 updateTransientActions(); 73 } 74 75 public AbstractProject<?, ?> asProject() { 76 return this; 77 } 78 79 @Override 80 public Hudson getParent() { 81 return Hudson.getInstance(); 82 } 83 84 @Override 85 public FilePath getWorkspace() { 86 Node node = getLastBuiltOn(); 87 if(node==null) node = getParent(); 88 return node.getWorkspaceFor(this); 89 } 90 91 @Override 92 public BallColor getIconColor() { 93 if(isDisabled()) 94 return BallColor.GREY; 96 else 97 return super.getIconColor(); 98 } 99 100 public synchronized Map <Descriptor<Builder>,Builder> getBuilders() { 101 return Descriptor.toMap(builders); 102 } 103 104 public synchronized Map <Descriptor<Publisher>,Publisher> getPublishers() { 105 return Descriptor.toMap(publishers); 106 } 107 108 public synchronized Map <Descriptor<BuildWrapper>,BuildWrapper> getBuildWrappers() { 109 return Descriptor.toMap(buildWrappers); 110 } 111 112 115 private void addPublisher(Publisher buildStep) throws IOException { 116 addToList(buildStep,publishers); 117 } 118 119 122 private void removePublisher(Descriptor<Publisher> descriptor) throws IOException { 123 removeFromList(descriptor, publishers); 124 } 125 126 @Override 127 public Build newBuild() throws IOException { 128 Build lastBuild = new Build(this); 129 builds.put(lastBuild); 130 return lastBuild; 131 } 132 133 @Override 134 protected Build loadBuild(File dir) throws IOException { 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 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 165 public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 166 Set <AbstractProject> upstream = Collections.emptySet(); 167 if(req.getParameter("pseudoUpstreamTrigger")!=null) { 168 upstream = new HashSet <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 Hudson.getInstance().rebuildDependencyGraph(); 192 193 197 for (Project p : Hudson.getInstance().getProjects()) { 198 boolean isUpstream = upstream.contains(p); 199 synchronized(p) { 200 List <AbstractProject> newChildProjects = new ArrayList <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 Hudson.getInstance().getQueue().scheduleMaintenance(); 221 222 Hudson.getInstance().rebuildDependencyGraph(); 224 } 225 226 private void updateTransientActions() { 227 if(transientActions==null) 228 transientActions = new Vector <Action>(); 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 <Action> getActions() { 250 List <Action> actions = new Vector <Action>(super.getActions()); 252 actions.addAll(transientActions); 253 return actions; 254 } 255 256 public List <ProminentProjectAction> getProminentActions() { 257 List <Action> a = getActions(); 258 List <ProminentProjectAction> pa = new Vector <ProminentProjectAction>(); 259 for (Action action : a) { 260 if(action instanceof ProminentProjectAction) 261 pa.add((ProminentProjectAction) action); 262 } 263 return pa; 264 } 265 266 270 @Deprecated 271 private transient String 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 getDisplayName() { 285 return "Build a free-style software project"; 286 } 287 288 public Project newInstance(String name) { 289 return new Project(Hudson.getInstance(),name); 290 } 291 } 292 } | Popular Tags |