forked from apache/nifi-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractDocumentationWriter.java
More file actions
378 lines (299 loc) · 18 KB
/
AbstractDocumentationWriter.java
File metadata and controls
378 lines (299 loc) · 18 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.documentation;
import org.apache.nifi.annotation.behavior.DynamicProperties;
import org.apache.nifi.annotation.behavior.DynamicProperty;
import org.apache.nifi.annotation.behavior.DynamicRelationship;
import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.PrimaryNodeOnly;
import org.apache.nifi.annotation.behavior.ReadsAttribute;
import org.apache.nifi.annotation.behavior.ReadsAttributes;
import org.apache.nifi.annotation.behavior.Restricted;
import org.apache.nifi.annotation.behavior.SideEffectFree;
import org.apache.nifi.annotation.behavior.Stateful;
import org.apache.nifi.annotation.behavior.SupportsBatching;
import org.apache.nifi.annotation.behavior.SupportsSensitiveDynamicProperties;
import org.apache.nifi.annotation.behavior.SystemResourceConsideration;
import org.apache.nifi.annotation.behavior.TriggerSerially;
import org.apache.nifi.annotation.behavior.TriggerWhenAnyDestinationAvailable;
import org.apache.nifi.annotation.behavior.TriggerWhenEmpty;
import org.apache.nifi.annotation.behavior.WritesAttribute;
import org.apache.nifi.annotation.behavior.WritesAttributes;
import org.apache.nifi.annotation.configuration.DefaultSchedule;
import org.apache.nifi.annotation.configuration.DefaultSettings;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.DeprecationNotice;
import org.apache.nifi.annotation.documentation.MultiProcessorUseCase;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.annotation.documentation.UseCase;
import org.apache.nifi.components.ConfigurableComponent;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.controller.ControllerService;
import org.apache.nifi.documentation.init.DocumentationControllerServiceInitializationContext;
import org.apache.nifi.documentation.init.DocumentationFlowAnalysisRuleInitializationContext;
import org.apache.nifi.documentation.init.DocumentationFlowRegistryClientInitializationContext;
import org.apache.nifi.documentation.init.DocumentationParameterProviderInitializationContext;
import org.apache.nifi.documentation.init.DocumentationProcessorInitializationContext;
import org.apache.nifi.documentation.init.DocumentationReportingInitializationContext;
import org.apache.nifi.flowanalysis.FlowAnalysisRule;
import org.apache.nifi.parameter.ParameterProvider;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.registry.flow.FlowRegistryClient;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.reporting.ReportingTask;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Base class for DocumentationWriter that simplifies iterating over all information for a component, creating a separate method
* for each, to ensure that implementations properly override all methods and therefore properly account for all information about
* a component.
*
* Please note that while this class lives within the nifi-api, it is provided primarily as a means for documentation components within
* the NiFi NAR Maven Plugin. Its home is the nifi-api, however, because the API is needed in order to extract the relevant information and
* the NAR Maven Plugin cannot have a direct dependency on nifi-api (doing so would cause a circular dependency). By having this homed within
* the nifi-api, the Maven plugin is able to discover the class dynamically and invoke the one or two methods necessary to create the documentation.
*
* This is a new capability in 1.9.0 in preparation for the Extension Registry and therefore, you should
* <b>NOTE WELL:</b> At this time, while this class is part of nifi-api, it is still evolving and may change in a non-backward-compatible manner or even be
* removed from one incremental release to the next. Use at your own risk!
*/
public abstract class AbstractDocumentationWriter implements ExtensionDocumentationWriter {
@Override
public void initialize(final ConfigurableComponent component) {
try {
if (component instanceof Processor) {
initialize((Processor) component);
} else if (component instanceof ControllerService) {
initialize((ControllerService) component);
} else if (component instanceof ReportingTask) {
initialize((ReportingTask) component);
} else if (component instanceof FlowAnalysisRule) {
initialize((FlowAnalysisRule) component);
} else if (component instanceof ParameterProvider) {
initialize((ParameterProvider) component);
} else if (component instanceof FlowRegistryClient) {
initialize((FlowRegistryClient) component);
}
} catch (final InitializationException ie) {
throw new RuntimeException("Failed to initialize " + component, ie);
}
}
protected void initialize(final Processor processor) {
processor.initialize(new DocumentationProcessorInitializationContext());
}
protected void initialize(final ControllerService service) throws InitializationException {
service.initialize(new DocumentationControllerServiceInitializationContext());
}
protected void initialize(final ReportingTask reportingTask) throws InitializationException {
reportingTask.initialize(new DocumentationReportingInitializationContext());
}
protected void initialize(final FlowAnalysisRule flowAnalysisRule) throws InitializationException {
flowAnalysisRule.initialize(new DocumentationFlowAnalysisRuleInitializationContext());
}
protected void initialize(final ParameterProvider parameterProvider) throws InitializationException {
parameterProvider.initialize(new DocumentationParameterProviderInitializationContext());
}
protected void initialize(final FlowRegistryClient flowRegistryClient) {
flowRegistryClient.initialize(new DocumentationFlowRegistryClientInitializationContext());
}
@Override
public final void write(final ConfigurableComponent component) throws IOException {
write(component, Collections.emptyList(), Collections.emptyMap());
}
@Override
public final void write(final ConfigurableComponent component, final Collection<ServiceAPI> providedServices, Map<String, ServiceAPI> propertyServices) throws IOException {
writeHeader(component);
writeBody(component, propertyServices);
if (providedServices != null && component instanceof ControllerService) {
writeProvidedServices(providedServices);
}
writeFooter(component);
}
@SuppressWarnings("removal")
protected void writeBody(final ConfigurableComponent component, Map<String, ServiceAPI> propertyServices) throws IOException {
writeExtensionName(component.getClass().getName());
writeExtensionType(getExtensionType(component));
writeDeprecationNotice(component.getClass().getAnnotation(DeprecationNotice.class));
writeDescription(getDescription(component));
writeTags(getTags(component));
writeProperties(component.getPropertyDescriptors(), propertyServices);
writeDynamicProperties(getDynamicProperties(component));
writeSupportsSensitiveDynamicProperties(component.getClass().getAnnotation(SupportsSensitiveDynamicProperties.class));
if (component instanceof Processor processor) {
writeRelationships(processor.getRelationships());
writeDynamicRelationship(getDynamicRelationship(processor));
writeReadsAttributes(getReadsAttributes(processor));
writeWritesAttributes(getWritesAttributes(processor));
writeTriggerSerially(processor.getClass().getAnnotation(TriggerSerially.class));
writeTriggerWhenEmpty(processor.getClass().getAnnotation(TriggerWhenEmpty.class));
writeTriggerWhenAnyDestinationAvailable(processor.getClass().getAnnotation(TriggerWhenAnyDestinationAvailable.class));
writeSupportsBatching(processor.getClass().getAnnotation(SupportsBatching.class));
writePrimaryNodeOnly(processor.getClass().getAnnotation(PrimaryNodeOnly.class));
writeSideEffectFree(processor.getClass().getAnnotation(SideEffectFree.class));
writeDefaultSettings(processor.getClass().getAnnotation(DefaultSettings.class));
}
writeStatefulInfo(component.getClass().getAnnotation(Stateful.class));
writeRestrictedInfo(component.getClass().getAnnotation(Restricted.class));
writeInputRequirementInfo(getInputRequirement(component));
writeSystemResourceConsiderationInfo(getSystemResourceConsiderations(component));
writeUseCases(getUseCases(component));
writeMultiProcessorUseCases(getMultiProcessorUseCases(component));
writeSeeAlso(component.getClass().getAnnotation(SeeAlso.class));
writeDefaultSchedule(component.getClass().getAnnotation(DefaultSchedule.class));
}
protected String getDescription(final ConfigurableComponent component) {
final CapabilityDescription capabilityDescription = component.getClass().getAnnotation(CapabilityDescription.class);
if (capabilityDescription == null) {
return null;
}
return capabilityDescription.value();
}
protected List<String> getTags(final ConfigurableComponent component) {
final Tags tags = component.getClass().getAnnotation(Tags.class);
if (tags == null) {
return Collections.emptyList();
}
final String[] tagValues = tags.value();
return tagValues == null ? Collections.emptyList() : Arrays.asList(tagValues);
}
protected List<DynamicProperty> getDynamicProperties(ConfigurableComponent configurableComponent) {
final List<DynamicProperty> dynamicProperties = new ArrayList<>();
final DynamicProperties dynProps = configurableComponent.getClass().getAnnotation(DynamicProperties.class);
if (dynProps != null) {
Collections.addAll(dynamicProperties, dynProps.value());
}
final DynamicProperty dynProp = configurableComponent.getClass().getAnnotation(DynamicProperty.class);
if (dynProp != null) {
dynamicProperties.add(dynProp);
}
return dynamicProperties;
}
private DynamicRelationship getDynamicRelationship(Processor processor) {
return processor.getClass().getAnnotation(DynamicRelationship.class);
}
private List<ReadsAttribute> getReadsAttributes(final Processor processor) {
final List<ReadsAttribute> attributes = new ArrayList<>();
final ReadsAttributes readsAttributes = processor.getClass().getAnnotation(ReadsAttributes.class);
if (readsAttributes != null) {
Collections.addAll(attributes, readsAttributes.value());
}
final ReadsAttribute readsAttribute = processor.getClass().getAnnotation(ReadsAttribute.class);
if (readsAttribute != null) {
attributes.add(readsAttribute);
}
return attributes;
}
private List<WritesAttribute> getWritesAttributes(Processor processor) {
List<WritesAttribute> attributes = new ArrayList<>();
WritesAttributes writesAttributes = processor.getClass().getAnnotation(WritesAttributes.class);
if (writesAttributes != null) {
Collections.addAll(attributes, writesAttributes.value());
}
WritesAttribute writeAttribute = processor.getClass().getAnnotation(WritesAttribute.class);
if (writeAttribute != null) {
attributes.add(writeAttribute);
}
return attributes;
}
private InputRequirement.Requirement getInputRequirement(final ConfigurableComponent component) {
final InputRequirement annotation = component.getClass().getAnnotation(InputRequirement.class);
return annotation == null ? null : annotation.value();
}
private List<SystemResourceConsideration> getSystemResourceConsiderations(final ConfigurableComponent component) {
SystemResourceConsideration[] systemResourceConsiderations = component.getClass().getAnnotationsByType(SystemResourceConsideration.class);
if (systemResourceConsiderations.length == 0) {
return Collections.emptyList();
}
return Arrays.asList(systemResourceConsiderations);
}
private List<UseCase> getUseCases(final ConfigurableComponent component) {
UseCase[] useCases = component.getClass().getAnnotationsByType(UseCase.class);
if (useCases.length == 0) {
return Collections.emptyList();
}
return Arrays.asList(useCases);
}
private List<MultiProcessorUseCase> getMultiProcessorUseCases(final ConfigurableComponent component) {
MultiProcessorUseCase[] useCases = component.getClass().getAnnotationsByType(MultiProcessorUseCase.class);
if (useCases.length == 0) {
return Collections.emptyList();
}
return Arrays.asList(useCases);
}
protected ExtensionType getExtensionType(final ConfigurableComponent component) {
if (component instanceof Processor) {
return ExtensionType.PROCESSOR;
}
if (component instanceof ControllerService) {
return ExtensionType.CONTROLLER_SERVICE;
}
if (component instanceof ReportingTask) {
return ExtensionType.REPORTING_TASK;
}
if (component instanceof FlowAnalysisRule) {
return ExtensionType.FLOW_ANALYSIS_RULE;
}
if (component instanceof ParameterProvider) {
return ExtensionType.PARAMETER_PROVIDER;
}
if (component instanceof FlowRegistryClient) {
return ExtensionType.FLOW_REGISTRY_CLIENT;
}
throw new AssertionError("Encountered unknown Configurable Component Type for " + component);
}
protected abstract void writeHeader(ConfigurableComponent component) throws IOException;
protected abstract void writeExtensionName(String extensionName) throws IOException;
protected abstract void writeExtensionType(ExtensionType extensionType) throws IOException;
protected abstract void writeDeprecationNotice(final DeprecationNotice deprecationNotice) throws IOException;
protected abstract void writeDescription(String description) throws IOException;
protected abstract void writeTags(List<String> tags) throws IOException;
protected abstract void writeProperties(List<PropertyDescriptor> properties, Map<String, ServiceAPI> propertyServices) throws IOException;
protected abstract void writeDynamicProperties(List<DynamicProperty> dynamicProperties) throws IOException;
protected abstract void writeStatefulInfo(Stateful stateful) throws IOException;
@SuppressWarnings("removal")
protected abstract void writeRestrictedInfo(Restricted restricted) throws IOException;
protected abstract void writeInputRequirementInfo(InputRequirement.Requirement requirement) throws IOException;
protected abstract void writeSystemResourceConsiderationInfo(List<SystemResourceConsideration> considerations) throws IOException;
protected abstract void writeSeeAlso(SeeAlso seeAlso) throws IOException;
protected abstract void writeUseCases(List<UseCase> useCases) throws IOException;
protected abstract void writeMultiProcessorUseCases(List<MultiProcessorUseCase> useCases) throws IOException;
protected abstract void writeDefaultSchedule(DefaultSchedule defaultSchedule) throws IOException;
// Processor-specific methods
protected abstract void writeRelationships(Set<Relationship> relationships) throws IOException;
protected abstract void writeDynamicRelationship(DynamicRelationship dynamicRelationship) throws IOException;
protected abstract void writeReadsAttributes(List<ReadsAttribute> attributes) throws IOException;
protected abstract void writeWritesAttributes(List<WritesAttribute> attributes) throws IOException;
protected abstract void writeTriggerSerially(TriggerSerially triggerSerially) throws IOException;
protected abstract void writeTriggerWhenEmpty(TriggerWhenEmpty triggerWhenEmpty) throws IOException;
protected abstract void writeTriggerWhenAnyDestinationAvailable(TriggerWhenAnyDestinationAvailable triggerWhenAnyDestinationAvailable) throws IOException;
protected abstract void writeSupportsBatching(SupportsBatching supportsBatching) throws IOException;
protected abstract void writeSupportsSensitiveDynamicProperties(SupportsSensitiveDynamicProperties supportsSensitiveDynamicProperties) throws IOException;
protected abstract void writePrimaryNodeOnly(PrimaryNodeOnly primaryNodeOnly) throws IOException;
protected abstract void writeSideEffectFree(SideEffectFree sideEffectFree) throws IOException;
protected abstract void writeDefaultSettings(DefaultSettings defaultSettings) throws IOException;
// ControllerService-specific methods
protected abstract void writeProvidedServices(Collection<ServiceAPI> providedServices) throws IOException;
protected abstract void writeFooter(ConfigurableComponent component) throws IOException;
}