-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathConfigurator.java
More file actions
238 lines (217 loc) · 8.5 KB
/
Configurator.java
File metadata and controls
238 lines (217 loc) · 8.5 KB
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
/*
* Copyright (c) 2018 CloudBees, Inc.
*
* 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 io.jenkins.plugins.casc;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ExtensionPoint;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.Symbol;
/**
* Define a {@link Configurator} which handles a configuration element, identified by name.
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
* @author Oleg Nenashev
* @see RootElementConfigurator
*/
public interface Configurator<T> {
@NonNull
static String normalize(@NonNull String name) {
String result = name;
if (result.toUpperCase().equals(name)) {
result = result.toLowerCase();
} else {
result = StringUtils.uncapitalize(name);
}
return result;
}
/**
* Get a configurator name.
*
* This should return the default name for the configurator,
* used for exporting yaml
* see {@link #getNames()} for all possible names which will be considered when configuring.
*
* @return short name for this component when used in a configuration.yaml file
*/
@NonNull
default String getName() {
return getNames().get(0);
}
/**
* Get all possible configurator names
*
* @return a list of all possible short names for this component when used in a configuration.yaml file
*/
@NonNull
default List<String> getNames() {
final Symbol annotation = getTarget().getAnnotation(Symbol.class);
if (annotation != null) {
return Arrays.asList(annotation.value());
}
return Collections.singletonList(normalize(getTarget().getSimpleName()));
}
/**
* @return Human friendly display name for this component, used in generated documentation.
*/
default String getDisplayName() {
return getName();
}
/**
* Target type this configurator can handle.
*/
Class<T> getTarget();
/**
* @return <code>true</code> if this configurator can handle type <code>clazz</code>. Implementation has to be
* consistent with {@link #getTarget()}
*/
default boolean canConfigure(Class clazz) {
return clazz == getTarget();
}
/**
*
*
* @return The API implemented by target type, i.e. implemented {@link ExtensionPoint} for components to implement
* some jenkins APIs, or raw type for others.
*/
@NonNull
default Class getImplementedAPI() {
return getTarget();
}
/**
* @return list of {@link Configurator}s to be considered so one can fully configure this component.
* Typically, configurator for an abstract extension point will return Configurators for available implementations.
*/
@NonNull
default List<Configurator<T>> getConfigurators(ConfigurationContext context) {
return Collections.singletonList(this);
}
/**
* Determine the list of Attribute available for configuration of the managed component.
*
* @return A set of {@link Attribute}s that describes this object
*/
@NonNull
Set<Attribute<T, ?>> describe();
/**
* @return Ordered version of {@link #describe()} for documentation generation.
* Only include non-ignored attribute
*/
@NonNull
default List<Attribute<T, ?>> getAttributes() {
return describe().stream()
.filter(a -> !a.isIgnored())
.sorted(Comparator.comparing(a -> a.name))
.collect(Collectors.toList());
}
/**
* Configures/creates a Jenkins object based on a tree.
*
* @param config
* Map/List/primitive objects (think YAML) that represents the configuration from which
* a Jenkins object is configured.
* @param context
* Fully configured Jenkins object used as the starting point for this configuration.
* @return
* Fully configured Jenkins object that results from this configuration.
* if no new objects got created, but some existing objects may have been modified, return updated target object.
* @throws ConfiguratorException if something went wrong, depends on the concrete implementation
*/
@NonNull
T configure(CNode config, ConfigurationContext context) throws ConfiguratorException;
/**
* Run the same logic as {@link #configure(CNode, ConfigurationContext)} in dry-run mode.
* Used to verify configuration is fine before being actually applied to a
* live jenkins controller.
* @param config
* Map/List/primitive objects (think YAML) that represents the configuration from which
* a Jenkins object is configured.
* @param context
* Fully configured Jenkins object used as the starting point for this configuration.
* @throws ConfiguratorException on configuration error
*/
T check(CNode config, ConfigurationContext context) throws ConfiguratorException;
/**
* Describe a component as a Configuration Nodes {@link CNode} to be exported as yaml.
* Only export attributes which are <b>not</b> set to default value.
*/
@CheckForNull
default CNode describe(T instance, ConfigurationContext context) throws Exception {
Mapping mapping = new Mapping();
for (Attribute attribute : getAttributes()) {
CNode value = attribute.describe(instance, context);
if (value != null) {
mapping.put(attribute.getName(), value);
}
}
return mapping;
}
/**
* Describe Structure of the attributes, as required by the schema.
* @param instance
* Object whose attributes will be described.
* @param context
* Fully configured Jenkins object used as the starting point for this configuration.
* @since 1.35
* @return CNode describing the attributes.
*/
@CheckForNull
default CNode describeStructure(T instance, ConfigurationContext context) {
Mapping mapping = new Mapping();
for (Attribute attribute : getAttributes()) {
if (context.getMode().equals("JSONSchema")) {
attribute.setJsonSchema(true);
}
CNode value = attribute.describeForSchema(instance, context);
if (value != null) {
mapping.put(attribute.getName(), attribute.getType().getSimpleName());
}
}
return mapping;
}
static double extractExtensionOrdinal(Object obj) {
if (obj instanceof Attribute<?, ?>) {
return extractExtensionOrdinal((Attribute<?, ?>) obj);
} else {
return extractExtensionOrdinal(obj.getClass());
}
}
static double extractExtensionOrdinal(Attribute<?, ?> attribute) {
return extractExtensionOrdinal(attribute.type);
}
static double extractExtensionOrdinal(Class clazz) {
Extension extension = (Extension) clazz.getAnnotation(Extension.class);
if (extension == null) {
return 0.0;
}
return extension.ordinal();
}
static Comparator<Object> extensionOrdinalSort() {
return Comparator.comparingDouble(Configurator::extractExtensionOrdinal).reversed();
}
}