Skip to content

Commit

Permalink
Made DependencyGroup private in DependencyGraph and not an instance of
Browse files Browse the repository at this point in the history
Dependency
  • Loading branch information
wolfs authored and kohsuke committed Jun 7, 2011
1 parent 9e41323 commit 8910df0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 136 deletions.
161 changes: 37 additions & 124 deletions core/src/main/java/hudson/model/DependencyGraph.java
@@ -1,19 +1,19 @@
/*
* The MIT License
*
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Martin Eigenbrodt. Seiji Sogabe, Alan Harder
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -24,6 +24,7 @@
*/
package hudson.model;

import com.google.common.collect.ImmutableList;
import hudson.security.ACL;
import hudson.security.NotSerilizableSecurityContext;
import org.acegisecurity.context.SecurityContext;
Expand Down Expand Up @@ -61,14 +62,14 @@
* there's a change (which is relatively rare), a new instance
* will be created. This eliminates the need of synchronization.
*
* @see Hudson#getDependencyGraph()
* @see Hudson#getDependencyGraph()
* @author Kohsuke Kawaguchi
*/
public final class DependencyGraph implements Comparator<AbstractProject> {

private Map<AbstractProject, List<DependencyGroup>> forward = new HashMap<AbstractProject, List<DependencyGroup>>();
private Map<AbstractProject, List<DependencyGroup>> backward = new HashMap<AbstractProject, List<DependencyGroup>>();

private transient Map<Class<?>, Object> computationalData;

private boolean built;
Expand Down Expand Up @@ -113,7 +114,7 @@ private DependencyGraph(boolean dummy) {
public <T> void putComputationalData(Class<T> key, T value) {
this.computationalData.put(key, value);
}

/**
* Gets temporary data which is needed for building up the dependency graph.
*/
Expand All @@ -122,7 +123,7 @@ public <T> T getComputationalData(Class<T> key) {
T result = (T) this.computationalData.get(key);
return result;
}

/**
* Gets all the immediate downstream projects (IOW forward edges) of the given project.
*
Expand All @@ -147,7 +148,7 @@ private List<AbstractProject> get(Map<AbstractProject, List<DependencyGroup>> ma
List<DependencyGroup> v = map.get(src);
if(v==null) return Collections.emptyList();
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;
}

Expand All @@ -167,8 +168,16 @@ public List<Dependency> getUpstreamDependencies(AbstractProject p) {

private List<Dependency> get(Map<AbstractProject, List<DependencyGroup>> map, AbstractProject src) {
List<DependencyGroup> v = map.get(src);
if(v!=null) return Collections.<Dependency>unmodifiableList(v);
else return Collections.emptyList();
if(v==null) {
return ImmutableList.of();
} else {
ImmutableList.Builder<Dependency> builder = ImmutableList.builder();
for (DependencyGroup dependencyGroup : v) {
builder.addAll(dependencyGroup.getGroup());
}
return builder.build();
}

}

/**
Expand Down Expand Up @@ -301,101 +310,8 @@ private Map<AbstractProject, List<DependencyGroup>> finalize(Map<AbstractProject
return Collections.unmodifiableMap(m);
}

// /**
// * Experimental visualization of project dependencies.
// */
// 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) {
private static final Comparator<DependencyGroup> NAME_COMPARATOR = new Comparator<DependencyGroup>() {
public int compare(DependencyGroup lhs, DependencyGroup rhs) {
int cmp = lhs.getUpstreamProject().getName().compareTo(rhs.getUpstreamProject().getName());
return cmp != 0 ? cmp : lhs.getDownstreamProject().getName().compareTo(rhs.getDownstreamProject().getName());
}
Expand All @@ -410,10 +326,10 @@ public int compare(AbstractProject o1, AbstractProject o2) {
Set<AbstractProject> o1sdownstreams = getTransitiveDownstream(o1);
Set<AbstractProject> o2sdownstreams = getTransitiveDownstream(o2);
if (o1sdownstreams.contains(o2)) {
if (o2sdownstreams.contains(o1)) return 0; else return 1;
if (o2sdownstreams.contains(o1)) return 0; else return 1;
} else {
if (o2sdownstreams.contains(o1)) return -1; else return 0;
}
if (o2sdownstreams.contains(o1)) return -1; else return 0;
}
}

/**
Expand Down Expand Up @@ -478,11 +394,12 @@ public int hashCode() {
/**
* 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>();

DependencyGroup(Dependency first) {
super(first.getUpstreamProject(), first.getDownstreamProject());
this.upstream = first.getUpstreamProject();
this.downstream= first.getDownstreamProject();
group.add(first);
}

Expand All @@ -493,19 +410,15 @@ private void add(Dependency next) {
public Set<Dependency> getGroup() {
return group;
}

@Override
public boolean shouldTriggerBuild(AbstractBuild build, TaskListener listener,
List<Action> actions) {
List<Action> check = new ArrayList<Action>();
for (Dependency d : group) {
if (d.shouldTriggerBuild(build, listener, check)) {
actions.addAll(check);
return true;
} else
check.clear();
}
return false;

private AbstractProject upstream, downstream;

public AbstractProject getUpstreamProject() {
return upstream;
}

public AbstractProject getDownstreamProject() {
return downstream;
}
}
}
12 changes: 0 additions & 12 deletions core/src/main/java/hudson/tasks/BuildTrigger.java
Expand Up @@ -201,19 +201,7 @@ public int compare(Dependency lhs, Dependency rhs) {
}
});

List<Dependency> depsToBuild = new ArrayList<Dependency>();
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();
if (p.isDisabled()) {
logger.println(Messages.BuildTrigger_Disabled(p.getName()));
Expand Down

0 comments on commit 8910df0

Please sign in to comment.