-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Add generic interface for loading service providers from plugins #88082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
04d0e29
Add OperatorHandler and SPI
6dac760
Merge branch 'master' into operator/handler_interface
elasticmachine bbf0def
Merge branch 'master' into operator/handler_interface
7f82018
Merge master
4a67ede
Better docs
4517be8
Fix wrong security policy file
6d6b0bf
Fix line dup
ec62bea
Fake the classloaders instead of using real
20cf2e7
Bring back real class loaders in tests
4980cc6
Merge branch 'master' into operator/action_spi
4d68944
Merge branch 'master' into operator/action_spi
elasticmachine 605787c
Merge branch 'master' into operator/action_spi
3fe5007
Apply review feedback
20d44a5
Merge branch 'operator/action_spi' of github.com:grcevski/elasticsear…
e38c2fc
Split the provider class in the test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
@@ -279,7 +280,6 @@ private Map<String, LoadedPlugin> loadBundles(Set<PluginBundle> bundles) { | |
|
||
// package-private for test visibility | ||
static void loadExtensions(Collection<LoadedPlugin> plugins) { | ||
|
||
Map<String, List<Plugin>> extendingPluginsByName = plugins.stream() | ||
.flatMap(t -> t.descriptor().getExtendedPlugins().stream().map(extendedPlugin -> Tuple.tuple(extendedPlugin, t.instance()))) | ||
.collect(Collectors.groupingBy(Tuple::v1, Collectors.mapping(Tuple::v2, Collectors.toList()))); | ||
|
@@ -293,6 +293,28 @@ static void loadExtensions(Collection<LoadedPlugin> plugins) { | |
} | ||
} | ||
|
||
/** | ||
* SPI convenience method that uses the {@link ServiceLoader} JDK class to load various SPI providers | ||
* from plugins/modules. | ||
* <p> | ||
* For example: | ||
* | ||
* <pre> | ||
* var pluginHandlers = pluginsService.loadServiceProviders(OperatorHandlerProvider.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think only the code should be in the |
||
* </pre> | ||
* @param service A templated service class to look for providers in plugins | ||
* @return an immutable {@link List} of discovered providers in the plugins/modules | ||
*/ | ||
public <T> List<? extends T> loadServiceProviders(Class<T> service) { | ||
List<T> result = new ArrayList<>(); | ||
|
||
for (LoadedPlugin pluginTuple : plugins()) { | ||
ServiceLoader.load(service, pluginTuple.loader()).iterator().forEachRemaining(c -> result.add(c)); | ||
} | ||
|
||
return Collections.unmodifiableList(result); | ||
} | ||
|
||
private static void loadExtensionsForPlugin(ExtensiblePlugin extensiblePlugin, List<Plugin> extendingPlugins) { | ||
ExtensiblePlugin.ExtensionLoader extensionLoader = new ExtensiblePlugin.ExtensionLoader() { | ||
@Override | ||
|
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
server/src/test/java/org/elasticsearch/plugins/spi/TestService.java
This file contains hidden or 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,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.plugins.spi; | ||
|
||
public interface TestService { | ||
String name(); | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in a
<pre>
block?