use of javax.script.SimpleBindings in project logging-log4j2 by apache.
the class ScriptFilter method filter.
@Override
public Result filter(final LogEvent event) {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("logEvent", event);
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
use of javax.script.SimpleBindings in project logging-log4j2 by apache.
the class ScriptPatternSelector method getFormatters.
@Override
public PatternFormatter[] getFormatters(final LogEvent event) {
final SimpleBindings bindings = new SimpleBindings();
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
bindings.put("logEvent", event);
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
if (object == null) {
return defaultFormatters;
}
final PatternFormatter[] patternFormatter = formatterMap.get(object.toString());
return patternFormatter == null ? defaultFormatters : patternFormatter;
}
use of javax.script.SimpleBindings in project sling by apache.
the class ExportServlet method addScriptBindings.
private void addScriptBindings(SlingScriptHelper scriptHelper, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
SimpleBindings bindings = new SimpleBindings();
bindings.put(SLING, scriptHelper);
bindings.put(RESOURCE, request.getResource());
bindings.put(RESOLVER, request.getResource().getResourceResolver());
bindings.put(REQUEST, request);
bindings.put(RESPONSE, response);
bindings.put(READER, request.getReader());
bindings.put(OUT, response.getWriter());
bindings.put(LOG, logger);
final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProvidersByContext.getBindingsValuesProviders(scriptEngineFactory, BINDINGS_CONTEXT);
if (!bindingsValuesProviders.isEmpty()) {
Set<String> protectedKeys = new HashSet<String>();
protectedKeys.addAll(PROTECTED_KEYS);
ProtectedBindings protectedBindings = new ProtectedBindings(bindings, protectedKeys);
for (BindingsValuesProvider provider : bindingsValuesProviders) {
provider.addBindings(protectedBindings);
}
}
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
request.setAttribute(SlingBindings.class.getName(), slingBindings);
}
use of javax.script.SimpleBindings 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.SimpleBindings in project sling by apache.
the class UseRuntimeExtension method call.
@Override
public Object call(final RenderContext renderContext, Object... arguments) {
ExtensionUtils.checkArgumentCount(RuntimeFunction.USE, arguments, 2);
RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
String identifier = runtimeObjectModel.toString(arguments[0]);
if (StringUtils.isEmpty(identifier)) {
throw new SightlyException("data-sly-use needs to be passed an identifier");
}
Map<String, Object> useArgumentsMap = runtimeObjectModel.toMap(arguments[1]);
Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap));
ArrayList<UseProvider> providers = new ArrayList<>(providersMap.values());
ListIterator<UseProvider> iterator = providers.listIterator(providers.size());
while (iterator.hasPrevious()) {
UseProvider provider = iterator.previous();
ProviderOutcome outcome = provider.provide(identifier, renderContext, useArguments);
Throwable failureCause;
if (outcome.isSuccess()) {
return outcome.getResult();
} else if ((failureCause = outcome.getCause()) != null) {
throw new SightlyException("Identifier " + identifier + " cannot be correctly instantiated by the Use API", failureCause);
}
}
throw new SightlyException("No use provider could resolve identifier " + identifier);
}
Aggregations