Search in sources :

Example 1 with JSObject

use of jdk.nashorn.api.scripting.JSObject in project sling by apache.

the class Module method runScript.

/**
     *
     * @return @throws ScriptException
     */
public Object runScript() throws ScriptException {
    log.debug("run script with id {}", get("id"));
    ScriptObjectMirror function = factory.getModuleCache().get((String) get(CONTEXT_FIELD_FILENAME));
    Resource moduleResource = (Resource) get(CONTEXT_FIELD_MODULE_RESOURCE);
    Resource resource = (Resource) get(CONTEXT_FIELD_RESOURCE);
    if (function == null) {
        if (moduleScript.isJsFile()) {
            function = Module.this.decorateScript(//readScript(moduleResource)//  
            readScript(moduleResource));
        }
        if (moduleScript.isJsonFile()) {
            String jsonfile = readScript(moduleResource);
            function = decorateScript("module.exports = " + jsonfile, false);
        }
        if (moduleScript.isResourceFile()) {
            Iterator<Resource> resChildren = moduleResource.listChildren();
            ArrayList<Resource> children = new ArrayList<Resource>();
            resChildren.forEachRemaining(children::add);
            put("children", children);
            /*ValueMap map = moduleResource.adaptTo(ValueMap.class);

                 Invocable invocable = (Invocable) factory.getNashornEngine();
                 Object jsonprop = null;
                 try {
                 jsonprop = invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "stringify", map);
                 } catch (NoSuchMethodException ex) {
                 throw new ScriptException(ex);
                 }
                 */
            SimpleResource simpleResource = moduleResource.adaptTo(SimpleResource.class);
            put("simpleResource", simpleResource);
            /*   String source = "exports.properties =  " + jsonprop + ";"
                 + "exports.path = currentNode.resource.path;"
                 + "exports.simpleResource = this.simpleResource;"
                 + "exports.children = this.children;";*/
            String source = "module.exports = this.simpleResource;";
            function = decorateScript(source, false);
        }
        if (!moduleScript.isResourceFile()) {
            factory.getModuleCache().put(moduleScript.getResource().getPath(), function);
        }
    } else {
        log.debug("module " + get(CONTEXT_FIELD_ID) + " received from cache");
    }
    if (moduleScript.isTextFile()) {
        log.debug("is textfile loaidng file");
        String source = StringEscapeUtils.escapeEcmaScript(readScript(moduleResource));
        log.debug("sourcE: ");
        source = "module.exports = \"" + source + "\";";
        log.debug(source);
        function = Module.this.decorateScript(source);
        factory.getModuleCache().put(moduleScript.getResource().getPath(), function);
    }
    JSObject process = (JSObject) factory.getNashornEngine().eval("Object");
    process.setMember("domain", log);
    if (function != null) {
        SimpleBindings currentNode = new SimpleBindings();
        if (resource != null) {
            currentNode.put("resource", resource);
            currentNode.put("properties", resource.adaptTo(ValueMap.class));
        } else {
            log.debug("module id {} resource is null", get(CONTEXT_FIELD_ID));
        }
        // changed require to be generated within the wrapper
        // TODO: need to be refactored
        function.call(this, get(CONTEXT_FIELD_EXPORTS), this, this, get(CONTEXT_FIELD_FILENAME), ((Resource) get(CONTEXT_FIELD_MODULE_RESOURCE)).getParent().getPath(), currentNode, (ConsoleLog) get(CONTEXT_FIELD_CONSOLE), null, (SlingScriptHelper) get(CONTEXT_FIELD_SLING), resource.adaptTo(SimpleResource.class));
    } else {
        log.warn("function not called because it is null");
    }
    put(CONTEXT_FIELD_EXPORTS, get(CONTEXT_FIELD_EXPORTS));
    return get(CONTEXT_FIELD_EXPORTS);
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) SimpleBindings(javax.script.SimpleBindings) ValueMap(org.apache.sling.api.resource.ValueMap) SimpleResource(org.apache.sling.scripting.esx.plugins.SimpleResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) SimpleResource(org.apache.sling.scripting.esx.plugins.SimpleResource) JSObject(jdk.nashorn.api.scripting.JSObject)

Example 2 with JSObject

use of jdk.nashorn.api.scripting.JSObject 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 3 with JSObject

use of jdk.nashorn.api.scripting.JSObject 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

JSObject (jdk.nashorn.api.scripting.JSObject)3 Invocable (javax.script.Invocable)2 ScriptException (javax.script.ScriptException)2 Resource (org.apache.sling.api.resource.Resource)2 SimpleResource (org.apache.sling.scripting.esx.plugins.SimpleResource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 SimpleBindings (javax.script.SimpleBindings)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1