Search in sources :

Example 11 with ScriptException

use of javax.script.ScriptException in project hazelcast by hazelcast.

the class ScriptExecutorOperation method run.

@Override
public void run() throws Exception {
    ScriptEngineManager scriptEngineManager = ScriptEngineManagerContext.getScriptEngineManager();
    ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
    if (engine == null) {
        throw new IllegalArgumentException("Could not find ScriptEngine named '" + engineName + "'.");
    }
    engine.put("hazelcast", getNodeEngine().getHazelcastInstance());
    if (bindings != null) {
        Set<Map.Entry<String, Object>> entries = bindings.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            engine.put(entry.getKey(), entry.getValue());
        }
    }
    try {
        this.result = engine.eval(script);
    } catch (ScriptException e) {
        this.result = e.getMessage();
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) HashMap(java.util.HashMap) ScriptEngine(javax.script.ScriptEngine)

Example 12 with ScriptException

use of javax.script.ScriptException in project hazelcast by hazelcast.

the class ScriptCallable method call.

@Override
public Object call() {
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine e = scriptEngineManager.getEngineByName("javascript");
    if (map != null) {
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            e.put(entry.getKey(), entry.getValue());
        }
    }
    e.put("hazelcast", hazelcastInstance);
    try {
        // for new JavaScript engine called Nashorn we need the compatibility script
        if (e.getFactory().getEngineName().toLowerCase().contains("nashorn")) {
            e.eval("load('nashorn:mozilla_compat.js');");
        }
        e.eval("importPackage(java.lang);");
        e.eval("importPackage(java.util);");
        e.eval("importPackage(com.hazelcast.core);");
        e.eval("importPackage(com.hazelcast.config);");
        e.eval("importPackage(java.util.concurrent);");
        e.eval("importPackage(org.junit);");
        return e.eval(script);
    } catch (ScriptException e1) {
        throw new RuntimeException(e1);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 13 with ScriptException

use of javax.script.ScriptException in project hazelcast by hazelcast.

the class ScriptRunnable method run.

@Override
public void run() {
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine e = scriptEngineManager.getEngineByName("javascript");
    if (map != null) {
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            e.put(entry.getKey(), entry.getValue());
        }
    }
    e.put("hazelcast", hazelcastInstance);
    try {
        // for new JavaScript engine called Nashorn we need the compatibility script
        if (e.getFactory().getEngineName().toLowerCase().contains("nashorn")) {
            e.eval("load('nashorn:mozilla_compat.js');");
        }
        e.eval("importPackage(java.lang);");
        e.eval("importPackage(java.util);");
        e.eval("importPackage(com.hazelcast.core);");
        e.eval("importPackage(com.hazelcast.config);");
        e.eval("importPackage(java.util.concurrent);");
        e.eval("importPackage(org.junit);");
        e.eval(script);
    } catch (ScriptException e1) {
        throw new RuntimeException(e1);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 14 with ScriptException

use of javax.script.ScriptException in project groovy-core by groovy.

the class GroovyScriptEngineImpl method eval.

public Object eval(String script, ScriptContext ctx) throws ScriptException {
    try {
        String val = (String) ctx.getAttribute("#jsr223.groovy.engine.keep.globals", ScriptContext.ENGINE_SCOPE);
        ReferenceBundle bundle = ReferenceBundle.getHardBundle();
        if (val != null && val.length() > 0) {
            if (val.equalsIgnoreCase("soft")) {
                bundle = ReferenceBundle.getSoftBundle();
            } else if (val.equalsIgnoreCase("weak")) {
                bundle = ReferenceBundle.getWeakBundle();
            } else if (val.equalsIgnoreCase("phantom")) {
                bundle = ReferenceBundle.getPhantomBundle();
            }
        }
        globalClosures.setBundle(bundle);
    } catch (ClassCastException cce) {
    /*ignore.*/
    }
    try {
        Class clazz = getScriptClass(script);
        if (clazz == null)
            throw new ScriptException("Script class is null");
        return eval(clazz, ctx);
    } catch (SyntaxException e) {
        throw new ScriptException(e.getMessage(), e.getSourceLocator(), e.getLine());
    } catch (Exception e) {
        if (debug)
            e.printStackTrace();
        throw new ScriptException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ReferenceBundle(org.codehaus.groovy.util.ReferenceBundle) MetaClass(groovy.lang.MetaClass) Class(java.lang.Class) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) String(java.lang.String) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 15 with ScriptException

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

the class Scripted method process.

/**
     * {@inheritDoc}
     */
@Override
public int process(Callback[] callbacks, int state) throws LoginException {
    switch(state) {
        case ISAuthConstants.LOGIN_START:
            substituteUIStrings();
            return STATE_RUN_SCRIPT;
        case STATE_RUN_SCRIPT:
            Bindings scriptVariables = new SimpleBindings();
            scriptVariables.put(REQUEST_DATA_VARIABLE_NAME, getScriptHttpRequestWrapper());
            String clientScriptOutputData = getClientScriptOutputData(callbacks);
            scriptVariables.put(CLIENT_SCRIPT_OUTPUT_DATA_VARIABLE_NAME, clientScriptOutputData);
            scriptVariables.put(LOGGER_VARIABLE_NAME, DEBUG);
            scriptVariables.put(STATE_VARIABLE_NAME, state);
            scriptVariables.put(SHARED_STATE, sharedState);
            scriptVariables.put(USERNAME_VARIABLE_NAME, userName);
            scriptVariables.put(SUCCESS_ATTR_NAME, SUCCESS_VALUE);
            scriptVariables.put(FAILED_ATTR_NAME, FAILURE_VALUE);
            scriptVariables.put(HTTP_CLIENT_VARIABLE_NAME, httpClient);
            scriptVariables.put(IDENTITY_REPOSITORY, identityRepository);
            try {
                scriptEvaluator.evaluateScript(getServerSideScript(), scriptVariables);
            } catch (ScriptException e) {
                DEBUG.message("Error running server side scripts", e);
                throw new AuthLoginException("Error running script", e);
            }
            state = ((Number) scriptVariables.get(STATE_VARIABLE_NAME)).intValue();
            userName = (String) scriptVariables.get(USERNAME_VARIABLE_NAME);
            sharedState.put(CLIENT_SCRIPT_OUTPUT_DATA_VARIABLE_NAME, clientScriptOutputData);
            if (state != SUCCESS_VALUE) {
                throw new AuthLoginException("Authentication failed");
            }
            return state;
        default:
            throw new AuthLoginException("Invalid state");
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Aggregations

ScriptException (javax.script.ScriptException)98 IOException (java.io.IOException)41 ScriptEngine (javax.script.ScriptEngine)36 Bindings (javax.script.Bindings)27 ScriptEngineManager (javax.script.ScriptEngineManager)17 CompiledScript (javax.script.CompiledScript)12 InputStreamReader (java.io.InputStreamReader)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)10 Map (java.util.Map)9 SimpleBindings (javax.script.SimpleBindings)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 Writer (java.io.Writer)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)4