Search in sources :

Example 96 with ScriptEngine

use of javax.script.ScriptEngine in project camel by apache.

the class Jsr223Test method testLanguageNames.

@Test
public void testLanguageNames() throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    for (String scriptName : scriptNames) {
        ScriptEngine engine = manager.getEngineByName(scriptName);
        assertNotNull("We should get the script engine for " + scriptName, engine);
    }
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 97 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class ScriptTemplateView method renderInternal.

@Override
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange) {
    return Mono.defer(() -> {
        ServerHttpResponse response = exchange.getResponse();
        try {
            ScriptEngine engine = getEngine();
            Invocable invocable = (Invocable) engine;
            String url = getUrl();
            String template = getTemplate(url);
            Function<String, String> templateLoader = path -> {
                try {
                    return getTemplate(path);
                } catch (IOException ex) {
                    throw new IllegalStateException(ex);
                }
            };
            RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
            Object html;
            if (this.renderObject != null) {
                Object thiz = engine.eval(this.renderObject);
                html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
            } else {
                html = invocable.invokeFunction(this.renderFunction, template, model, context);
            }
            byte[] bytes = String.valueOf(html).getBytes(StandardCharsets.UTF_8);
            DataBuffer buffer = response.bufferFactory().allocateBuffer(bytes.length).write(bytes);
            return response.writeWith(Mono.just(buffer));
        } catch (ScriptException ex) {
            throw new IllegalStateException("Failed to render script template", new StandardScriptEvalException(ex));
        } catch (Exception ex) {
            throw new IllegalStateException("Failed to render script template", ex);
        }
    });
}
Also used : ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) ApplicationContextException(org.springframework.context.ApplicationContextException) Function(java.util.function.Function) StandardScriptUtils(org.springframework.scripting.support.StandardScriptUtils) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Locale(java.util.Locale) Map(java.util.Map) ScriptException(javax.script.ScriptException) Resource(org.springframework.core.io.Resource) AbstractUrlBasedView(org.springframework.web.reactive.result.view.AbstractUrlBasedView) ResourceLoader(org.springframework.core.io.ResourceLoader) MediaType(org.springframework.http.MediaType) ObjectUtils(org.springframework.util.ObjectUtils) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) DataBuffer(org.springframework.core.io.buffer.DataBuffer) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) StandardCharsets(java.nio.charset.StandardCharsets) Invocable(javax.script.Invocable) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ScriptEngine(javax.script.ScriptEngine) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) FileCopyUtils(org.springframework.util.FileCopyUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ScriptEngine(javax.script.ScriptEngine) ApplicationContextException(org.springframework.context.ApplicationContextException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 98 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class ScriptTemplateView method createEngineFromName.

protected ScriptEngine createEngineFromName() {
    if (this.scriptEngineManager == null) {
        this.scriptEngineManager = new ScriptEngineManager(getApplicationContext().getClassLoader());
    }
    ScriptEngine engine = StandardScriptUtils.retrieveEngineByName(this.scriptEngineManager, this.engineName);
    loadScripts(engine);
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 99 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class ScriptTemplateView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        ScriptEngine engine = getEngine();
        Invocable invocable = (Invocable) engine;
        String url = getUrl();
        String template = getTemplate(url);
        Function<String, String> templateLoader = path -> {
            try {
                return getTemplate(path);
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }
        };
        RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
        Object html;
        if (this.renderObject != null) {
            Object thiz = engine.eval(this.renderObject);
            html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
        } else {
            html = invocable.invokeFunction(this.renderFunction, template, model, context);
        }
        response.getWriter().write(String.valueOf(html));
    } catch (ScriptException ex) {
        throw new ServletException("Failed to render script template", new StandardScriptEvalException(ex));
    }
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) HashMap(java.util.HashMap) ApplicationContextException(org.springframework.context.ApplicationContextException) Function(java.util.function.Function) StandardScriptUtils(org.springframework.scripting.support.StandardScriptUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) Charset(java.nio.charset.Charset) Locale(java.util.Locale) Map(java.util.Map) ScriptException(javax.script.ScriptException) Resource(org.springframework.core.io.Resource) ResourceLoader(org.springframework.core.io.ResourceLoader) ObjectUtils(org.springframework.util.ObjectUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) StandardCharsets(java.nio.charset.StandardCharsets) NamedThreadLocal(org.springframework.core.NamedThreadLocal) Invocable(javax.script.Invocable) AbstractUrlBasedView(org.springframework.web.servlet.view.AbstractUrlBasedView) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ScriptEngine(javax.script.ScriptEngine) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) FileCopyUtils(org.springframework.util.FileCopyUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) ServletException(javax.servlet.ServletException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 100 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class StandardScriptUtils method retrieveEngineByName.

/**
	 * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager}
	 * by name, delegating to {@link ScriptEngineManager#getEngineByName} but
	 * throwing a descriptive exception if not found or if initialization failed.
	 * @param scriptEngineManager the ScriptEngineManager to use
	 * @param engineName the name of the engine
	 * @return a corresponding ScriptEngine (never {@code null})
	 * @throws IllegalArgumentException if no matching engine has been found
	 * @throws IllegalStateException if the desired engine failed to initialize
	 */
public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) {
    ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
    if (engine == null) {
        Set<String> engineNames = new LinkedHashSet<>();
        for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) {
            List<String> factoryNames = engineFactory.getNames();
            if (factoryNames.contains(engineName)) {
                // If it happens to initialize fine now, alright, but we really expect an exception.
                try {
                    engine = engineFactory.getScriptEngine();
                    engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE);
                } catch (Throwable ex) {
                    throw new IllegalStateException("Script engine with name '" + engineName + "' failed to initialize", ex);
                }
            }
            engineNames.addAll(factoryNames);
        }
        throw new IllegalArgumentException("Script engine with name '" + engineName + "' not found; registered engine names: " + engineNames);
    }
    return engine;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)147 ScriptEngineManager (javax.script.ScriptEngineManager)49 ScriptException (javax.script.ScriptException)38 Test (org.junit.Test)26 IOException (java.io.IOException)16 Bindings (javax.script.Bindings)15 Map (java.util.Map)13 Invocable (javax.script.Invocable)12 FileReader (java.io.FileReader)9 HashMap (java.util.HashMap)9 ScriptContext (javax.script.ScriptContext)9 ScriptEngineFactory (javax.script.ScriptEngineFactory)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 SimpleBindings (javax.script.SimpleBindings)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7 File (java.io.File)6 InputStreamReader (java.io.InputStreamReader)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6