Skip to content
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

Allow YamlTemplateRegistry to fallback to TemplateResources #112615

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

package org.elasticsearch.xpack.core.template;

import org.elasticsearch.xpack.core.template.resources.TemplateResources;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class ResourceUtils {

static byte[] loadVersionedResourceUTF8(Class<?> clazz, String name, int version, String versionProperty) {
return loadVersionedResourceUTF8(clazz, name, version, versionProperty, Map.of());
}
Expand All @@ -25,16 +28,33 @@ static byte[] loadVersionedResourceUTF8(
Map<String, String> variables
) {
try {
String content = loadResource(clazz, name);
String content = loadResourceWithFallback(clazz, name);
content = TemplateUtils.replaceVariables(content, String.valueOf(version), versionProperty, variables);
return content.getBytes(StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static String loadResource(Class<?> clazz, String name) throws IOException {
InputStream is = clazz.getResourceAsStream(name);
/**
* loadResourceWithFallback loads template resources from TemplateResource class. It expects
* the loader to be a named Java module and the template resources to contain required templates
* with a folder named with the module name. If the specified resource name is not found in
* template resource then the plugin's resources is used as a fallback.
*
* @param clazz the runtime class of the source plugin
* @param name the relative path of the resource with leading `/`
* @return the loaded resource as string
* @throws IOException
*/
static String loadResourceWithFallback(Class<?> clazz, String name) throws IOException {
InputStream is = null;
if (clazz.getModule().isNamed()) {
is = TemplateResources.class.getResourceAsStream("/" + clazz.getModule().getName() + name);
}
if (is == null) {
is = clazz.getResourceAsStream(name);
}
if (is == null) {
throw new IOException("Resource [" + name + "] not found in classpath.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.core.template.ResourceUtils.loadResource;
import static org.elasticsearch.xpack.core.template.ResourceUtils.loadResourceWithFallback;
import static org.elasticsearch.xpack.core.template.ResourceUtils.loadVersionedResourceUTF8;

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ public YamlTemplateRegistry(
try {
final Map<String, Object> resources = XContentHelper.convertToMap(
YamlXContent.yamlXContent,
loadResource(this.getClass(), "/resources.yaml"),
loadResourceWithFallback(this.getClass(), "/resources.yaml"),
false
);
version = (((Number) resources.get("version")).intValue());
Expand Down