private ModuleScope loadModule()

in src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java [228:307]


    private ModuleScope loadModule(Context cx, String modulePath, ModuleScope moduleScope, Scriptable thisObj) {
        String absolutePath = modulePath;
        if (modulePath.startsWith(".")) {
            // relative
            if (moduleScope == null) {
                throw Context.reportRuntimeError("Cannot resolve relative module name outside of a module scope.");
            }
            absolutePath = (moduleScope.getModuleName() + "/" + modulePath).replaceAll("[^/]*/\\./", "");
            while (absolutePath.matches("([^/]*/)?[^/]*/\\.\\./")) {
                absolutePath = absolutePath.replaceAll("([^/]*/)?[^/]*/\\.\\./", "");
            }
        }
        absolutePath = absolutePath + ".js";

        SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING, SlingScriptHelper.class);
        if (sling == null) {
            throw new NullPointerException(SlingBindings.SLING);
        }
        ResourceResolver resrev = sling.getScript().getScriptResource().getResourceResolver();

        Resource script = null;
        String scriptName = null;
        for (String basepath : resrev.getSearchPath()) {
            script = resrev.resolve(basepath + absolutePath);
            if (script != null && !(script instanceof NonExistingResource)) {
                scriptName = basepath + absolutePath;
                break;
            }
        }
        if (script == null) {
            throw Context.reportRuntimeError("Unable to resolve module " + absolutePath + " in search path");
        }

        InputStream scriptStream = script.adaptTo(InputStream.class);
        if (scriptStream == null) {
            // try once again
            scriptStream = resrev.resolve(scriptName).adaptTo(InputStream.class);
            if (scriptStream == null) {
                throw Context.reportRuntimeError("Script file " + script.getPath() + " cannot be read");
            }
        }

        try {
            // reader for the stream
            Reader scriptReader = new InputStreamReader(scriptStream, Charset.forName("UTF-8"));

            // check whether we have to wrap the basic reader
            if (scriptName.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
                scriptReader = new EspReader(scriptReader);
            }

            // read the suff buffered for better performance
            scriptReader = new BufferedReader(scriptReader);

            // TODO: execute script with ModuleScope
            // now, let's go

            ModuleScope scope = moduleScope;
            if (scope == null) {
                scope = new ModuleScope(thisObj, absolutePath.substring(0, absolutePath.length() - 3));
            } else {
                scope.reset();
            }

            cx.evaluateReader(scope, scriptReader, scriptName, 1, null);

            return scope;

        } catch (IOException ioe) {

            throw Context.reportRuntimeError("Failure reading file " + scriptName + ": " + ioe);

        } finally {
            // ensure the script input stream is closed
            try {
                scriptStream.close();
            } catch (IOException ignore) {
            }
        }
    }