Java Code Examples for javax.script.SimpleBindings#putAll()

The following examples show how to use javax.script.SimpleBindings#putAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionRule.java    From frostmourne with MIT License 6 votes vote down vote up
@Override
public boolean verify(AlarmProcessLogger alarmProcessLogger, RuleContract ruleContract, MetricContract metricContract, IMetric metric) {
    Map<String, Object> context = context(alarmProcessLogger, ruleContract, metricContract, metric);

    SimpleBindings simpleBindings = new SimpleBindings();
    simpleBindings.putAll(context);
    Object result = null;
    String expression = ruleContract.getSettings().get("EXPRESSION");
    try {
        result = scriptEngine.eval(expression, simpleBindings);
    } catch (ScriptException ex) {
        LOGGER.error("error when execute js expression: " + expression, ex);
        throw new RuntimeException("error when execute js expression: " + expression, ex);
    }
    return (Boolean) result;
}
 
Example 2
Source File: MeecrowaveRunMojo.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private void scriptCustomization(final List<String> customizers, final String ext, final Map<String, Object> customBindings) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("project", project);
            engine.eval(new StringReader(js), bindings);
            bindings.putAll(customBindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
 
Example 3
Source File: ScriptCondition.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the script
 * 
 * @param baseDir
 * @param candidates
 * @return
 */
@SuppressWarnings("unchecked")
public List<PathWithAttributes> selectFilesToDelete(final Path basePath, final List<PathWithAttributes> candidates) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("basePath", basePath);
    bindings.put("pathList", candidates);
    bindings.putAll(configuration.getProperties());
    bindings.put("configuration", configuration);
    bindings.put("substitutor", configuration.getStrSubstitutor());
    bindings.put("statusLogger", LOGGER);
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return (List<PathWithAttributes>) object;
}
 
Example 4
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                     final Object... params) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", new SimpleMessage(msg));
    bindings.put("parameters", params);
    bindings.put("throwable", null);
    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;
}
 
Example 5
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
                     final Throwable t) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", msg instanceof String ? new SimpleMessage((String)msg) : new ObjectMessage(msg));
    bindings.put("parameters", null);
    bindings.put("throwable", t);
    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;
}
 
Example 6
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
                     final Throwable t) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", msg);
    bindings.put("parameters", null);
    bindings.put("throwable", t);
    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;
}
 
Example 7
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: ScriptPatternSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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;
}