SLING-4935 - Rhino scripts not based on a resource will not get cached correctly by the Sling Script Cache
* avoid caching scripts for which a name cannot be extracted from the bindings / script resource
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1695020 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java b/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
index d04d3d4..158ff22 100644
--- a/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
+++ b/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
@@ -117,7 +117,10 @@
return slingCompiledScript;
}
};
- scriptCache.putScript(cachedScript);
+ // SLING-4935 avoid caching scripts for which we cannot determine a name
+ if (!scriptName.equals(NO_SCRIPT_NAME)) {
+ scriptCache.putScript(cachedScript);
+ }
LOGGER.debug("Added {} script to Script Cache.", scriptName);
return slingCompiledScript;
} catch (IOException e) {
@@ -226,13 +229,6 @@
@Override
public Object eval(ScriptContext scriptContext) throws ScriptException {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
- String scriptName = "NO_SCRIPT_NAME";
- {
- SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
- if (helper != null) {
- scriptName = helper.getScript().getScriptResource().getPath();
- }
- }
// container for replaced properties
Map<String, Object> replacedProperties = null;
@@ -314,9 +310,8 @@
// prevent variables to be pushed back in case of errors
isTopLevelCall = false;
-
- final ScriptException se = new ScriptException(
- "Failure running script " + scriptName + ": " + t.getMessage());
+ String scriptName = getScriptName(scriptContext);
+ final ScriptException se = new ScriptException("Failure running script " + scriptName + ": " + t.getMessage());
se.initCause(t);
throw se;
@@ -362,4 +357,17 @@
}
return NO_SCRIPT_NAME;
}
+
+ private String getScriptName(ScriptContext scriptContext) {
+ Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+ String scriptName = (String) bindings.get(ScriptEngine.FILENAME);
+ if (scriptName != null && !"".equals(scriptName)) {
+ return scriptName;
+ }
+ SlingScriptHelper sling = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
+ if (sling != null) {
+ return sling.getScript().getScriptResource().getPath();
+ }
+ return NO_SCRIPT_NAME;
+ }
}