Search in sources :

Example 86 with ScriptException

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);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 87 with ScriptException

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);
            }
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ZipFile(java.util.zip.ZipFile) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Example 88 with ScriptException

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;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) XplBuilder(org.apache.sling.scripting.xproc.xpl.XplBuilder) Pipeline(org.apache.sling.scripting.xproc.xpl.api.Pipeline)

Example 89 with ScriptException

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;
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) SimpleResource(org.apache.sling.scripting.esx.plugins.SimpleResource) Resource(org.apache.sling.api.resource.Resource) JSObject(jdk.nashorn.api.scripting.JSObject) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JSObject(jdk.nashorn.api.scripting.JSObject)

Example 90 with ScriptException

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);
    }
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) JSObject(jdk.nashorn.api.scripting.JSObject)

Aggregations

ScriptException (javax.script.ScriptException)98 IOException (java.io.IOException)41 ScriptEngine (javax.script.ScriptEngine)36 Bindings (javax.script.Bindings)27 ScriptEngineManager (javax.script.ScriptEngineManager)17 CompiledScript (javax.script.CompiledScript)12 InputStreamReader (java.io.InputStreamReader)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)10 Map (java.util.Map)9 SimpleBindings (javax.script.SimpleBindings)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 Writer (java.io.Writer)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)4