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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5 * Jean-Baptiste Quenot, Seiji Sogabe, Tom Huybrechts
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.model;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.AbstractList;
34
35 import javax.servlet.ServletException;
36
37 import jenkins.model.Jenkins;
38 import jenkins.util.TimeDuration;
39 import net.sf.json.JSONArray;
40 import net.sf.json.JSONObject;
41
42 import org.kohsuke.stapler.QueryParameter;
43 import org.kohsuke.stapler.StaplerRequest;
44 import org.kohsuke.stapler.StaplerResponse;
45 import org.kohsuke.stapler.export.Exported;
46 import org.kohsuke.stapler.export.ExportedBean;
47
48 import hudson.Extension;
49 import org.kohsuke.stapler.export.Flavor;
50
51 /**
52 * Keeps a list of the parameters defined for a project.
53 *
54 * <p>
55 * This class also implements {@link Action} so that <tt>index.jelly</tt> provides
56 * a form to enter build parameters.
57 */
58 @ExportedBean(defaultVisibility=2)
59 public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?, ?>>
60 implements Action {
61
62 private final List<ParameterDefinition> parameterDefinitions;
63
64 public ParametersDefinitionProperty(List<ParameterDefinition> parameterDefinitions) {
65 this.parameterDefinitions = parameterDefinitions;
66 }
67
68 public ParametersDefinitionProperty(ParameterDefinition... parameterDefinitions) {
69 this.parameterDefinitions = Arrays.asList(parameterDefinitions);
70 }
71
72 public AbstractProject<?,?> getOwner() {
73 return owner;
74 }
75
76 @Exported
77 public List<ParameterDefinition> getParameterDefinitions() {
78 return parameterDefinitions;
79 }
80
81 /**
82 * Gets the names of all the parameter definitions.
83 */
84 public List<String> getParameterDefinitionNames() {
85 return new AbstractList<String>() {
86 public String get(int index) {
87 return parameterDefinitions.get(index).getName();
88 }
89
90 public int size() {
91 return parameterDefinitions.size();
92 }
93 };
94 }
95
96 @Override
97 public Collection<Action> getJobActions(AbstractProject<?, ?> job) {
98 return Collections.<Action>singleton(this);
99 }
100
101 public AbstractProject<?, ?> getProject() {
102 return (AbstractProject<?, ?>) owner;
103 }
104
105 public void _doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
106 _doBuild(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
107 }
108
109 /**
110 * Interprets the form submission and schedules a build for a parameterized job.
111 *
112 * <p>
113 * This method is supposed to be invoked from {@link AbstractProject#doBuild(StaplerRequest, StaplerResponse, TimeDuration)}.
114 */
115 public void _doBuild(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException {
116 if (delay==null) delay=new TimeDuration(owner.getQuietPeriod());
117
118
119 List<ParameterValue> values = new ArrayList<ParameterValue>();
120
121 JSONObject formData = req.getSubmittedForm();
122 JSONArray a = JSONArray.fromObject(formData.get("parameter"));
123
124 for (Object o : a) {
125 JSONObject jo = (JSONObject) o;
126 String name = jo.getString("name");
127
128 ParameterDefinition d = getParameterDefinition(name);
129 if(d==null)
130 throw new IllegalArgumentException("No such parameter definition: " + name);
131 ParameterValue parameterValue = d.createValue(req, jo);
132 values.add(parameterValue);
133 }
134
135 Jenkins.getInstance().getQueue().schedule(
136 owner, delay.getTime(), new ParametersAction(values), new CauseAction(new Cause.UserIdCause()));
137
138 // send the user back to the job top page.
139 rsp.sendRedirect(".");
140 }
141
142 public void buildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
143 buildWithParameters(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
144 }
145
146 public void buildWithParameters(StaplerRequest req, StaplerResponse rsp, TimeDuration delay) throws IOException, ServletException {
147 List<ParameterValue> values = new ArrayList<ParameterValue>();
148 for (ParameterDefinition d: parameterDefinitions) {
149 ParameterValue value = d.createValue(req);
150 if (value != null) {
151 values.add(value);
152 }
153 }
154 if (delay==null) delay=new TimeDuration(owner.getQuietPeriod());
155
156 Jenkins.getInstance().getQueue().schedule(
157 owner, delay.getTime(), new ParametersAction(values), owner.getBuildCause(req));
158
159 if (requestWantsJson(req)) {
160 rsp.setContentType("application/json");
161 rsp.serveExposedBean(req, owner, Flavor.JSON);
162 } else {
163 // send the user back to the job top page.
164 rsp.sendRedirect(".");
165 }
166 }
167
168 private boolean requestWantsJson(StaplerRequest req) {
169 String a = req.getHeader("Accept");
170 if (a==null) return false;
171 return !a.contains("text/html") && a.contains("application/json");
172 }
173
174 /**
175 * Gets the {@link ParameterDefinition} of the given name, if any.
176 */
177 public ParameterDefinition getParameterDefinition(String name) {
178 for (ParameterDefinition pd : parameterDefinitions)
179 if (pd.getName().equals(name))
180 return pd;
181 return null;
182 }
183
184 @Extension
185 public static class DescriptorImpl extends JobPropertyDescriptor {
186 @Override
187 public boolean isApplicable(Class<? extends Job> jobType) {
188 return AbstractProject.class.isAssignableFrom(jobType);
189 }
190
191 @Override
192 public JobProperty<?> newInstance(StaplerRequest req,
193 JSONObject formData) throws FormException {
194 if (formData.isNullObject()) {
195 return null;
196 }
197
198 JSONObject parameterized = formData.getJSONObject("parameterized");
199
200 if (parameterized.isNullObject()) {
201 return null;
202 }
203
204 List<ParameterDefinition> parameterDefinitions = Descriptor.newInstancesFromHeteroList(
205 req, parameterized, "parameter", ParameterDefinition.all());
206 if(parameterDefinitions.isEmpty())
207 return null;
208
209 return new ParametersDefinitionProperty(parameterDefinitions);
210 }
211
212 @Override
213 public String getDisplayName() {
214 return Messages.ParametersDefinitionProperty_DisplayName();
215 }
216 }
217
218 public String getDisplayName() {
219 return null;
220 }
221
222 public String getIconFileName() {
223 return null;
224 }
225
226 public String getUrlName() {
227 return null;
228 }
229 }
230