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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.matrix;
25
26 import hudson.EnvVars;
27 import hudson.FilePath;
28 import hudson.model.AbstractBuild;
29 import hudson.model.Build;
30 import hudson.model.Node;
31 import hudson.slaves.WorkspaceList;
32 import hudson.slaves.WorkspaceList.Lease;
33 import org.kohsuke.stapler.Ancestor;
34 import org.kohsuke.stapler.Stapler;
35 import org.kohsuke.stapler.StaplerRequest;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.Calendar;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44 * Execution of {@link MatrixConfiguration}.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class MatrixRun extends Build<MatrixConfiguration,MatrixRun> {
49 public MatrixRun(MatrixConfiguration job) throws IOException {
50 super(job);
51 }
52
53 public MatrixRun(MatrixConfiguration job, Calendar timestamp) {
54 super(job, timestamp);
55 }
56
57 public MatrixRun(MatrixConfiguration project, File buildDir) throws IOException {
58 super(project, buildDir);
59 }
60
61 @Override
62 public String getUpUrl() {
63 StaplerRequest req = Stapler.getCurrentRequest();
64 if(req!=null) {
65 List<Ancestor> ancs = req.getAncestors();
66 for( int i=1; i<ancs.size(); i++) {
67 if(ancs.get(i).getObject()==this) {
68 Object parentObj = ancs.get(i-1).getObject();
69 if(parentObj instanceof MatrixBuild || parentObj instanceof MatrixConfiguration) {
70 return ancs.get(i-1).getUrl()+'/';
71 }
72 }
73 }
74 }
75 return super.getDisplayName();
76 }
77
78 /**
79 * Gets the {@link MatrixBuild} that has the same build number.
80 *
81 * @return
82 * null if no such build exists, which happens when the module build
83 * is manually triggered.
84 */
85 public MatrixBuild getParentBuild() {
86 return getParent().getParent().getBuildByNumber(getNumber());
87 }
88
89 /**
90 * The same as {@link #getParentBuild()}.
91 */
92 @Override
93 public AbstractBuild<?, ?> getRootBuild() {
94 return getParentBuild();
95 }
96
97 @Override
98 public String getDisplayName() {
99 StaplerRequest req = Stapler.getCurrentRequest();
100 if(req!=null) {
101 List<Ancestor> ancs = req.getAncestors();
102 for( int i=1; i<ancs.size(); i++) {
103 if(ancs.get(i).getObject()==this) {
104 if(ancs.get(i-1).getObject() instanceof MatrixBuild) {
105 return getParent().getCombination().toCompactString(getParent().getParent().getAxes());
106 }
107 }
108 }
109 }
110 return super.getDisplayName();
111 }
112
113 @Override
114 public Map<String,String> getBuildVariables() {
115 Map<String,String> r = super.getBuildVariables();
116 // pick up user axes
117 AxisList axes = getParent().getParent().getAxes();
118 for (Map.Entry<String,String> e : getParent().getCombination().entrySet()) {
119 Axis a = axes.find(e.getKey());
120 if (a!=null)
121 a.addBuildVariable(e.getValue(),r);
122 else
123 r.put(e.getKey(), e.getValue());
124 }
125 return r;
126 }
127
128 /**
129 * If the parent {@link MatrixBuild} is kept, keep this record too.
130 */
131 @Override
132 public String getWhyKeepLog() {
133 MatrixBuild pb = getParentBuild();
134 if(pb!=null && pb.getWhyKeepLog()!=null)
135 return Messages.MatrixRun_KeptBecauseOfParent(pb);
136 return super.getWhyKeepLog();
137 }
138
139 @Override
140 public MatrixConfiguration getParent() {// don't know why, but javac wants this
141 return super.getParent();
142 }
143
144 @Override
145 public void run() {
146 execute(new MatrixRunExecution());
147 }
148
149 private class MatrixRunExecution extends BuildExecution {
150 protected Lease getParentWorkspaceLease(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
151 MatrixProject mp = getParent().getParent();
152
153 String customWorkspace = mp.getCustomWorkspace();
154 if (customWorkspace != null) {
155 // we allow custom workspaces to be concurrently used between jobs.
156 return Lease.createDummyLease(n.getRootPath().child(getEnvironment(listener).expand(customWorkspace)));
157 }
158 return wsl.allocate(n.getWorkspaceFor(mp), getParentBuild());
159 }
160
161 @Override
162 protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
163 MatrixProject mp = getParent().getParent();
164
165 // lock is done at the parent level, so that concurrent MatrixProjects get respective workspace,
166 // but within MatrixConfigurations that belong to the same MatrixBuild.
167 // if MatrixProject is configured with custom workspace, we assume that the user knows what he's doing
168 // and try not to append unique random suffix.
169 Lease baseLease = getParentWorkspaceLease(n,wsl);
170
171 // resolve the relative path against the parent workspace, which needs locking
172 FilePath baseDir = baseLease.path;
173
174 // prepare variables that can be used in the child workspace setting
175 EnvVars env = getEnvironment(listener);
176 env.put("COMBINATION",getParent().getCombination().toString('/','/')); // e.g., "axis1/a/axis2/b"
177 env.put("SHORT_COMBINATION",getParent().getDigestName()); // e.g., "0fbcab35"
178 env.put("PARENT_WORKSPACE",baseDir.getRemote());
179 env.putAll(getBuildVariables());
180
181 // child workspace need no individual locks, whether or not we use custom workspace
182 String childWs = mp.getChildCustomWorkspace();
183 return Lease.createLinkedDummyLease(baseDir.child(env.expand(childWs)),baseLease);
184 }
185 }
186 }
187