use of javax.script.ScriptContext in project GNS by MobilityFirst.
the class ActiveNonBlockingRunner method updateCache.
/**
* Update cache needs to be synchronized, as some code cache may not be evaled before being used.
*
* @param codeId
* @param code
* @throws ScriptException
*/
private synchronized void updateCache(String codeId, String code) throws ScriptException {
if (!contexts.containsKey(codeId)) {
// Create a context if one does not yet exist and eval the code
ScriptContext sc = new SimpleScriptContext();
contexts.put(codeId, sc);
codeHashes.put(codeId, code.hashCode());
engine.eval(code, sc);
} else if (codeHashes.get(codeId) != code.hashCode()) {
// The context exists, but we need to eval the new code
ScriptContext sc = contexts.get(codeId);
codeHashes.put(codeId, code.hashCode());
engine.eval(code, sc);
}
}
use of javax.script.ScriptContext in project GNS by MobilityFirst.
the class ActiveTrustedRunner method updateCache.
private synchronized void updateCache(String codeId, String code) throws ScriptException {
if (!contexts.containsKey(codeId)) {
// Create a context if one does not yet exist and eval the code
ScriptContext sc = new SimpleScriptContext();
contexts.put(codeId, sc);
codeHashes.put(codeId, code.hashCode());
engine.eval(code, sc);
} else if (codeHashes.get(codeId) != code.hashCode()) {
// The context exists, but we need to eval the new code
ScriptContext sc = contexts.get(codeId);
codeHashes.put(codeId, code.hashCode());
engine.eval(code, sc);
}
}
use of javax.script.ScriptContext in project sling by apache.
the class SightlyCompiledScriptTest method testEvalSlingBindings.
/**
* Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
* attributes.
* @throws ScriptException
*/
@Test
public void testEvalSlingBindings() throws ScriptException {
ScriptEngine scriptEngine = mock(ScriptEngine.class);
final RenderUnit renderUnit = mock(RenderUnit.class);
Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
final BundleContext bundleContext = MockOsgi.newBundleContext();
bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
ScriptContext scriptContext = mock(ScriptContext.class);
StringWriter writer = new StringWriter();
when(scriptContext.getWriter()).thenReturn(writer);
Bindings scriptContextBindings = new SimpleBindings() {
{
put("test", "testValue");
put(SlingBindings.REQUEST, request);
put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
}
};
SlingBindings oldBindings = new SlingBindings();
oldBindings.put("old", "oldValue");
request.setAttribute(SlingBindings.class.getName(), oldBindings);
when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
compiledScript.eval(scriptContext);
ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
// request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
int invocation = 1;
for (SlingBindings bindings : slingBindingsValues) {
switch(invocation) {
case 1:
assertEquals(oldBindings, bindings);
break;
case 2:
assertEquals(3, bindings.size());
for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
assertEquals(entry.getValue(), bindings.get(entry.getKey()));
}
break;
case 3:
assertEquals(oldBindings, bindings);
}
invocation++;
}
for (String key : attributeNameArgumentCaptor.getAllValues()) {
assertEquals(SlingBindings.class.getName(), key);
}
}
use of javax.script.ScriptContext 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.ScriptContext in project perry by ca-cwds.
the class AbacMethodInterceptor method checkPermission.
@SuppressWarnings("unchecked")
private void checkPermission(String permission, Object arg) throws ScriptException {
AbacPermission abacPermission = new AbacPermission(permission);
String selector = abacPermission.getSecuredObject().toString();
int dotIndex = selector.indexOf('.');
String identifier;
if (dotIndex == -1) {
identifier = selector;
} else {
identifier = selector.substring(0, dotIndex);
}
ScriptContext scriptContext = new SimpleScriptContext();
scriptContext.setAttribute(identifier, arg, ScriptContext.ENGINE_SCOPE);
for (Object o : (Collection<Object>) scriptEngine.eval("[" + selector + "].flatten()", scriptContext)) {
abacPermission.setSecuredObject(o);
SecurityUtils.getSubject().checkPermission(abacPermission);
}
}
Aggregations