ParametersDefinitionProperty.java 8.1 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 org.kohsuke.stapler.export.Flavor;
51

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

54 55 56 57 58 59 60
/**
 * 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. 
 */
61
@ExportedBean(defaultVisibility=2)
62 63 64 65 66 67 68 69 70
public class ParametersDefinitionProperty extends JobProperty<AbstractProject<?, ?>>
        implements Action {

    private final List<ParameterDefinition> parameterDefinitions;

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

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

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

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    /**
     * 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();
            }
        };
    }

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

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

108 109 110 111
    public void _doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        _doBuild(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
    }

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

121 122

        List<ParameterValue> values = new ArrayList<ParameterValue>();
123
        
124 125 126 127 128 129 130 131 132 133
        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 已提交
134 135
            ParameterValue parameterValue = d.createValue(req, jo);
            values.add(parameterValue);
136 137
        }

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

147 148 149 150
    public void buildWithParameters(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        buildWithParameters(req,rsp,TimeDuration.fromString(req.getParameter("delay")));
    }

151
    public void buildWithParameters(StaplerRequest req, StaplerResponse rsp, TimeDuration delay) throws IOException, ServletException {
152 153 154 155 156 157 158
        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (ParameterDefinition d: parameterDefinitions) {
        	ParameterValue value = d.createValue(req);
        	if (value != null) {
        		values.add(value);
        	}
        }
159
        if (delay==null)    delay=new TimeDuration(owner.getQuietPeriod());
160

161
        Jenkins.getInstance().getQueue().schedule(
162
                owner, delay.getTime(), new ParametersAction(values), owner.getBuildCause(req));
163

164
        if (requestWantsJson(req)) {
165 166 167 168 169 170
            rsp.setContentType("application/json");
            rsp.serveExposedBean(req, owner, Flavor.JSON);
        } else {
            // send the user back to the job top page.
            rsp.sendRedirect(".");
        }
171 172
    }

173 174 175 176 177 178
    private boolean requestWantsJson(StaplerRequest req) {
        String a = req.getHeader("Accept");
        if (a==null)    return false;
        return !a.contains("text/html") && a.contains("application/json");
    }

179 180 181 182 183 184 185 186 187 188
    /**
     * 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;
    }

189
    @Extension
190 191 192 193 194 195 196 197 198 199 200 201 202
    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;
            }

203 204 205
            JSONObject parameterized = formData.getJSONObject("parameterized");

            if (parameterized.isNullObject()) {
206 207 208
            	return null;
            }
            
209
            List<ParameterDefinition> parameterDefinitions = Descriptor.newInstancesFromHeteroList(
210
                    req, parameterized, "parameter", ParameterDefinition.all());
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            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() {
232
        return null;
233 234
    }
}