-
Notifications
You must be signed in to change notification settings - Fork 711
/
DataBoundConfigurator.java
330 lines (282 loc) · 13.7 KB
/
DataBoundConfigurator.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package io.jenkins.plugins.casc.impl.configurators;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Descriptor;
import hudson.util.Secret;
import io.jenkins.plugins.casc.Attribute;
import io.jenkins.plugins.casc.BaseConfigurator;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.Configurator;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.impl.attributes.DescribableAttribute;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.PostConstruct;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.ClassDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Stapler;
import static com.google.common.base.Defaults.defaultValue;
/**
* A generic {@link Configurator} to configure components with a {@link org.kohsuke.stapler.DataBoundConstructor}.
* Intended to replicate Stapler's request-to-instance lifecycle, including {@link PostConstruct} init methods.
* Will rely on <a href="https://github.com/jenkinsci/jep/tree/master/jep/205">JEP-205</a> once implemented
*
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class DataBoundConfigurator<T> extends BaseConfigurator<T> {
private static final Logger LOGGER = Logger.getLogger(DataBoundConfigurator.class.getName());
private final Class<T> target;
public DataBoundConfigurator(Class<T> clazz) {
this.target = clazz;
}
@CheckForNull
public static Constructor getDataBoundConstructor(@NonNull Class type) {
for (Constructor c : type.getConstructors()) {
if (c.getAnnotation(DataBoundConstructor.class) != null) return c;
}
return null;
}
@Override
public Class getTarget() {
return target;
}
/**
* Build a fresh new component based on provided configuration and {@link org.kohsuke.stapler.DataBoundConstructor}
*/
@Override
protected T instance(Mapping config, ConfigurationContext context) throws ConfiguratorException {
final Constructor dataBoundConstructor = getDataBoundConstructor();
return tryConstructor((Constructor<T>) dataBoundConstructor, config, context);
}
@NonNull
@Override
public T configure(CNode c, ConfigurationContext context) throws ConfiguratorException {
T object = super.configure(c, context);
for (Method method : target.getMethods()) {
if (method.getParameterCount() == 0 && method.getAnnotation(PostConstruct.class) != null) {
try {
method.invoke(object, null);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new ConfiguratorException(this, "Failed to invoke configurator method " + method, e);
}
}
}
return object;
}
@Override
public T check(CNode config, ConfigurationContext context) throws ConfiguratorException {
// As DataBound objects are replaced in jenkins model we can build one from configuration without side-effects
// BUT we don't invoke @PostConstruct methods which might un some post-build registration into jenkins APIs.
return super.configure(config, context);
}
private T tryConstructor(Constructor<T> constructor, Mapping config, ConfigurationContext context) throws ConfiguratorException {
final Parameter[] parameters = constructor.getParameters();
final String[] names = ClassDescriptor.loadParameterNames(constructor);
Object[] args = new Object[names.length];
if (parameters.length > 0) {
// Many jenkins components haven't been migrated to @DataBoundSetter vs @NotNull constructor parameters
// as a result it might be valid to reference a describable without parameters
for (int i = 0; i < names.length; i++) {
final CNode value = config.get(names[i]);
final Class t = parameters[i].getType();
Class<?> clazz = constructor.getDeclaringClass();
if (value == null && (parameters[i].isAnnotationPresent(Nonnull.class) ||
constructor.isAnnotationPresent(ParametersAreNonnullByDefault.class) ||
clazz.isAnnotationPresent(ParametersAreNonnullByDefault.class) ||
clazz.getPackage().isAnnotationPresent(ParametersAreNonnullByDefault.class) &&
!parameters[i].isAnnotationPresent(CheckForNull.class))) {
if (Set.class.isAssignableFrom(t)) {
LOGGER.log(Level.FINER, "The parameter to be set is @Nonnull but is not present; " +
"setting equal to empty set.");
args[i] = Collections.emptySet();
} else if (List.class.isAssignableFrom(t)) {
LOGGER.log(Level.FINER, "The parameter to be set is @Nonnull but is not present; " +
"setting equal to empty list.");
args[i] = Collections.emptyList();
} else {
throw new ConfiguratorException(names[i] + " is required to configure " + target);
}
continue;
}
if (value != null) {
if (Collection.class.isAssignableFrom(t)) {
final Type pt = parameters[i].getParameterizedType();
final Configurator lookup = context.lookupOrFail(pt);
final Collection<Object> collection;
if (Set.class.isAssignableFrom(t)) {
collection = new HashSet<>();
} else {
collection = new ArrayList<>();
}
for (CNode o : value.asSequence()) {
collection.add(lookup.configure(o, context));
}
args[i] = collection;
} else {
final Type pt = parameters[i].getParameterizedType();
final Type k = pt != null ? pt : t;
final Configurator configurator = context.lookupOrFail(k);
args[i] = configurator.configure(value, context);
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Setting {0}.{1} = {2}",
new Object[]{target, names[i], t == Secret.class || Attribute.calculateIfSecret(target, names[i]) ? "****" : value});
}
} else if (t.isPrimitive()) {
args[i] = defaultValue(t);
}
}
}
final T object;
try {
object = constructor.newInstance(args);
} catch (IllegalArgumentException | InstantiationException | InvocationTargetException | IllegalAccessException ex) {
List<String> argumentTypes = new ArrayList<>(args.length);
for (Object arg : args) {
argumentTypes.add(arg != null ? arg.getClass().getName() : "null");
}
List<String> parameterTypes = new ArrayList<>(parameters.length);
for (Parameter parameter: parameters){
parameterTypes.add(parameter.getParameterizedType().getTypeName());
}
List<String> expectedParamList = new ArrayList<>(parameters.length);
for(int i = 0; i < parameters.length; i++){
expectedParamList.add(names[i] + " " + parameterTypes.get(i));
}
throw new ConfiguratorException(this,
"Failed to construct instance of " + target +
".\n Constructor: " + constructor.toString() +
".\n Arguments: " + argumentTypes +
".\n Expected Parameters: " + String.join(", ",expectedParamList)
, ex);
}
// constructor was successful, so let's removed configuration elements we have consumed doing so.
for (String name : names) {
config.remove(name);
}
return object;
}
@NonNull
public String getName() {
final Descriptor d = getDescriptor();
return DescribableAttribute.getPreferredSymbol(d, getImplementedAPI(), getTarget());
}
private Descriptor getDescriptor() {
return Jenkins.getInstance().getDescriptor(getTarget());
}
@NonNull
public Class getImplementedAPI() {
final Descriptor descriptor = getDescriptor();
if (descriptor != null) {
// traverse Descriptor's class hierarchy until we found "extends Descriptor<ExtensionPoint>"
Class c = descriptor.getClass();
Type superclass;
do {
superclass = c.getGenericSuperclass();
c = c.getSuperclass();
} while (c != Descriptor.class);
if (superclass instanceof ParameterizedType) {
final ParameterizedType genericSuperclass = (ParameterizedType) superclass;
Type type = genericSuperclass.getActualTypeArguments()[0];
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getRawType();
}
if (type instanceof Class) {
return (Class) type;
}
}
}
return super.getImplementedAPI();
}
@NonNull
@Override
public Set<Attribute<T,?>> describe() {
final Set<Attribute<T,?>> attributes = super.describe();
final Constructor constructor = getDataBoundConstructor(target);
if (constructor != null) {
final Parameter[] parameters = constructor.getParameters();
final String[] names = ClassDescriptor.loadParameterNames(constructor);
for (int i = 0; i < parameters.length; i++) {
final Parameter p = parameters[i];
final Attribute a = createAttribute(names[i], TypePair.of(p));
if (a == null) continue;
attributes.add(a);
}
}
return attributes;
}
@CheckForNull
@Override
public CNode describe(T instance, ConfigurationContext context) throws Exception {
// Here we assume a correctly designed DataBound Object will have required attributes set by DataBoundConstructor
// and all others using DataBoundSetters. So constructor parameters for sure are part of the description, others
// need to be compared with default values.
// Build same object with only constructor parameters
final Constructor constructor = getDataBoundConstructor();
final Parameter[] parameters = constructor.getParameters();
final String[] names = ClassDescriptor.loadParameterNames(constructor);
final Attribute[] attributes = new Attribute[parameters.length];
final Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
final Parameter p = parameters[i];
final Attribute a = createAttribute(names[i], TypePair.of(p));
Object value = a.getValue(instance);
if (value != null) {
Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
if (converted instanceof Collection || !a.isMultiple()) {
args[i] = converted;
} else if (Set.class.isAssignableFrom(p.getType())) {
args[i] = Collections.singleton(converted);
} else {
args[i] = Collections.singletonList(converted);
}
}
if (args[i] == null && p.getType().isPrimitive()) {
args[i] = defaultValue(p.getType());
}
attributes[i] = a;
}
T ref = (T) constructor.newInstance(args);
// compare instance with this "default" object
Mapping mapping = compare(instance, ref, context);
// add constructor parameters
for (int i = 0; i < parameters.length; i++) {
if (args[i] == null) continue;
mapping.put(names[i], attributes[i].describe(instance, context));
}
return mapping;
}
/**
* Gets DataBoundConstructor or fails.
* @return constructor with {@link org.kohsuke.stapler.DataBoundConstructor} annotation
* @throws ConfiguratorException Constructor not found
*/
private Constructor getDataBoundConstructor() throws ConfiguratorException {
final Constructor constructor = getDataBoundConstructor(target);
if (constructor == null) {
throw new ConfiguratorException(target.getName() + " is missing a @DataBoundConstructor");
}
return constructor;
}
public String getDisplayName() {
final Descriptor descriptor = getDescriptor();
return descriptor != null ? descriptor.getDisplayName() : getName();
}
}