-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[Stable plugin API] Load plugin named components #89969
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
Changes from all commits
2a2179b
f59692a
c508977
515071b
58726df
5927de1
e722af2
fadaf34
6534f34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 89969 | ||
| summary: "[Stable plugin API] Load plugin named components" | ||
| area: Infra/Plugins | ||
| type: enhancement | ||
| issues: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.scanners; | ||
|
|
||
| import org.elasticsearch.logging.LogManager; | ||
| import org.elasticsearch.logging.Logger; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.xcontent.XContentType.JSON; | ||
|
|
||
| public class ExtensibleFileReader { | ||
| private static final Logger logger = LogManager.getLogger(ExtensibleFileReader.class); | ||
|
|
||
| private String extensibleFile; | ||
|
|
||
| public ExtensibleFileReader(String extensibleFile) { | ||
| this.extensibleFile = extensibleFile; | ||
| } | ||
|
|
||
| public Map<String, String> readFromFile() { | ||
| Map<String, String> res = new HashMap<>(); | ||
| // todo should it be BufferedInputStream ? | ||
| try (InputStream in = getClass().getResourceAsStream(extensibleFile)) { | ||
| if (in != null) { | ||
| try (XContentParser parser = JSON.xContent().createParser(XContentParserConfiguration.EMPTY, in)) { | ||
| // TODO should we validate the classes actually exist? | ||
| return parser.mapStrings(); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| logger.error("failed reading extensible file", e); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.scanners; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.core.Strings.format; | ||
|
|
||
| /** | ||
| * A registry of Extensible interfaces/classes read from extensibles.json file. | ||
| * The file is generated during Elasticsearch built time (or commited) | ||
| * basing on the classes declared in stable plugins api (i.e. plugin-analysis-api) | ||
| * | ||
| * This file is present in server jar. | ||
| * a class/interface is directly extensible when is marked with @Extensible annotation | ||
| * a class/interface can be indirectly extensible when it extends/implements a directly extensible class | ||
| * | ||
| * Information about extensible interfaces/classes are stored in a map where: | ||
| * key and value are the same cannonical name of the class that is directly marked with @Extensible | ||
| * or | ||
| * key: a cannonical name of the class that is indirectly extensible but extends another extensible class (directly/indirectly) | ||
| * value: cannonical name of the class that is directly extensible | ||
| * | ||
| * The reason for indirectly extensible classes is to allow stable plugin apis to create hierarchies | ||
| * | ||
| * Example: | ||
| * <pre> | ||
| * @Extensible | ||
| * interface E{ | ||
| * public void foo(); | ||
| * } | ||
| * interface Eprim extends E{ | ||
| * } | ||
| * | ||
| * class Aclass implements E{ | ||
| * | ||
| * } | ||
| * | ||
| * @Extensible | ||
| * class E2 { | ||
| * public void bar(){} | ||
| * } | ||
| * </pre> | ||
| * the content of extensibles.json should be | ||
| * { | ||
| * "E" : "E", | ||
| * "Eprim" : "E", | ||
| * "A" : "E", | ||
| * "E2" : "E2" | ||
| * } | ||
| * | ||
| * @see org.elasticsearch.plugin.api.Extensible | ||
| */ | ||
| public class ExtensiblesRegistry { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(ExtensiblesRegistry.class); | ||
|
|
||
| private static final String EXTENSIBLES_FILE = "/org/elasticsearch/plugins/scanners/extensibles.json"; | ||
| public static final ExtensiblesRegistry INSTANCE = new ExtensiblesRegistry(EXTENSIBLES_FILE); | ||
|
|
||
| // classname (potentially extending/implementing extensible) to interface/class annotated with extensible | ||
| private final Map<String, String> loadedExtensible; | ||
|
|
||
| ExtensiblesRegistry(String extensiblesFile) { | ||
| ExtensibleFileReader extensibleFileReader = new ExtensibleFileReader(extensiblesFile); | ||
|
|
||
| this.loadedExtensible = extensibleFileReader.readFromFile(); | ||
| if (loadedExtensible.size() > 0) { | ||
| logger.debug(() -> format("Loaded extensible from cache file %s", loadedExtensible)); | ||
| } | ||
| } | ||
|
|
||
| public boolean hasExtensible(String extensibleName) { | ||
| return loadedExtensible.containsKey(extensibleName); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.scanners; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public record NameToPluginInfo(Map<String, PluginInfo> nameToPluginInfoMap) { | ||
|
|
||
| public NameToPluginInfo() { | ||
| this(new HashMap<>()); | ||
| } | ||
|
|
||
| public NameToPluginInfo put(String name, PluginInfo pluginInfo) { | ||
| nameToPluginInfoMap.put(name, pluginInfo); | ||
| return this; | ||
| } | ||
|
|
||
| public void putAll(Map<String, PluginInfo> namedPluginInfoMap) { | ||
| this.nameToPluginInfoMap.putAll(namedPluginInfoMap); | ||
| } | ||
|
|
||
| public NameToPluginInfo put(NameToPluginInfo nameToPluginInfo) { | ||
| putAll(nameToPluginInfo.nameToPluginInfoMap); | ||
| return this; | ||
| } | ||
|
|
||
| public PluginInfo getForPluginName(String pluginName) { | ||
| return nameToPluginInfoMap.get(pluginName); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * 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.scanners; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.core.Strings; | ||
| import org.elasticsearch.plugins.PluginBundle; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static java.util.Collections.emptyMap; | ||
| import static org.elasticsearch.xcontent.XContentType.JSON; | ||
|
|
||
| /** | ||
| * Reads named components declared by a plugin in a cache file. | ||
| * Cache file is expected to be present in plugin's lib directory | ||
| * <p> | ||
| * The content of a cache file is a JSON representation of a map where: | ||
| * keys -> name of the extensible interface (a class/interface marked with @Extensible) | ||
| * values -> a map of name to implementation class name | ||
| */ | ||
| public class NamedComponentReader { | ||
|
|
||
| private Logger logger = LogManager.getLogger(NamedComponentReader.class); | ||
| private static final String NAMED_COMPONENTS_FILE_NAME = "named_components.json"; | ||
| /** | ||
| * a registry of known classes marked or indirectly marked (extending marked class) with @Extensible | ||
| */ | ||
| private final ExtensiblesRegistry extensiblesRegistry; | ||
|
Contributor
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. is this field used?
Contributor
Author
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. at this scope of the PR it is not. We do not perform the scanning and we also do not register known extensibles (it will be its primary use). In NamedComponentReader it could be used for validating the read file. will add some simple validation |
||
|
|
||
| public NamedComponentReader() { | ||
| this(ExtensiblesRegistry.INSTANCE); | ||
| } | ||
|
Contributor
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. let's chain this constructor, so we only have one substantive constructor body, e.g. |
||
|
|
||
| NamedComponentReader(ExtensiblesRegistry extensiblesRegistry) { | ||
| this.extensiblesRegistry = extensiblesRegistry; | ||
| } | ||
|
|
||
| public Map<String, NameToPluginInfo> findNamedComponents(PluginBundle bundle, ClassLoader pluginClassLoader) { | ||
| Path pluginDir = bundle.getDir(); | ||
| return findNamedComponents(pluginDir, pluginClassLoader); | ||
| } | ||
|
|
||
| // scope for testing | ||
| Map<String, NameToPluginInfo> findNamedComponents(Path pluginDir, ClassLoader pluginClassLoader) { | ||
| try { | ||
| Path namedComponent = findNamedComponentCacheFile(pluginDir); | ||
| if (namedComponent != null) { | ||
| Map<String, NameToPluginInfo> namedComponents = readFromFile(namedComponent, pluginClassLoader); | ||
| logger.debug(() -> Strings.format("Plugin in dir %s declared named components %s.", pluginDir, namedComponents)); | ||
|
|
||
| return namedComponents; | ||
| } | ||
| logger.debug(() -> Strings.format("No named component defined in plugin dir %s", pluginDir)); | ||
| } catch (IOException e) { | ||
| logger.error("unable to read named components", e); | ||
| } | ||
| return emptyMap(); | ||
| } | ||
|
|
||
| private Path findNamedComponentCacheFile(Path pluginDir) throws IOException { | ||
| try (Stream<Path> list = Files.list(pluginDir)) { | ||
| return list.filter(p -> p.getFileName().toString().equals(NAMED_COMPONENTS_FILE_NAME)).findFirst().orElse(null); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Map<String, NameToPluginInfo> readFromFile(Path namedComponent, ClassLoader pluginClassLoader) throws IOException { | ||
| Map<String, NameToPluginInfo> res = new HashMap<>(); | ||
|
|
||
| try ( | ||
| var json = new BufferedInputStream(Files.newInputStream(namedComponent)); | ||
| var parser = JSON.xContent().createParser(XContentParserConfiguration.EMPTY, json) | ||
| ) { | ||
| Map<String, Object> map = parser.map(); | ||
| for (Map.Entry<String, Object> fileAsMap : map.entrySet()) { | ||
| String extensibleInterface = fileAsMap.getKey(); | ||
| validateExtensible(extensibleInterface); | ||
| Map<String, Object> components = (Map<String, Object>) fileAsMap.getValue(); | ||
| for (Map.Entry<String, Object> nameToComponent : components.entrySet()) { | ||
| String name = nameToComponent.getKey(); | ||
| String value = (String) nameToComponent.getValue(); | ||
|
|
||
| res.computeIfAbsent(extensibleInterface, k -> new NameToPluginInfo()) | ||
| .put(name, new PluginInfo(name, value, pluginClassLoader)); | ||
| } | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| private void validateExtensible(String extensibleInterface) { | ||
| if (extensiblesRegistry.hasExtensible(extensibleInterface) == false) { | ||
| throw new IllegalStateException("Unknown extensible name " + extensibleInterface); | ||
| } | ||
| } | ||
| } | ||
| 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.scanners; | ||
|
|
||
| record PluginInfo(String name, String className, ClassLoader loader) { | ||
|
|
||
| } |
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.
I was wondering if getClass().getClassLoader().getResourceAsStream could be used here (no preference, but wanted to see the difference..)
what I found is that:
getClass().getResourceAsStream("/org/elasticsearch/plugins/scanners/extensibles.json)works without any modifications to module-info.javabut
getClass().getClassLoader().getResourceAsStream("org/elasticsearch/plugins/scanners/extensibles.json")only works when
opens org.elasticsearch.plugins.scanners;is added to module-info.javathis is mentioned in ClassLoader javadoc
https://docs.oracle.com/javase/9/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-
Class's javadoc does not seem to be that restrictive
@ChrisHegarty any ideas why the JDK did this that way? I suppose that Class#getResource is more preferred to Classloader#getResource ?
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.
@pgomulka The difference is clear from the javadoc/spec - j.l.Class::getResourceAsStream is caller-sensitive and determines the resource access based on the module of the immediate caller. Where as j.l.ClassLoader::getResourceAsStream is not caller-sensitive, therefore cannot determine resource access so behaves with no regard to the immediate caller. Ok, but why?
Generally speaking, caller-sensitive methods should not be extensible, as they pose problems for the implementing class, as well as opening the possibility to non-conformance. The method in j.l.Class is not extensible, so can be caller-sensitive. The method in ClassLoader is extensible, so is not caller-sensitive.