File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\DependencyRunner.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5 * Brian Westrich, Jean-Baptiste Quenot
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package hudson;
26
27 import hudson.model.AbstractProject;
28 import jenkins.model.Jenkins;
29 import hudson.security.ACL;
30
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.Collection;
36 import java.util.logging.Logger;
37
38 import org.acegisecurity.Authentication;
39 import org.acegisecurity.context.SecurityContext;
40 import org.acegisecurity.context.SecurityContextHolder;
41
42 /**
43 * Runs a job on all projects in the order of dependencies
44 */
45 public class DependencyRunner implements Runnable {
46
47 private static final Logger LOGGER = Logger.getLogger(DependencyRunner.class.getName());
48
49 ProjectRunnable runnable;
50
51 List<AbstractProject> polledProjects = new ArrayList<AbstractProject>();
52
53 public DependencyRunner(ProjectRunnable runnable) {
54 this.runnable = runnable;
55 }
56
57 public void run() {
58 SecurityContext oldContext = ACL.impersonate(ACL.SYSTEM);
59 try {
60 Set<AbstractProject> topLevelProjects = new HashSet<AbstractProject>();
61 // Get all top-level projects
62 LOGGER.fine("assembling top level projects");
63 for (AbstractProject p : Jenkins.getInstance().getAllItems(AbstractProject.class))
64 if (p.getUpstreamProjects().size() == 0) {
65 LOGGER.fine("adding top level project " + p.getName());
66 topLevelProjects.add(p);
67 } else {
68 LOGGER.fine("skipping project since not a top level project: " + p.getName());
69 }
70 populate(topLevelProjects);
71 for (AbstractProject p : polledProjects) {
72 LOGGER.fine("running project in correct dependency order: " + p.getName());
73 runnable.run(p);
74 }
75 } finally {
76 SecurityContextHolder.setContext(oldContext);
77 }
78 }
79
80 private void populate(Collection<? extends AbstractProject> projectList) {
81 for (AbstractProject<?,?> p : projectList) {
82 if (polledProjects.contains(p)) {
83 // Project will be readded at the queue, so that we always use
84 // the longest path
85 LOGGER.fine("removing project " + p.getName() + " for re-add");
86 polledProjects.remove(p);
87 }
88
89 LOGGER.fine("adding project " + p.getName());
90 polledProjects.add(p);
91
92 // Add all downstream dependencies
93 populate(p.getDownstreamProjects());
94 }
95 }
96
97 public interface ProjectRunnable {
98 void run(AbstractProject p);
99 }
100 }
101