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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jean-Baptiste Quenot, Seiji Sogabe, Tom Huybrechts
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.model;
25
26 import hudson.Util;
27 import hudson.EnvVars;
28 import hudson.diagnosis.OldDataMonitor;
29 import hudson.matrix.MatrixChildAction;
30 import hudson.model.Queue.QueueAction;
31 import hudson.model.labels.LabelAssignmentAction;
32 import hudson.model.queue.SubTask;
33 import hudson.tasks.BuildStep;
34 import hudson.tasks.BuildWrapper;
35 import hudson.util.VariableResolver;
36 import org.kohsuke.stapler.export.Exported;
37 import org.kohsuke.stapler.export.ExportedBean;
38
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Set;
46
47 /**
48 * Records the parameter values used for a build.
49 *
50 * <P>
51 * This object is associated with the build record so that we remember what parameters
52 * were used for what build. It is also attached to the queue item to remember parameter
53 * that were specified when scheduling.
54 */
55 @ExportedBean
56 public class ParametersAction implements Action, Iterable<ParameterValue>, QueueAction, EnvironmentContributingAction, LabelAssignmentAction, MatrixChildAction {
57
58 private final List<ParameterValue> parameters;
59
60 /**
61 * @deprecated since 1.283; kept to avoid warnings loading old build data, but now transient.
62 */
63 private transient AbstractBuild<?, ?> build;
64
65 public ParametersAction(List<ParameterValue> parameters) {
66 this.parameters = parameters;
67 }
68
69 public ParametersAction(ParameterValue... parameters) {
70 this(Arrays.asList(parameters));
71 }
72
73 public void createBuildWrappers(AbstractBuild<?,?> build, Collection<? super BuildWrapper> result) {
74 for (ParameterValue p : parameters) {
75 BuildWrapper w = p.createBuildWrapper(build);
76 if(w!=null) result.add(w);
77 }
78 }
79
80 public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {
81 for (ParameterValue p : parameters)
82 p.buildEnvVars(build,env);
83 }
84
85 /**
86 * Performs a variable subsitution to the given text and return it.
87 */
88 public String substitute(AbstractBuild<?,?> build, String text) {
89 return Util.replaceMacro(text,createVariableResolver(build));
90 }
91
92 /**
93 * Creates an {@link VariableResolver} that aggregates all the parameters.
94 *
95 * <p>
96 * If you are a {@link BuildStep}, most likely you should call {@link AbstractBuild#getBuildVariableResolver()}.
97 */
98 public VariableResolver<String> createVariableResolver(AbstractBuild<?,?> build) {
99 VariableResolver[] resolvers = new VariableResolver[parameters.size()+1];
100 int i=0;
101 for (ParameterValue p : parameters)
102 resolvers[i++] = p.createVariableResolver(build);
103
104 resolvers[i] = build.getBuildVariableResolver();
105
106 return new VariableResolver.Union<String>(resolvers);
107 }
108
109 public Iterator<ParameterValue> iterator() {
110 return parameters.iterator();
111 }
112
113 @Exported(visibility=2)
114 public List<ParameterValue> getParameters() {
115 return Collections.unmodifiableList(parameters);
116 }
117
118 public ParameterValue getParameter(String name) {
119 for (ParameterValue p : parameters)
120 if (p.getName().equals(name))
121 return p;
122 return null;
123 }
124
125 public Label getAssignedLabel(SubTask task) {
126 for (ParameterValue p : parameters) {
127 Label l = p.getAssignedLabel(task);
128 if (l!=null) return l;
129 }
130 return null;
131 }
132
133 public String getDisplayName() {
134 return Messages.ParameterAction_DisplayName();
135 }
136
137 public String getIconFileName() {
138 return "document-properties.png";
139 }
140
141 public String getUrlName() {
142 return "parameters";
143 }
144
145 /**
146 * Allow an other build of the same project to be scheduled, if it has other parameters.
147 */
148 public boolean shouldSchedule(List<Action> actions) {
149 List<ParametersAction> others = Util.filter(actions, ParametersAction.class);
150 if (others.isEmpty()) {
151 return !parameters.isEmpty();
152 } else {
153 // I don't think we need multiple ParametersActions, but let's be defensive
154 Set<ParameterValue> params = new HashSet<ParameterValue>();
155 for (ParametersAction other: others) {
156 params.addAll(other.parameters);
157 }
158 return !params.equals(new HashSet<ParameterValue>(this.parameters));
159 }
160 }
161
162 private Object readResolve() {
163 if (build != null)
164 OldDataMonitor.report(build, "1.283");
165 return this;
166 }
167 }
168