/
Axis.java
302 lines (275 loc) · 9.83 KB
/
Axis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* 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.
*/
package hudson.matrix;
import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.RestrictedSince;
import hudson.Util;
import hudson.matrix.MatrixBuild.MatrixBuildExecution;
import hudson.model.AbstractDescribableImpl;
import jenkins.model.Jenkins;
import hudson.util.QuotedStringTokenizer;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.DataBoundConstructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
/**
* Configuration axis.
*
* <p>
* This class represents a single dimension of the configuration matrix.
* For example, the JAX-WS RI test configuration might include
* one axis "container={glassfish,tomcat,jetty}" and another axis
* "stax={sjsxp,woodstox}", and so on.
*
* @author Kohsuke Kawaguchi
*/
public class Axis extends AbstractDescribableImpl<Axis> implements Comparable<Axis>, Iterable<String>, ExtensionPoint {
/**
* Name of this axis.
* Used as a variable name.
*
* @deprecated as of 1.373
* Use {@link #getName()}
*/
@Deprecated
@Restricted(NoExternalUse.class)
public final String name;
/**
* Possible values for this axis.
*
* @deprecated as of 1.373
* Use {@link #getValues()}
*/
@Deprecated
@Restricted(NoExternalUse.class)
@RestrictedSince("1.463")
public final List<String> values;
public Axis(String name, List<String> values) {
if (values == null || values.isEmpty()) {
values = Collections.emptyList();
}
this.name = name;
this.values = new ArrayList<String>(values);
}
public Axis(String name, String... values) {
this(name,Arrays.asList(values));
}
/**
* Used to build {@link Axis} from form.
*
* Axis with empty values need to be removed later.
*/
@DataBoundConstructor
public Axis(String name, String valueString) {
this.name = name;
this.values = new ArrayList<String>(Arrays.asList(Util.tokenize(valueString)));
}
/**
* Returns true if this axis is a system-reserved axis
* that used to have special treatment.
*
* @deprecated as of 1.373
* System vs user difference are generalized into extension point.
*/
@Deprecated
public boolean isSystem() {
return false;
}
public Iterator<String> iterator() {
return getValues().iterator();
}
public int size() {
return getValues().size();
}
public String value(int index) {
return getValues().get(index);
}
/**
* The inverse of {@link #value(int)}.
*/
public int indexOf(String value) {
return values.indexOf(value);
}
/**
* Axis is fully ordered so that we can convert between a list of axis
* and a string unambiguously.
*/
public int compareTo(Axis that) {
return this.name.compareTo(that.name);
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 41 * hash + (this.values != null ? this.values.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Axis other = (Axis) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (this.values != other.values && (this.values == null || !this.values.equals(other.values))) {
return false;
}
return true;
}
/**
* Name of this axis.
* Used as a variable name.
*/
public String getName() {
return name;
}
/**
* Possible values for this axis.
*/
public List<String> getValues() {
return Collections.unmodifiableList(values);
}
/**
* Called right at the beginning of {@link MatrixBuild} execution to allow {@link Axis} to update {@link #values}
* based on the current build.
*
* <p>
* Historically, axes values are considered static. They were assumed to reflect what the user has typed in,
* and their values are changed only when the project is reconfigured. So abstractions are built around this
* notion, and so for example {@link MatrixProject} has the current axes and their values, which it uses
* to render its UI.
*
* <p>
* So when the need was identified to change the values of axes per build, we decided that this be represented
* as a kind of project configuration update (where a project gets reconfigured every time a build runs), and
* this call back was added to allow {@link Axis} to update the next return value from the {@link #getValues()}
* (which is typically done by updating {@link #values}.)
*
* <p>
* While it is not strictly required, because of these historical reasons, UI will look better if
* Future calls to {@link Axis#getValues()} return the same values as what this method returns (until
* the next rebuild call).
*
* @param context
* The ongoing build. Never null.
* @return
* Never null. Returns the updated set of values.
* @since 1.471
*/
public List<String> rebuild(MatrixBuildExecution context) {
return getValues();
}
@Override
public AxisDescriptor getDescriptor() {
return (AxisDescriptor)super.getDescriptor();
}
@Override
public String toString() {
return new StringBuilder().append(name).append("={").append(String.join(",", values)).append('}').toString();
}
/**
* Used for generating the config UI.
* If the axis is big and occupies a lot of space, use newline for separator
* to display multi-line text.
*/
public String getValueString() {
int len=0;
for (String value : values)
len += value.length();
char delim = len>30 ? '\n' : ' ';
// Build string connected with delimiter, quoting as needed
StringBuilder buf = new StringBuilder(len+values.size()*3);
for (String value : values) {
if (buf.length() != 0) buf.append(delim);
buf.append(QuotedStringTokenizer.quote(value, ""));
}
return buf.toString();
}
/**
* Parses the submitted form (where possible values are
* presented as a list of checkboxes) and creates an axis
*/
public static Axis parsePrefixed(StaplerRequest req, String name) {
List<String> values = new ArrayList<String>();
String prefix = name+'.';
Enumeration<String> e = req.getParameterNames();
while (e.hasMoreElements()) {
String paramName = (String) e.nextElement();
if(paramName.startsWith(prefix))
values.add(paramName.substring(prefix.length()));
}
if(values.isEmpty())
return null;
return new Axis(name,values);
}
/**
* Previously we used to persist {@link Axis}, but now those are divided into subtypes.
* So upon deserialization, resolve to the proper type.
*/
public Object readResolve() {
if (getClass()!=Axis.class) return this;
/*
This method is necessary only because earlier versions of Jenkins treated
axis names "label" and "jdk" differently,
plus Axis was a concrete class, and we need to be able to read that back.
So this measure is not needed for newly added Axes.
*/
if (getName().equals("jdk"))
return new JDKAxis(getValues());
if (getName().equals("label"))
return new LabelAxis(getName(),getValues());
return new TextAxis(getName(),getValues());
}
/**
* Returns all the registered {@link AxisDescriptor}s.
*/
public static DescriptorExtensionList<Axis,AxisDescriptor> all() {
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins != null) {
return jenkins.<Axis,AxisDescriptor>getDescriptorList(Axis.class);
} else {
return DescriptorExtensionList.createDescriptorList((Jenkins)null, Axis.class);
}
}
/**
* Converts the selected value (which is among {@link #values}) and adds that to the given map,
* which serves as the build variables.
*/
public void addBuildVariable(String value, Map<String,String> map) {
map.put(name,value);
}
}