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);
}
}
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);
}
});
}
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;
}
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));
}
}
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;
}
Aggregations