use of javax.script.Bindings in project sling by apache.
the class SightlyCompiledScript method eval.
@Override
public Object eval(ScriptContext context) throws ScriptException {
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
SlingHttpServletRequest request = slingBindings.getRequest();
if (request == null) {
throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
}
Object oldBindings = request.getAttribute(SlingBindings.class.getName());
try {
request.setAttribute(SlingBindings.class.getName(), slingBindings);
RenderContext renderContext = new RenderContextImpl(context);
PrintWriter out = new PrintWriter(context.getWriter());
renderUnit.render(out, renderContext, new SimpleBindings());
} finally {
request.setAttribute(SlingBindings.class.getName(), oldBindings);
}
return null;
}
use of javax.script.Bindings in project sling by apache.
the class ResourceRuntimeExtension method provideResource.
private String provideResource(final RenderContext renderContext, Object pathObj, Map<String, Object> options) {
Map<String, Object> opts = new HashMap<>(options);
final Bindings bindings = renderContext.getBindings();
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Map originalAttributes = ExtensionUtils.setRequestAttributes(request, (Map) options.remove(OPTION_REQUEST_ATTRIBUTES));
RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
String resourceType = runtimeObjectModel.toString(getAndRemoveOption(opts, OPTION_RESOURCE_TYPE));
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
if (pathObj instanceof Resource) {
Resource includedResource = (Resource) pathObj;
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
} else {
String includePath = runtimeObjectModel.toString(pathObj);
// build path completely
includePath = buildPath(includePath, options, request.getResource());
if (includePath != null) {
// check if path identifies an existing resource
Resource includedResource = request.getResourceResolver().getResource(includePath);
PathInfo pathInfo;
if (includedResource != null) {
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
} else {
// analyse path and decompose potential selectors from the path
pathInfo = new PathInfo(includePath);
Map<String, String> dispatcherOptionsMap = handleSelectors(request, pathInfo.selectors, opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, pathInfo.path, dispatcherOptions, resourceType);
}
} else {
// use the current resource
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, request.getResource(), dispatcherOptions, resourceType);
}
}
ExtensionUtils.setRequestAttributes(request, originalAttributes);
return writer.toString();
}
use of javax.script.Bindings in project sling by apache.
the class RenderUnit method render.
/**
* Render the main script template
*
* @param out the {@link PrintWriter} to which the commands are written
* @param renderContext the rendering context
* @param arguments the arguments for this unit
*/
public final void render(PrintWriter out, RenderContext renderContext, Bindings arguments) {
Bindings globalBindings = renderContext.getBindings();
render(out, buildGlobalScope(globalBindings), new CaseInsensitiveBindings(arguments), renderContext);
}
use of javax.script.Bindings in project sling by apache.
the class JsEnvironment method runResource.
public void runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments, UnaryCallback callback) {
ScriptContext scriptContext = new SimpleScriptContext();
CommonJsModule module = new CommonJsModule();
Bindings scriptBindings = buildBindings(scriptResource, globalBindings, arguments, module);
scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(ScriptEngine.FILENAME, scriptResource.getPath(), ScriptContext.ENGINE_SCOPE);
runScript(scriptResource, scriptContext, callback, module);
}
use of javax.script.Bindings in project java8-tutorial by winterbe.
the class Nashorn11 method test5.
private static void test5() throws ScriptException {
NashornScriptEngine engine = createEngine();
engine.eval("var obj = { foo: 'foo' };");
engine.eval("function printFoo() { print(obj.foo) };");
ScriptContext defaultContext = engine.getContext();
Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
SimpleScriptContext context1 = new SimpleScriptContext();
context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
SimpleScriptContext context2 = new SimpleScriptContext();
context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
engine.eval("obj.foo = 'bar';", context1);
engine.eval("printFoo();", context1);
engine.eval("printFoo();", context2);
}
Aggregations