Search in sources :

Example 21 with SimpleBindings

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings)

Example 22 with SimpleBindings

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Example 23 with SimpleBindings

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);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) HashSet(java.util.HashSet)

Example 24 with SimpleBindings

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);
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 25 with SimpleBindings

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);
}
Also used : RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ProviderOutcome(org.apache.sling.scripting.sightly.use.ProviderOutcome) UseProvider(org.apache.sling.scripting.sightly.use.UseProvider) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings)

Aggregations

SimpleBindings (javax.script.SimpleBindings)67 Bindings (javax.script.Bindings)47 Test (org.junit.Test)20 ScriptContext (javax.script.ScriptContext)14 SimpleScriptContext (javax.script.SimpleScriptContext)12 Test (org.testng.annotations.Test)11 ScriptException (javax.script.ScriptException)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 StringWriter (java.io.StringWriter)5 HashMap (java.util.HashMap)5 ScriptEngine (javax.script.ScriptEngine)5 PrintWriter (java.io.PrintWriter)4 CompiledScript (javax.script.CompiledScript)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 StringReader (java.io.StringReader)3 Map (java.util.Map)3 ScriptEngineManager (javax.script.ScriptEngineManager)3 Resource (org.apache.sling.api.resource.Resource)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ProtectedBindings (org.apache.sling.scripting.core.impl.helper.ProtectedBindings)3