Search in sources :

Example 6 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class ScriptableApiSpi method afterExecute.

@Override
public void afterExecute(Api api, ApiConsumer consumer, ApiService service, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.AfterExecute)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
    }
    try {
        // invoke afterExecute
        engine.invoke(spi, Functions.AfterExecute, jsApi, consumer, service, request, response);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 7 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class ScriptableApiSpi method onRequest.

@Override
public void onRequest(Api api, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.OnRequest)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
    }
    // invoke onRequest
    try {
        engine.invoke(spi, Functions.OnRequest, jsApi, request, response);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 8 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class DefaultScriptingEngine method eval.

@Override
public Object eval(Supported supported, final Api api, ApiResource resource, ScriptContext sContext) throws ScriptingEngineException {
    if (supported == null) {
        throw new ScriptingEngineException("Unsupported Scripting Engine ");
    }
    ScriptEngine engine = engines.get(supported);
    if (engine == null) {
        throw new ScriptingEngineException("Unsupported Scripting Engine " + supported);
    }
    // get platform libs
    ScriptObjectMirror libs = (ScriptObjectMirror) platform.get(Libs);
    String[] libsKeys = libs.getOwnKeys(true);
    CachedScript cached = scripts.get(resource.owner() + Lang.COLON + resource.path());
    Reader reader = null;
    if (cached == null || cached.timestamp < resource.timestamp().getTime()) {
        InputStream rio = null;
        try {
            StringBuilder startScript = new StringBuilder(ScriptStart);
            // add platform libraries
            for (String lib : libsKeys) {
                startScript.append(Var).append(Lang.SPACE).append(lib).append(Lang.EQUALS).append(Libs).append(Lang.DOT).append(lib).append(Lang.SEMICOLON);
            }
            startScript.append(Native);
            for (String d : Denied) {
                startScript.append(d);
            }
            String sStartScript = startScript.toString();
            startScript.setLength(0);
            rio = resource.toInput();
            // format String script = platform.callMember (, arg);
            List<InputStream> blocks = new ArrayList<InputStream>();
            blocks.add(new ByteArrayInputStream(sStartScript.getBytes()));
            blocks.add(rio);
            blocks.add(new ByteArrayInputStream(ScriptEnd.getBytes()));
            reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(blocks)));
            cached = new CachedScript();
            cached.script = ((Compilable) engine).compile(reader);
            cached.timestamp = resource.timestamp().getTime();
            scripts.put(resource.owner() + Lang.COLON + resource.path(), cached);
        } catch (Exception e) {
            throw new ScriptingEngineException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(rio);
        }
    }
    if (sContext == null) {
        return null;
    }
    Bindings bindings = new SimpleBindings();
    bindings.put(JavaClass, new Function<String, Class<?>>() {

        @Override
        public Class<?> apply(String type) {
            try {
                return api.getClassLoader().loadClass(type);
            } catch (ClassNotFoundException cnfe) {
                throw new RuntimeException(cnfe);
            }
        }
    });
    // add platform libraries
    bindings.put(Libs, libs);
    try {
        Iterator<String> vars = sContext.vars();
        while (vars.hasNext()) {
            String var = vars.next();
            bindings.put(var, sContext.var(var));
        }
        return cached.script.eval(bindings);
    } catch (ScriptException e) {
        throw new ScriptingEngineException(e.getMessage(), e);
    } finally {
        bindings.clear();
    }
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) SequenceInputStream(java.io.SequenceInputStream) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleBindings(javax.script.SimpleBindings)

Example 9 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class DefaultScriptingEngine method invoke.

@Override
public Object invoke(Object scriptable, String function, Object... args) throws ScriptingEngineException {
    if (scriptable == null) {
        scriptable = platform;
    }
    if (!(scriptable instanceof ScriptObjectMirror)) {
        throw new ScriptingEngineException("object is not a valid scriptable object");
    }
    ScriptObjectMirror som = (ScriptObjectMirror) scriptable;
    if (args != null && args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (arg == null || arg instanceof ScriptObjectMirror) {
                continue;
            }
            Class<?> type = arg.getClass();
            Scriptable ann = type.getAnnotation(Scriptable.class);
            if (ann == null) {
                continue;
            }
            Object jsArg = null;
            if (Referenceable.class.isAssignableFrom(arg.getClass())) {
                jsArg = ((Referenceable) arg).getReference();
            }
            if (jsArg == null) {
                jsArg = platform.callMember(ann.name(), arg);
                if (Referenceable.class.isAssignableFrom(arg.getClass())) {
                    ((Referenceable) arg).setReference(jsArg);
                }
            }
            args[i] = jsArg;
        }
    }
    try {
        return som.callMember(function, args);
    } catch (Throwable err) {
        throw new ScriptingEngineException(err.getMessage(), err);
    }
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Referenceable(com.bluenimble.platform.Referenceable) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) Scriptable(com.bluenimble.platform.scripting.Scriptable)

Example 10 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class ScriptableApiServiceSpi method onStart.

@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    SpecAndSpiPair helper = (SpecAndSpiPair) api.getHelper();
    if (helper == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting or couldn't start correctly.");
    }
    Object jsApi = helper.spec();
    if (jsApi == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    JsonObject runtime = service.getRuntime();
    String script = Json.getString(runtime, Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("script not found in " + ApiUtils.RuntimeKey);
    }
    String[] path = Lang.split(script, Lang.SLASH);
    ApiResource rScript = null;
    try {
        rScript = api.getResourcesManager().get(path);
    } catch (ApiResourcesManagerException ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (rScript == null) {
        throw new ApiManagementException("function '" + Lang.join(path, Lang.SLASH) + "' not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    // create the spec
    Object jsService = null;
    try {
        jsService = engine.invoke(null, ApiService.class.getSimpleName(), service);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsService == null) {
        throw new ApiManagementException("can't create 'service spec' js object");
    }
    // create the spi
    Object spi = null;
    try {
        spi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (spi == null) {
        throw new ApiManagementException("script returned an undefined object");
    }
    service.setHelper(new SpecAndSpiPair(jsService, spi));
    if (!engine.has(spi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(spi, Functions.OnStart, jsApi, jsService, context);
    } catch (ScriptingEngineException ex) {
        ex.setScript(script);
        throw new ApiManagementException(ex.getMessage(), ex);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Aggregations

ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)12 JsonObject (com.bluenimble.platform.json.JsonObject)10 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)10 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)7 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)3 ApiResource (com.bluenimble.platform.api.ApiResource)2 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 Referenceable (com.bluenimble.platform.Referenceable)1 ApiOutput (com.bluenimble.platform.api.ApiOutput)1 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 Scriptable (com.bluenimble.platform.scripting.Scriptable)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 SequenceInputStream (java.io.SequenceInputStream)1 ArrayList (java.util.ArrayList)1