Search in sources :

Example 6 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class MustacheScriptEngine method compile.

/**
 * Compile a template string to (in this case) a Mustache object than can
 * later be re-used for execution to fill in missing parameter values.
 *
 * @param templateSource a string representing the template to compile.
 * @return a compiled template object for later execution.
 */
@Override
public <T> T compile(String templateName, String templateSource, ScriptContext<T> context, Map<String, String> options) {
    if (context.instanceClazz.equals(TemplateScript.class) == false) {
        throw new IllegalArgumentException("mustache engine does not know how to handle context [" + context.name + "]");
    }
    final MustacheFactory factory = createMustacheFactory(options);
    Reader reader = new StringReader(templateSource);
    try {
        Mustache template = factory.compile(reader, "query-template");
        TemplateScript.Factory compiled = params -> new MustacheExecutableScript(template, params);
        return context.factoryClazz.cast(compiled);
    } catch (MustacheException ex) {
        throw new ScriptException(ex.getMessage(), ex, Collections.emptyList(), templateSource, NAME);
    }
}
Also used : Script(org.opensearch.script.Script) ScriptEngine(org.opensearch.script.ScriptEngine) StringWriter(java.io.StringWriter) Mustache(com.github.mustachejava.Mustache) Set(java.util.Set) MustacheException(com.github.mustachejava.MustacheException) Reader(java.io.Reader) PrivilegedAction(java.security.PrivilegedAction) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) GeneralScriptException(org.opensearch.script.GeneralScriptException) ScriptException(org.opensearch.script.ScriptException) Logger(org.apache.logging.log4j.Logger) StringReader(java.io.StringReader) Supplier(org.apache.logging.log4j.util.Supplier) Map(java.util.Map) MustacheFactory(com.github.mustachejava.MustacheFactory) SpecialPermission(org.opensearch.SpecialPermission) TemplateScript(org.opensearch.script.TemplateScript) AccessController(java.security.AccessController) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ScriptContext(org.opensearch.script.ScriptContext) GeneralScriptException(org.opensearch.script.GeneralScriptException) ScriptException(org.opensearch.script.ScriptException) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Mustache(com.github.mustachejava.Mustache) MustacheException(com.github.mustachejava.MustacheException) TemplateScript(org.opensearch.script.TemplateScript) MustacheFactory(com.github.mustachejava.MustacheFactory)

Aggregations

ScriptContext (org.opensearch.script.ScriptContext)6 Script (org.opensearch.script.Script)4 ScriptService (org.opensearch.script.ScriptService)3 List (java.util.List)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 IndexAnalyzers (org.opensearch.index.analysis.IndexAnalyzers)2 NamedAnalyzer (org.opensearch.index.analysis.NamedAnalyzer)2 AnalysisModule (org.opensearch.indices.analysis.AnalysisModule)2 Mustache (com.github.mustachejava.Mustache)1 MustacheException (com.github.mustachejava.MustacheException)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1