Search in sources :

Example 46 with SimpleBindings

use of javax.script.SimpleBindings in project metrics by dropwizard.

the class PickledGraphiteTest method unpickleOutput.

private String unpickleOutput() throws Exception {
    StringBuilder results = new StringBuilder();
    // the charset is important. if the GraphitePickleReporter and this test
    // don't agree, the header is not always correctly unpacked.
    String payload = output.toString("UTF-8");
    PyList result = new PyList();
    int nextIndex = 0;
    while (nextIndex < payload.length()) {
        Bindings bindings = new SimpleBindings();
        bindings.put("payload", payload.substring(nextIndex));
        unpickleScript.eval(bindings);
        result.addAll(result.size(), (PyList) bindings.get("metrics"));
        nextIndex += ((BigInteger) bindings.get("batchLength")).intValue();
    }
    for (Object aResult : result) {
        PyTuple datapoint = (PyTuple) aResult;
        String name = datapoint.get(0).toString();
        PyTuple valueTuple = (PyTuple) datapoint.get(1);
        Object timestamp = valueTuple.get(0);
        Object value = valueTuple.get(1);
        results.append(name).append(" ").append(value).append(" ").append(timestamp).append("\n");
    }
    return results.toString();
}
Also used : SimpleBindings(javax.script.SimpleBindings) PyList(org.python.core.PyList) PyTuple(org.python.core.PyTuple) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 47 with SimpleBindings

use of javax.script.SimpleBindings in project cas by apereo.

the class ScriptingUtils method executeGroovyScriptEngine.

/**
 * Execute inline groovy script engine.
 *
 * @param <T>       the type parameter
 * @param script    the script
 * @param variables the variables
 * @param clazz     the clazz
 * @return the t
 */
public static <T> T executeGroovyScriptEngine(final String script, final Map<String, Object> variables, final Class<T> clazz) {
    try {
        val engine = new ScriptEngineManager().getEngineByName("groovy");
        val binding = new SimpleBindings();
        if (variables != null && !variables.isEmpty()) {
            binding.putAll(variables);
        }
        if (!binding.containsKey("logger")) {
            binding.put("logger", LOGGER);
        }
        val result = engine.eval(script, binding);
        return getGroovyScriptExecutionResultOrThrow(clazz, result);
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return null;
}
Also used : lombok.val(lombok.val) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) IOException(java.io.IOException)

Example 48 with SimpleBindings

use of javax.script.SimpleBindings in project meecrowave by apache.

the class MeecrowaveRunMojo method scriptCustomization.

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

Example 49 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class BatchResource method actionCollection.

@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context serverContext, ActionRequest actionRequest) {
    if (!actionRequest.getAction().equals(BATCH)) {
        final String msg = "Action '" + actionRequest.getAction() + "' not implemented for this resource";
        final NotSupportedException exception = new NotSupportedException(msg);
        debug.error(msg, exception);
        return exception.asPromise();
    }
    String scriptId = null;
    try {
        JsonValue scriptIdValue = actionRequest.getContent().get(SCRIPT_ID);
        if (scriptIdValue == null) {
            if (debug.errorEnabled()) {
                debug.error("BatchResource :: actionCollection - ScriptId null. Default scripts not implemented.");
            }
            return new BadRequestException().asPromise();
        } else {
            scriptId = scriptIdValue.asString();
        }
        final JsonValue requests = actionRequest.getContent().get(REQUESTS);
        final String realm = getRealm(serverContext);
        final ScriptConfiguration scriptConfig = scriptingServiceFactory.create(SubjectUtils.createSuperAdminSubject(), realm).get(scriptId);
        final ScriptObject script = new ScriptObject(scriptConfig.getName(), scriptConfig.getScript(), scriptConfig.getLanguage());
        final ScriptResponse response = new ScriptResponse();
        final Bindings bindings = new SimpleBindings();
        bindings.put(PAYLOAD, requests);
        bindings.put(CONTEXT, serverContext);
        bindings.put(LOGGER, debug);
        bindings.put(REQUESTER, requester);
        bindings.put(RESPONSE, response);
        return newResultPromise(newActionResponse((JsonValue) scriptEvaluator.evaluateScript(script, bindings)));
    } catch (ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return exceptionMappingHandler.handleError(serverContext, actionRequest, e).asPromise();
    } catch (javax.script.ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return new InternalServerErrorException().asPromise();
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) JsonValue(org.forgerock.json.JsonValue) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ScriptResponse(org.forgerock.openam.scripting.rest.batch.helpers.ScriptResponse)

Example 50 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class OidcClaimsExtensionTest method testBindings.

private <T> Bindings testBindings(Set<String> scopes, Map<String, Set<T>> requestedClaims) {
    Bindings scriptVariables = new SimpleBindings();
    scriptVariables.put("logger", logger);
    scriptVariables.put("claims", new HashMap<String, Object>());
    scriptVariables.put("accessToken", accessToken);
    scriptVariables.put("session", ssoToken);
    scriptVariables.put("identity", identity);
    scriptVariables.put("scopes", scopes);
    scriptVariables.put("requestedClaims", requestedClaims);
    return scriptVariables;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ScriptObject(org.forgerock.openam.scripting.ScriptObject) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Aggregations

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4