Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
import java.util.MissingResourceException;

/**
* Utilities for for dealing with {@link Locale} objects
* Utilities for dealing with {@link Locale} objects
*/
public class LocaleUtils {

/**
* Parse the given locale as {@code language}, {@code language-country} or
* {@code language-country-variant}.
* Either underscores or hyphens may be used as separators, but consistently, ie.
* you may not use an hyphen to separate the language from the country and an
* Either underscores or hyphens may be used as separators, but consistently, i.e.
* you may not use a hyphen to separate the language from the country and an
* underscore to separate the country from the variant.
* @throws IllegalArgumentException if there are too many parts in the locale string
* @throws IllegalArgumentException if the language or country is not recognized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,18 +543,38 @@ public static TemplateScript.Factory compileTemplate(
Script script = new Script(ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, propertyValue, Map.of());
return scriptService.compile(script, TemplateScript.CONTEXT);
} else {
return (params) -> new TemplateScript(params) {
@Override
public String execute() {
return propertyValue;
}
};
return new ConstantTemplateScriptFactory(propertyValue);
}
} catch (Exception e) {
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, propertyName, e);
}
}

/**
* A 'template script' that ignores the model to which it is applied and just always returns a constant String.
* <p>
* Having a separate named class for this allows for some hot code paths to pre-apply the 'template script' statically,
* rather than bothering to invoke it per-document. Note that this is probably only useful if something expensive
* is being done with the result of calling the script, and the code can then avoid doing that thing per-document.
*/
public static class ConstantTemplateScriptFactory implements TemplateScript.Factory {
final TemplateScript script;

private ConstantTemplateScriptFactory(String value) {
this.script = new TemplateScript(Map.of()) {
@Override
public String execute() {
return value;
}
};
}

@Override
public TemplateScript newInstance(Map<String, Object> params) {
return script;
}
}

private static void addMetadataToException(
ElasticsearchException exception,
String processorType,
Expand Down
Loading