forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expanded plugin constructor capabilities (opensearch-project#481)
Support more constructors for plugins by adding the DataPrepperPluginConstructor annotation. This is the preferred constructor. If no other constructor is available for a plugin, use the no-op constructor. Updated the HTTPSource plugin to use this capability to receive both a configuration model and PluginMetrics via the constructor. For a single parameter, un-annotated constructor in plugins, the only supported parameter is once again PluginSetting. Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
22 changed files
with
553 additions
and
404 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
.../src/main/java/com/amazon/dataprepper/model/annotations/DataPrepperPluginConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.amazon.dataprepper.model.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Add to a plugin class to indicate which constructor should be used by Data Prepper. | ||
* <p> | ||
* The current behavior for choosing a constructor is: | ||
* <ol> | ||
* <li>Choose the constructor annotated with {@link DataPrepperPluginConstructor}</li> | ||
* <li>Choose a constructor which takes in a single parameter matching | ||
* the {@link DataPrepperPlugin#pluginConfigurationType()} for the plugin</li> | ||
* <li>Use the default (ie. empty) constructor</li> | ||
* </ol> | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.CONSTRUCTOR}) | ||
public @interface DataPrepperPluginConstructor { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
data-prepper-core/src/main/java/com/amazon/dataprepper/plugin/PluginArgumentsContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.amazon.dataprepper.plugin; | ||
|
||
import com.amazon.dataprepper.metrics.PluginMetrics; | ||
import com.amazon.dataprepper.model.configuration.PluginSetting; | ||
import com.amazon.dataprepper.model.plugin.InvalidPluginDefinitionException; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* An internal class which represents all the data which can be provided | ||
* when constructing a new plugin. | ||
*/ | ||
class PluginArgumentsContext { | ||
private final Map<Class<?>, Supplier<Object>> typedArgumentsSuppliers; | ||
|
||
private PluginArgumentsContext(final Builder builder) { | ||
Objects.requireNonNull(builder.pluginSetting, | ||
"PluginArgumentsContext received a null Builder object. This is likely an error in the plugin framework."); | ||
|
||
typedArgumentsSuppliers = new HashMap<>(); | ||
|
||
typedArgumentsSuppliers.put(builder.pluginSetting.getClass(), () -> builder.pluginSetting); | ||
|
||
if(builder.pluginConfiguration != null) { | ||
typedArgumentsSuppliers.put(builder.pluginConfiguration.getClass(), () -> builder.pluginConfiguration); | ||
} | ||
|
||
typedArgumentsSuppliers.put(PluginMetrics.class, () -> PluginMetrics.fromPluginSetting(builder.pluginSetting)); | ||
} | ||
|
||
Object[] createArguments(final Class<?>[] parameterTypes) { | ||
return Arrays.stream(parameterTypes) | ||
.map(this::getRequiredArgumentSupplier) | ||
.map(Supplier::get) | ||
.toArray(); | ||
} | ||
|
||
private Supplier<Object> getRequiredArgumentSupplier(final Class<?> parameterType) { | ||
if(typedArgumentsSuppliers.containsKey(parameterType)) { | ||
return typedArgumentsSuppliers.get(parameterType); | ||
} | ||
|
||
throw new InvalidPluginDefinitionException("Unable to create an argument for required plugin parameter type: " + parameterType); | ||
} | ||
|
||
static class Builder { | ||
private Object pluginConfiguration; | ||
private PluginSetting pluginSetting; | ||
|
||
Builder withPluginConfiguration(final Object pluginConfiguration) { | ||
this.pluginConfiguration = pluginConfiguration; | ||
return this; | ||
} | ||
|
||
Builder withPluginSetting(final PluginSetting pluginSetting) { | ||
this.pluginSetting = pluginSetting; | ||
return this; | ||
} | ||
|
||
PluginArgumentsContext build() { | ||
return new PluginArgumentsContext(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.