use of javax.script.Bindings in project sling by apache.
the class RhinoJavaScriptEngineTest method testPreserveScopeBetweenEvals.
public void testPreserveScopeBetweenEvals() throws ScriptException {
MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
Bindings context = new SimpleBindings();
engine.eval("var f = 1", context);
Object result = null;
try {
result = engine.eval("f += 1", context);
} catch (ScriptException e) {
TestCase.fail(e.getMessage());
}
assertTrue(result instanceof Double);
assertEquals(2.0, result);
}
use of javax.script.Bindings in project sling by apache.
the class RhinoJavaScriptEngine method getScriptName.
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;
}
use of javax.script.Bindings in project sling by apache.
the class RenderUnitProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
if (identifier.endsWith("." + SightlyScriptEngineFactory.EXTENSION)) {
Bindings globalBindings = renderContext.getBindings();
SlingScriptHelper sling = BindingsUtils.getHelper(globalBindings);
SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
final Resource renderUnitResource = ScriptUtils.resolveScript(scriptingResourceResolverProvider.getRequestScopedResourceResolver(), renderContext, identifier);
if (renderUnitResource == null) {
Resource caller = ResourceResolution.getResourceForRequest(request.getResourceResolver(), request);
if (caller != null) {
String resourceSuperType = caller.getResourceSuperType();
StringBuilder errorMessage = new StringBuilder("Cannot find resource ");
errorMessage.append(identifier).append(" for base path ").append(caller.getPath());
if (StringUtils.isNotEmpty(resourceSuperType)) {
errorMessage.append(" with resource super type ").append(resourceSuperType);
}
errorMessage.append(".");
return ProviderOutcome.failure(new SightlyException(errorMessage.toString()));
} else {
return ProviderOutcome.failure(new SightlyException("Cannot resolve template " + identifier + " for script " + sling.getScript().getScriptResource().getPath()));
}
}
RenderUnit renderUnit;
try {
CachedScript cachedScript = scriptCache.getScript(renderUnitResource.getPath());
final SightlyCompiledScript compiledScript;
if (cachedScript != null) {
compiledScript = (SightlyCompiledScript) cachedScript.getCompiledScript();
} else {
SightlyScriptEngine sightlyScriptEngine = (SightlyScriptEngine) scriptEngineManager.getEngineByName(SightlyScriptEngineFactory.SHORT_NAME);
String encoding = renderUnitResource.getResourceMetadata().getCharacterEncoding();
if (StringUtils.isEmpty(encoding)) {
encoding = "UTF-8";
}
InputStream inputStream = renderUnitResource.adaptTo(InputStream.class);
if (inputStream == null) {
return ProviderOutcome.failure();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);
ScriptNameAwareReader reader = new ScriptNameAwareReader(inputStreamReader, renderUnitResource.getPath());
compiledScript = (SightlyCompiledScript) sightlyScriptEngine.compile(reader);
scriptCache.putScript(new CachedScript() {
@Override
public String getScriptPath() {
return renderUnitResource.getPath();
}
@Override
public CompiledScript getCompiledScript() {
return compiledScript;
}
});
}
renderUnit = compiledScript.getRenderUnit();
return ProviderOutcome.success(renderUnit);
} catch (Exception e) {
return ProviderOutcome.failure(e);
}
}
return ProviderOutcome.failure();
}
use of javax.script.Bindings in project sling by apache.
the class SightlyScriptEngine method getScriptName.
private String getScriptName(ScriptContext scriptContext) {
if (scriptContext != null) {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
String scriptName = (String) bindings.get(ScriptEngine.FILENAME);
if (scriptName != null && !"".equals(scriptName)) {
return scriptName;
}
SlingScriptHelper sling = BindingsUtils.getHelper(bindings);
if (sling != null) {
return sling.getScript().getScriptResource().getPath();
}
}
return NO_SCRIPT;
}
use of javax.script.Bindings in project sling by apache.
the class SightlyScriptEngine method eval.
@Override
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
checkArguments(reader, scriptContext);
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
final SlingHttpServletRequest request = slingBindings.getRequest();
if (request == null) {
throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
}
final Object oldValue = request.getAttribute(SlingBindings.class.getName());
try {
request.setAttribute(SlingBindings.class.getName(), slingBindings);
SightlyCompiledScript compiledScript = internalCompile(reader, scriptContext);
return compiledScript.eval(scriptContext);
} catch (Exception e) {
throw new ScriptException(e);
} finally {
request.setAttribute(SlingBindings.class.getName(), oldValue);
}
}
Aggregations