提交 8910df03 编写于 作者: S Stefan Wolf 提交者: Kohsuke Kawaguchi

Made DependencyGroup private in DependencyGraph and not an instance of

Dependency
上级 9e413234
/* /*
* The MIT License * The MIT License
* *
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Martin Eigenbrodt. Seiji Sogabe, Alan Harder * Martin Eigenbrodt. Seiji Sogabe, Alan Harder
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
*/ */
package hudson.model; package hudson.model;
import com.google.common.collect.ImmutableList;
import hudson.security.ACL; import hudson.security.ACL;
import hudson.security.NotSerilizableSecurityContext; import hudson.security.NotSerilizableSecurityContext;
import org.acegisecurity.context.SecurityContext; import org.acegisecurity.context.SecurityContext;
...@@ -61,14 +62,14 @@ import java.util.Stack; ...@@ -61,14 +62,14 @@ import java.util.Stack;
* there's a change (which is relatively rare), a new instance * there's a change (which is relatively rare), a new instance
* will be created. This eliminates the need of synchronization. * will be created. This eliminates the need of synchronization.
* *
* @see Hudson#getDependencyGraph() * @see Hudson#getDependencyGraph()
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
public final class DependencyGraph implements Comparator<AbstractProject> { public final class DependencyGraph implements Comparator<AbstractProject> {
private Map<AbstractProject, List<DependencyGroup>> forward = new HashMap<AbstractProject, List<DependencyGroup>>(); private Map<AbstractProject, List<DependencyGroup>> forward = new HashMap<AbstractProject, List<DependencyGroup>>();
private Map<AbstractProject, List<DependencyGroup>> backward = new HashMap<AbstractProject, List<DependencyGroup>>(); private Map<AbstractProject, List<DependencyGroup>> backward = new HashMap<AbstractProject, List<DependencyGroup>>();
private transient Map<Class<?>, Object> computationalData; private transient Map<Class<?>, Object> computationalData;
private boolean built; private boolean built;
...@@ -113,7 +114,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -113,7 +114,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
public <T> void putComputationalData(Class<T> key, T value) { public <T> void putComputationalData(Class<T> key, T value) {
this.computationalData.put(key, value); this.computationalData.put(key, value);
} }
/** /**
* Gets temporary data which is needed for building up the dependency graph. * Gets temporary data which is needed for building up the dependency graph.
*/ */
...@@ -122,7 +123,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -122,7 +123,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
T result = (T) this.computationalData.get(key); T result = (T) this.computationalData.get(key);
return result; return result;
} }
/** /**
* Gets all the immediate downstream projects (IOW forward edges) of the given project. * Gets all the immediate downstream projects (IOW forward edges) of the given project.
* *
...@@ -147,7 +148,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -147,7 +148,7 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
List<DependencyGroup> v = map.get(src); List<DependencyGroup> v = map.get(src);
if(v==null) return Collections.emptyList(); if(v==null) return Collections.emptyList();
List<AbstractProject> result = new ArrayList<AbstractProject>(v.size()); List<AbstractProject> result = new ArrayList<AbstractProject>(v.size());
for (Dependency d : v) result.add(up ? d.getUpstreamProject() : d.getDownstreamProject()); for (DependencyGroup d : v) result.add(up ? d.getUpstreamProject() : d.getDownstreamProject());
return result; return result;
} }
...@@ -167,8 +168,16 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -167,8 +168,16 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
private List<Dependency> get(Map<AbstractProject, List<DependencyGroup>> map, AbstractProject src) { private List<Dependency> get(Map<AbstractProject, List<DependencyGroup>> map, AbstractProject src) {
List<DependencyGroup> v = map.get(src); List<DependencyGroup> v = map.get(src);
if(v!=null) return Collections.<Dependency>unmodifiableList(v); if(v==null) {
else return Collections.emptyList(); return ImmutableList.of();
} else {
ImmutableList.Builder<Dependency> builder = ImmutableList.builder();
for (DependencyGroup dependencyGroup : v) {
builder.addAll(dependencyGroup.getGroup());
}
return builder.build();
}
} }
/** /**
...@@ -301,101 +310,8 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -301,101 +310,8 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
return Collections.unmodifiableMap(m); return Collections.unmodifiableMap(m);
} }
// /** private static final Comparator<DependencyGroup> NAME_COMPARATOR = new Comparator<DependencyGroup>() {
// * Experimental visualization of project dependencies. public int compare(DependencyGroup lhs, DependencyGroup rhs) {
// */
// public void doGraph( StaplerRequest req, StaplerResponse rsp ) throws IOException {
// // Require admin permission for now (avoid exposing project names with restricted permissions)
// Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
// try {
//
// // creates a dummy graphics just so that we can measure font metrics
// BufferedImage emptyImage = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB );
// Graphics2D graphics = emptyImage.createGraphics();
// graphics.setFont(FONT);
// final FontMetrics fontMetrics = graphics.getFontMetrics();
//
// // TODO: timestamp check
// Layout<AbstractProject> layout = new Layout<AbstractProject>(new Navigator<AbstractProject>() {
// public Collection<AbstractProject> vertices() {
// // only include projects that have some dependency
// List<AbstractProject> r = new ArrayList<AbstractProject>();
// for (AbstractProject p : Hudson.getInstance().getAllItems(AbstractProject.class)) {
// if(!getDownstream(p).isEmpty() || !getUpstream(p).isEmpty())
// r.add(p);
// }
// return r;
// }
//
// public Collection<AbstractProject> edge(AbstractProject p) {
// return getDownstream(p);
// }
//
// public Dimension getSize(AbstractProject p) {
// int w = fontMetrics.stringWidth(p.getDisplayName()) + MARGIN*2;
// return new Dimension(w, fontMetrics.getHeight() + MARGIN*2);
// }
// }, Direction.LEFTRIGHT);
//
// Rectangle area = layout.calcDrawingArea();
// area.grow(4,4); // give it a bit of margin
// BufferedImage image = new BufferedImage(area.width, area.height, BufferedImage.TYPE_INT_RGB );
// Graphics2D g2 = image.createGraphics();
// g2.setTransform(AffineTransform.getTranslateInstance(-area.x,-area.y));
// g2.setPaint(Color.WHITE);
// g2.fill(area);
// g2.setFont(FONT);
//
// g2.setPaint(Color.BLACK);
// for( AbstractProject p : layout.vertices() ) {
// final Point sp = center(layout.vertex(p));
//
// for (AbstractProject q : layout.edges(p)) {
// Point cur=sp;
// for( Point pt : layout.edge(p,q) ) {
// g2.drawLine(cur.x, cur.y, pt.x, pt.y);
// cur=pt;
// }
//
// final Point ep = center(layout.vertex(q));
// g2.drawLine(cur.x, cur.y, ep.x, ep.y);
// }
// }
//
// int diff = fontMetrics.getAscent()+fontMetrics.getLeading()/2;
//
// for( AbstractProject p : layout.vertices() ) {
// Rectangle r = layout.vertex(p);
// g2.setPaint(Color.WHITE);
// g2.fillRect(r.x, r.y, r.width, r.height);
// g2.setPaint(Color.BLACK);
// g2.drawRect(r.x, r.y, r.width, r.height);
// g2.drawString(p.getDisplayName(), r.x+MARGIN, r.y+MARGIN+ diff);
// }
//
// rsp.setContentType("image/png");
// ServletOutputStream os = rsp.getOutputStream();
// ImageIO.write(image, "PNG", os);
// os.close();
// } catch(HeadlessException e) {
// // not available. send out error message
// rsp.sendRedirect2(req.getContextPath()+"/images/headless.png");
// }
// }
//
// private Point center(Rectangle r) {
// return new Point(r.x+r.width/2,r.y+r.height/2);
// }
//
// private static final Font FONT = new Font("TimesRoman", Font.PLAIN, 10);
// /**
// * Margins between the project name and its bounding box.
// */
// private static final int MARGIN = 4;
private static final Comparator<Dependency> NAME_COMPARATOR = new Comparator<Dependency>() {
public int compare(Dependency lhs, Dependency rhs) {
int cmp = lhs.getUpstreamProject().getName().compareTo(rhs.getUpstreamProject().getName()); int cmp = lhs.getUpstreamProject().getName().compareTo(rhs.getUpstreamProject().getName());
return cmp != 0 ? cmp : lhs.getDownstreamProject().getName().compareTo(rhs.getDownstreamProject().getName()); return cmp != 0 ? cmp : lhs.getDownstreamProject().getName().compareTo(rhs.getDownstreamProject().getName());
} }
...@@ -410,10 +326,10 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -410,10 +326,10 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
Set<AbstractProject> o1sdownstreams = getTransitiveDownstream(o1); Set<AbstractProject> o1sdownstreams = getTransitiveDownstream(o1);
Set<AbstractProject> o2sdownstreams = getTransitiveDownstream(o2); Set<AbstractProject> o2sdownstreams = getTransitiveDownstream(o2);
if (o1sdownstreams.contains(o2)) { if (o1sdownstreams.contains(o2)) {
if (o2sdownstreams.contains(o1)) return 0; else return 1; if (o2sdownstreams.contains(o1)) return 0; else return 1;
} else { } else {
if (o2sdownstreams.contains(o1)) return -1; else return 0; if (o2sdownstreams.contains(o1)) return -1; else return 0;
} }
} }
/** /**
...@@ -478,11 +394,12 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -478,11 +394,12 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
/** /**
* Collect multiple dependencies between the same two projects. * Collect multiple dependencies between the same two projects.
*/ */
public static class DependencyGroup extends Dependency { private static class DependencyGroup {
private Set<Dependency> group = new LinkedHashSet<Dependency>(); private Set<Dependency> group = new LinkedHashSet<Dependency>();
DependencyGroup(Dependency first) { DependencyGroup(Dependency first) {
super(first.getUpstreamProject(), first.getDownstreamProject()); this.upstream = first.getUpstreamProject();
this.downstream= first.getDownstreamProject();
group.add(first); group.add(first);
} }
...@@ -493,19 +410,15 @@ public final class DependencyGraph implements Comparator<AbstractProject> { ...@@ -493,19 +410,15 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
public Set<Dependency> getGroup() { public Set<Dependency> getGroup() {
return group; return group;
} }
@Override private AbstractProject upstream, downstream;
public boolean shouldTriggerBuild(AbstractBuild build, TaskListener listener,
List<Action> actions) { public AbstractProject getUpstreamProject() {
List<Action> check = new ArrayList<Action>(); return upstream;
for (Dependency d : group) { }
if (d.shouldTriggerBuild(build, listener, check)) {
actions.addAll(check); public AbstractProject getDownstreamProject() {
return true; return downstream;
} else
check.clear();
}
return false;
} }
} }
} }
...@@ -201,19 +201,7 @@ public class BuildTrigger extends Recorder implements DependecyDeclarer { ...@@ -201,19 +201,7 @@ public class BuildTrigger extends Recorder implements DependecyDeclarer {
} }
}); });
List<Dependency> depsToBuild = new ArrayList<Dependency>();
for (Dependency dep : downstreamProjects) { for (Dependency dep : downstreamProjects) {
if (dep instanceof DependencyGraph.DependencyGroup) {
for (Dependency d : ((DependencyGraph.DependencyGroup)dep).getGroup()) {
depsToBuild.add(d);
}
}
else {
depsToBuild.add(dep);
}
}
for (Dependency dep : depsToBuild) {
AbstractProject p = dep.getDownstreamProject(); AbstractProject p = dep.getDownstreamProject();
if (p.isDisabled()) { if (p.isDisabled()) {
logger.println(Messages.BuildTrigger_Disabled(p.getName())); logger.println(Messages.BuildTrigger_Disabled(p.getName()));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册