use of javax.script.ScriptException in project tomee by apache.
the class TomEEEmbeddedMojo method scriptCustomization.
private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
if (customizers == null || customizers.isEmpty()) {
return;
}
final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
if (engine == null) {
throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
}
for (final String js : customizers) {
try {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("catalinaBase", base);
engine.eval(new StringReader(js), bindings);
} catch (final ScriptException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
use of javax.script.ScriptException in project tomee by apache.
the class AbstractTomEEMojo method scriptCustomization.
private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException {
if (customizers != null) {
final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
if (engine == null) {
throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
}
for (final String js : customizers) {
try {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("catalinaBase", catalinaBase.getAbsolutePath());
bindings.put("resolver", new Resolver() {
@Override
public File resolve(final String group, final String artifact, final String version, final String classifier, final String type) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public File resolve(final String group, final String artifact, final String version) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public File resolve(final String group, final String artifact, final String version, final String type) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
});
engine.eval(new StringReader(js), bindings);
} catch (final ScriptException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
}
use of javax.script.ScriptException in project sling by apache.
the class XProcScriptEngine method eval.
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
String scriptName = helper.getScript().getScriptResource().getPath();
try {
XplBuilder xplBuilder = new XplBuilder();
Pipeline xpl = (Pipeline) xplBuilder.build(reader);
xpl.getEnv().setSling(helper);
xpl.eval();
} catch (Throwable t) {
log.error("Failure running XProc script.", t);
final ScriptException se = new ScriptException("Failure running XProc script " + scriptName);
se.initCause(t);
throw se;
}
return null;
}
use of javax.script.ScriptException in project sling by apache.
the class Module method loadAsDirectory.
/**
*
* @param module
* @param path
* @param currentResource
* @return
* @throws ScriptException
*/
public ModuleScript loadAsDirectory(String module, String path, Resource currentResource, String loader) throws ScriptException {
ResourceResolver resolver = currentResource.getResourceResolver();
Resource packageJson = resolver.getResource(path + "/package.json");
if (packageJson != null) {
Node jsonFile = packageJson.adaptTo(Node.class);
try {
boolean isFile = (jsonFile.isNodeType(NodeType.NT_FILE) || jsonFile.isNodeType(NodeType.NT_RESOURCE));
if (isFile) {
InputStream is = packageJson.getChild("jcr:content").adaptTo(InputStream.class);
try {
String jsonData = IOUtils.toString(is);
Invocable invocable = (Invocable) factory.getNashornEngine();
JSObject jsonprop = null;
try {
jsonprop = (JSObject) invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "parse", jsonData);
} catch (NoSuchMethodException ex) {
throw new ScriptException(ex);
}
Object main = jsonprop.getMember("main");
if (main != null) {
String packageModule = (String) main;
String mainpath = normalizePath(packageModule, path);
return loadAsFile(packageModule, mainpath, currentResource, loader);
}
} catch (IOException ex) {
throw new ScriptException(ex);
}
}
} catch (RepositoryException ex) {
throw new ScriptException(ex);
}
}
Resource indexjs = resolver.getResource(path + "/index.js");
if (indexjs != null) {
return createModuleScript(indexjs, ModuleScript.JS_FILE);
}
Resource indexjson = resolver.getResource(path + "/index.json");
if (indexjson != null) {
return createModuleScript(indexjson, ModuleScript.JSON_FILE);
}
Resource indexnode = resolver.getResource(path + "/index.node");
if (indexnode != null) {
throw new ScriptException("Node module .node (binary) loading is currently not supported");
}
return null;
}
use of javax.script.ScriptException in project sling by apache.
the class ScriptSandboxServiceImpl method compileSource.
@Override
public String compileSource(String source) throws ScriptException {
Invocable inv = (Invocable) scriptEngine;
JSObject rs;
try {
rs = (JSObject) inv.invokeMethod(babel, "transform", source, babelOptions);
return rs.getMember("code").toString();
} catch (ScriptException ex) {
throw new ScriptException(ex);
} catch (NoSuchMethodException ex) {
throw new ScriptException(ex);
}
}
Aggregations