in src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java [145:211]
private void load(Context cx, Scriptable thisObj, Object[] args) {
SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING, SlingScriptHelper.class);
if (sling == null) {
throw new NullPointerException(SlingBindings.SLING);
}
Scriptable globalScope = ScriptableObject.getTopLevelScope(thisObj);
Resource scriptResource = sling.getScript().getScriptResource();
ResourceResolver resolver = scriptResource.getResourceResolver();
// the path of the current script to resolve realtive paths
String currentScript = sling.getScript().getScriptResource().getPath();
String scriptParent = ResourceUtil.getParent(currentScript);
for (Object arg : args) {
String scriptName = ScriptRuntime.toString(arg);
Resource loadScript = null;
if (!scriptName.startsWith("/")) {
String absScriptName = scriptParent + "/" + scriptName;
loadScript = resolver.resolve(absScriptName);
}
// not resolved relative to the current script
if (loadScript == null) {
loadScript = resolver.resolve(scriptName);
}
if (loadScript == null) {
throw Context.reportRuntimeError("Script file " + scriptName + " not found");
}
InputStream scriptStream = loadScript.adaptTo(InputStream.class);
if (scriptStream == null) {
throw Context.reportRuntimeError("Script file " + scriptName + " cannot be read from");
}
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);
// now, let's go
cx.evaluateReader(globalScope, scriptReader, scriptName, 1, null);
} 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) {
}
}
}
}