Search in sources :

Example 1 with ScriptCachingContext

use of org.jaggeryjs.scriptengine.cache.ScriptCachingContext in project jaggery by wso2.

the class JaggeryDeployerManager method executeScripts.

private static void executeScripts(Context context, JSONArray arr) {
    if (arr != null) {
        try {
            JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(context.getServletContext());
            CommonManager.setJaggeryContext(sharedContext);
            RhinoEngine engine = sharedContext.getEngine();
            org.mozilla.javascript.Context cx = engine.enterContext();
            ServletContext servletContext = (ServletContext) sharedContext.getProperty(org.jaggeryjs.hostobjects.web.Constants.SERVLET_CONTEXT);
            ScriptableObject sharedScope = sharedContext.getScope();
            Object[] scripts = arr.toArray();
            for (Object script : scripts) {
                if (!(script instanceof String)) {
                    log.error("Invalid value for initScripts/destroyScripts in jaggery.conf : " + script);
                    continue;
                }
                String path = (String) script;
                path = path.startsWith("/") ? path : "/" + path;
                Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                callstack.push(path);
                String[] parts = WebAppManager.getKeys(servletContext.getContextPath(), path, path);
                ScriptCachingContext sctx = new ScriptCachingContext(sharedContext.getTenantDomain(), parts[0], parts[1], parts[2]);
                sctx.setSecurityDomain(new JaggerySecurityDomain(path, servletContext));
                engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {

                    @Override
                    protected void build() throws IOException {
                        try {
                            sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                        } catch (ScriptException e) {
                        // throw new IOException(e);
                        }
                    }
                }, sharedScope, sctx);
            }
        } catch (ScriptException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (org.mozilla.javascript.Context.getCurrentContext() != null) {
                RhinoEngine.exitContext();
            }
        }
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 2 with ScriptCachingContext

use of org.jaggeryjs.scriptengine.cache.ScriptCachingContext in project jaggery by wso2.

the class WebAppManager method getScriptCachingContext.

protected static ScriptCachingContext getScriptCachingContext(HttpServletRequest request, String scriptPath) throws ScriptException {
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    String tenantId = jaggeryContext.getTenantDomain();
    String[] parts = getKeys(request.getContextPath(), scriptPath, scriptPath);
    /**
         * tenantId = tenantId
         * context = webapp context
         * path = relative path to the directory of *.js file
         * cacheKey = name of the *.js file being cached
         */
    ScriptCachingContext sctx = new ScriptCachingContext(tenantId, parts[0], parts[1], parts[2]);
    ServletContext servletContext = request.getServletContext();
    sctx.setSecurityDomain(new JaggerySecurityDomain(getNormalizedScriptPath(parts), servletContext));
    long lastModified = getScriptLastModified(servletContext, scriptPath);
    sctx.setSourceModifiedTime(lastModified);
    return sctx;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 3 with ScriptCachingContext

use of org.jaggeryjs.scriptengine.cache.ScriptCachingContext in project jaggery by wso2.

the class RhinoEngine method evalScript.

private Object evalScript(Reader scriptReader, ScriptableObject scope, ScriptCachingContext sctx) throws ScriptException {
    Context cx = enterContext();
    Object result;
    try {
        if (sctx == null) {
            result = cx.evaluateString(scope, HostObjectUtil.readerToString(scriptReader), "wso2js", 1, null);
        } else if (debugMode) {
            //If the server is started to debug scripts
            String scriptPath = sctx.getContext() + sctx.getPath() + sctx.getCacheKey();
            result = cx.evaluateString(scope, HostObjectUtil.readerToString(scriptReader), scriptPath, 1, null);
        } else {
            Script script = cacheManager.getScriptObject(scriptReader, sctx);
            if (script == null) {
                cacheManager.cacheScript(scriptReader, sctx);
                script = cacheManager.getScriptObject(scriptReader, sctx);
            }
            result = script.exec(cx, scope);
        }
        return result;
    } catch (Exception e) {
        throw new ScriptException(e);
    } finally {
        exitContext();
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 4 with ScriptCachingContext

use of org.jaggeryjs.scriptengine.cache.ScriptCachingContext in project jaggery by wso2.

the class ModuleManager method initScripts.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private void initScripts(Module moduleObject, Context cx, JavaScriptModule module, boolean isCustom) throws ScriptException {
    String name = null;
    String path = null;
    JavaScriptScript script;
    List scriptList = moduleObject.getScripts();
    Iterator itr = scriptList.iterator();
    while (itr.hasNext()) {
        try {
            //process methods
            org.jaggeryjs.jaggery.core.Script scriptObject = (org.jaggeryjs.jaggery.core.Script) itr.next();
            name = scriptObject.getName();
            path = scriptObject.getPath();
            script = new JavaScriptScript(name);
            Reader reader;
            final String fileName;
            ScriptCachingContext sctx;
            if (isCustom) {
                String filteredPath = filterPath(path);
                fileName = modulesDir + File.separator + module.getName() + File.separator + filterPath(path);
                reader = new FileReader(fileName);
                int endIndex = filteredPath.lastIndexOf(File.separator);
                sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), '<' + module.getName() + '>', filteredPath.substring(0, endIndex), filteredPath.substring(endIndex));
            } else {
                reader = new InputStreamReader(ModuleManager.class.getClassLoader().getResourceAsStream(path));
                fileName = modulesDir + File.separator + name;
                int endIndex = path.lastIndexOf('/');
                sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), "<<" + name + ">>", '/' + path.substring(0, endIndex), path.substring(endIndex));
            }
            CacheManager cacheManager = new CacheManager(null);
            sctx.setSecurityDomain(new RhinoSecurityDomain() {

                @SuppressFBWarnings("PATH_TRAVERSAL_IN")
                @Override
                public CodeSource getCodeSource() throws ScriptException {
                    try {
                        URL url = new File(fileName).getCanonicalFile().toURI().toURL();
                        return new CodeSource(url, (Certificate[]) null);
                    } catch (MalformedURLException e) {
                        throw new ScriptException(e);
                    } catch (IOException e) {
                        throw new ScriptException(e);
                    }
                }
            });
            sctx.setSourceModifiedTime(1);
            Script cachedScript = cacheManager.getScriptObject(reader, sctx);
            if (cachedScript == null) {
                cacheManager.cacheScript(reader, sctx);
                cachedScript = cacheManager.getScriptObject(reader, sctx);
            }
            script.setScript(cachedScript);
            module.addScript(script);
        } catch (FileNotFoundException e) {
            String msg = "Error executing script. Script cannot be found, name : " + name + ", path : " + path;
            log.error(msg, e);
            throw new ScriptException(msg, e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RhinoSecurityDomain(org.jaggeryjs.scriptengine.security.RhinoSecurityDomain) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) URL(java.net.URL) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Iterator(java.util.Iterator) CacheManager(org.jaggeryjs.scriptengine.cache.CacheManager) List(java.util.List) Script(org.mozilla.javascript.Script) ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) CodeSource(java.security.CodeSource) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with ScriptCachingContext

use of org.jaggeryjs.scriptengine.cache.ScriptCachingContext in project jaggery by wso2.

the class WebAppManager method executeScript.

private static ScriptableObject executeScript(JaggeryContext jaggeryContext, ScriptableObject scope, String fileURL, final boolean isJSON, boolean isBuilt, boolean isIncludeOnce) throws ScriptException {
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
    ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT);
    String parent = includesCallstack.lastElement();
    String[] keys = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
    fileURL = getNormalizedScriptPath(keys);
    if (includesCallstack.search(fileURL) != -1) {
        return scope;
    }
    if (isIncludeOnce && includedScripts.get(fileURL) != null) {
        return scope;
    }
    ScriptReader source;
    RhinoEngine engine = jaggeryContext.getEngine();
    if (isBuilt) {
        source = new ScriptReader(context.getResourceAsStream(fileURL)) {

            @Override
            protected void build() throws IOException {
                try {
                    if (isJSON) {
                        sourceReader = new StringReader("(" + HostObjectUtil.streamToString(sourceIn) + ")");
                    } else {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    }
                } catch (ScriptException e) {
                    throw new IOException(e);
                }
            }
        };
    } else {
        source = new ScriptReader(context.getResourceAsStream(fileURL));
    }
    ScriptCachingContext sctx = new ScriptCachingContext(jaggeryContext.getTenantDomain(), keys[0], keys[1], keys[2]);
    sctx.setSecurityDomain(new JaggerySecurityDomain(fileURL, context));
    long lastModified = WebAppManager.getScriptLastModified(context, fileURL);
    sctx.setSourceModifiedTime(lastModified);
    includedScripts.put(fileURL, true);
    includesCallstack.push(fileURL);
    if (isJSON) {
        scope = (ScriptableObject) engine.eval(source, scope, sctx);
    } else {
        engine.exec(source, scope, sctx);
    }
    includesCallstack.pop();
    return scope;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ServletContext(javax.servlet.ServletContext) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Aggregations

ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)5 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)4 ServletContext (javax.servlet.ServletContext)3 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)2 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)2 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 CodeSource (java.security.CodeSource)1 Iterator (java.util.Iterator)1 List (java.util.List)1 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)1 CacheManager (org.jaggeryjs.scriptengine.cache.CacheManager)1 RhinoSecurityDomain (org.jaggeryjs.scriptengine.security.RhinoSecurityDomain)1 JSONObject (org.json.simple.JSONObject)1 Script (org.mozilla.javascript.Script)1