ParametersDefinitionProperty.java 8.4 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4 5
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
 * Jean-Baptiste Quenot, Seiji Sogabe, Tom Huybrechts
K
kohsuke 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 
 * 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
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
25 26 27 28
package hudson.model;

import java.io.IOException;
import java.util.ArrayList;
29
import java.util.Arrays;
30 31
import java.util.Collection;
import java.util.Collections;
32
import java.util.List;
33
import java.util.AbstractList;
34 35

import javax.servlet.ServletException;
36

37
import hudson.model.Queue.WaitingItem;
38
import jenkins.model.Jenkins;
39
import jenkins.util.TimeDuration;
40 41 42
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

43
import org.kohsuke.stapler.QueryParameter;
44 45
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
46 47 48
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

49
import hudson.Extension;
50
import javax.annotation.CheckForNull;
51
import org.kohsuke.stapler.export.Flavor;
52

53 54
import static javax.servlet.http.HttpServletResponse.SC_CREATED;

55 56 57 58 59 60 61
/**
 * Keeps a list of the parameters defined for a project.
 *
 * <p>
 * This class also implements {@link Action} so that <tt>index.jelly</tt> provides
 * a form to enter build parameters. 
 */
62
@ExportedBean(defaultVisibility=2)
63 64 65 66 67 68 69 70 71
public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?, ?>>
        implements Action {

    private final List<ParameterDefinition> parameterDefinitions;

    public ParametersDefinitionProperty(List<ParameterDefinition> parameterDefinitions) {
        this.parameterDefinitions = parameterDefinitions;
    }

72 73 74 75
    public ParametersDefinitionProperty(ParameterDefinition... parameterDefinitions) {
        this.parameterDefinitions = Arrays.asList(parameterDefinitions);
    }
    
76 77 78 79
    public AbstractProject<?,?> getOwner() {
        return owner;
    }

80
    @Exported
81 82 83 84
    public List<ParameterDefinition> getParameterDefinitions() {
        return parameterDefinitions;
    }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    /**
     * Gets the names of all the parameter definitions.
     */
    public List<String> getParameterDefinitionNames() {
        return new AbstractList<String>() {
            public String get(int index) {
                return parameterDefinitions.get(index).getName();
            }

            public int size() {
                return parameterDefinitions.size();
            }
        };
    }

100
    @Override
101 102
    public Collection<Action> getJobActions(AbstractProject<?, ?> job) {
        return Collections.<Action>singleton(this);
103 104 105 106 107 108
    }

    public AbstractProject<?, ?> getProject() {
        return (AbstractProject<?, ?>) owner;
    }

109 110
    /** @deprecated use {@link #_doBuild(StaplerRequest, StaplerResponse, TimeDuration)} */
    @Deprecated
111 112 113 114
    public void _doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        _doBuild(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
    }

115 116 117 118
    /**
     * Interprets the form submission and schedules a build for a parameterized job.
     *
     * <p>
119
     * This method is supposed to be invoked from {@link AbstractProject#doBuild(StaplerRequest, StaplerResponse, TimeDuration)}.
120
     */
121 122 123
    public void _doBuild(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException {
        if (delay==null)    delay=new TimeDuration(owner.getQuietPeriod());

124 125

        List<ParameterValue> values = new ArrayList<ParameterValue>();
126
        
127 128 129 130 131 132 133 134 135 136
        JSONObject formData = req.getSubmittedForm();
        JSONArray a = JSONArray.fromObject(formData.get("parameter"));

        for (Object o : a) {
            JSONObject jo = (JSONObject) o;
            String name = jo.getString("name");

            ParameterDefinition d = getParameterDefinition(name);
            if(d==null)
                throw new IllegalArgumentException("No such parameter definition: " + name);
H
huybrechts 已提交
137 138
            ParameterValue parameterValue = d.createValue(req, jo);
            values.add(parameterValue);
139 140
        }

141
    	WaitingItem item = Jenkins.getInstance().getQueue().schedule(
142
                owner, delay.getTime(), new ParametersAction(values), new CauseAction(new Cause.UserIdCause()));
143 144 145 146 147
        if (item!=null)
            rsp.sendRedirect(SC_CREATED,req.getContextPath()+'/'+item.getUrl());
        else
            // send the user back to the job top page.
            rsp.sendRedirect(".");
148
    }
149

150 151
    /** @deprecated use {@link #buildWithParameters(StaplerRequest, StaplerResponse, TimeDuration)} */
    @Deprecated
152 153 154 155
    public void buildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        buildWithParameters(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
    }

156
    public void buildWithParameters(StaplerRequest req, StaplerResponse rsp, @CheckForNull TimeDuration delay) throws IOException, ServletException {
157 158 159 160 161 162 163
        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (ParameterDefinition d: parameterDefinitions) {
        	ParameterValue value = d.createValue(req);
        	if (value != null) {
        		values.add(value);
        	}
        }
164
        if (delay==null)    delay=new TimeDuration(owner.getQuietPeriod());
165

166
        Jenkins.getInstance().getQueue().schedule(
167
                owner, delay.getTime(), new ParametersAction(values), owner.getBuildCause(req));
168

169
        if (requestWantsJson(req)) {
170 171 172 173 174 175
            rsp.setContentType("application/json");
            rsp.serveExposedBean(req, owner, Flavor.JSON);
        } else {
            // send the user back to the job top page.
            rsp.sendRedirect(".");
        }
176 177
    }

178 179 180 181 182 183
    private boolean requestWantsJson(StaplerRequest req) {
        String a = req.getHeader("Accept");
        if (a==null)    return false;
        return !a.contains("text/html") && a.contains("application/json");
    }

184 185 186 187 188 189 190 191 192 193
    /**
     * Gets the {@link ParameterDefinition} of the given name, if any.
     */
    public ParameterDefinition getParameterDefinition(String name) {
        for (ParameterDefinition pd : parameterDefinitions)
            if (pd.getName().equals(name))
                return pd;
        return null;
    }

194
    @Extension
195 196 197 198 199 200 201 202 203 204 205 206 207
    public static class DescriptorImpl extends JobPropertyDescriptor {
        @Override
        public boolean isApplicable(Class<? extends Job> jobType) {
            return AbstractProject.class.isAssignableFrom(jobType);
        }

        @Override
        public JobProperty<?> newInstance(StaplerRequest req,
                                          JSONObject formData) throws FormException {
            if (formData.isNullObject()) {
                return null;
            }

208 209 210
            JSONObject parameterized = formData.getJSONObject("parameterized");

            if (parameterized.isNullObject()) {
211 212 213
            	return null;
            }
            
214
            List<ParameterDefinition> parameterDefinitions = Descriptor.newInstancesFromHeteroList(
215
                    req, parameterized, "parameter", ParameterDefinition.all());
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            if(parameterDefinitions.isEmpty())
                return null;

            return new ParametersDefinitionProperty(parameterDefinitions);
        }

        @Override
        public String getDisplayName() {
            return Messages.ParametersDefinitionProperty_DisplayName();
        }
    }

    public String getDisplayName() {
        return null;
    }

    public String getIconFileName() {
        return null;
    }

    public String getUrlName() {
237
        return null;
238 239
    }
}