use of org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl 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 org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl in project sling by apache.
the class SightlyJavaCompilerServiceTest method getInstancePojoTest.
private void getInstancePojoTest(String className) throws Exception {
RenderContextImpl renderContext = Mockito.mock(RenderContextImpl.class);
JavaCompiler javaCompiler = Mockito.mock(JavaCompiler.class);
CompilationResult compilationResult = Mockito.mock(CompilationResult.class);
when(compilationResult.getErrors()).thenReturn(new ArrayList<CompilerMessage>());
when(javaCompiler.compile(Mockito.any(CompilationUnit[].class), Mockito.any(Options.class))).thenReturn(compilationResult);
when(classLoaderWriter.getClassLoader().loadClass(className)).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return MockPojo.class;
}
});
Whitebox.setInternalState(compiler, "classLoaderWriter", classLoaderWriter);
Whitebox.setInternalState(compiler, "javaCompiler", javaCompiler);
Whitebox.setInternalState(compiler, "scriptingResourceResolverProvider", scriptingResourceResolverProvider);
Object obj = compiler.getResourceBackedUseObject(renderContext, className);
assertTrue("Expected to obtain a " + MockPojo.class.getName() + " object.", obj instanceof MockPojo);
}
Aggregations