Search in sources :

Example 1 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class SchedulerPlugin method stopJobs.

private void stopJobs(ApiSpace space) throws PluginRegistryException {
    JsonObject schedulerFeature = Json.getObject(space.getFeatures(), feature);
    if (schedulerFeature == null || schedulerFeature.isEmpty()) {
        return;
    }
    Iterator<String> keys = schedulerFeature.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject source = Json.getObject(schedulerFeature, key);
        if (!this.getName().equalsIgnoreCase(Json.getString(source, ApiSpace.Features.Provider))) {
            continue;
        }
        JsonObject spec = Json.getObject(source, ApiSpace.Features.Spec);
        if (spec == null) {
            continue;
        }
        String id = space.getNamespace() + Lang.DASH + key;
        try {
            oScheduler.deleteJob(jobKey(Prefix.Job + id, Prefix.Group + id));
        } catch (SchedulerException e) {
            throw new PluginRegistryException(e.getMessage(), e);
        }
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) JsonObject(com.bluenimble.platform.json.JsonObject) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 2 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class SchedulerPlugin method startJobs.

private void startJobs(ApiSpace space) throws PluginRegistryException {
    JsonObject schedulerFeature = Json.getObject(space.getFeatures(), feature);
    if (schedulerFeature == null || schedulerFeature.isEmpty()) {
        return;
    }
    Iterator<String> keys = schedulerFeature.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject source = Json.getObject(schedulerFeature, key);
        if (!this.getName().equalsIgnoreCase(Json.getString(source, ApiSpace.Features.Provider))) {
            continue;
        }
        JsonObject spec = Json.getObject(source, ApiSpace.Features.Spec);
        if (spec == null) {
            continue;
        }
        String expression = Json.getString(source, Spec.Expression);
        if (Lang.isNullOrEmpty(expression)) {
            continue;
        }
        String api = Json.getString(source, Spec.Api);
        if (Lang.isNullOrEmpty(api)) {
            continue;
        }
        String service = Json.getString(source, Spec.Service);
        if (Lang.isNullOrEmpty(service)) {
            continue;
        }
        String id = space.getNamespace() + Lang.DASH + key;
        JobBuilder builder = newJob(ServiceJob.class).withIdentity(Prefix.Job + id, Prefix.Group + id);
        builder.usingJobData(_Space, space.getNamespace());
        builder.usingJobData(_Api, api);
        builder.usingJobData(_Service, service);
        JsonObject data = Json.getObject(spec, Spec.Data);
        if (!Json.isNullOrEmpty(data)) {
            Iterator<String> dataKeys = data.keys();
            while (dataKeys.hasNext()) {
                String dataKey = dataKeys.next();
                builder.usingJobData(dataKey, String.valueOf(data.get(dataKey)));
            }
        }
        JobDetail job = builder.build();
        String timeZone = Json.getString(source, Spec.TimeZone);
        CronScheduleBuilder csb = cronSchedule(Json.getString(source, Spec.Expression));
        if (!Lang.isNullOrEmpty(timeZone)) {
            csb.inTimeZone(TimeZone.getTimeZone(timeZone));
        }
        Trigger trigger = newTrigger().withIdentity(Prefix.Trigger + id, Prefix.Group + id).withSchedule(csb).forJob(job.getKey()).build();
        try {
            oScheduler.scheduleJob(trigger);
        } catch (SchedulerException e) {
            throw new PluginRegistryException(e.getMessage(), e);
        }
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) SchedulerException(org.quartz.SchedulerException) JobBuilder(org.quartz.JobBuilder) JsonObject(com.bluenimble.platform.json.JsonObject) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 3 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class ScriptableApiServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    SpecAndSpiPair serviceHelper = (SpecAndSpiPair) request.getService().getHelper();
    Object spi = serviceHelper.spi();
    if (spi == null) {
        throw new ApiServiceExecutionException("service spi not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.Execute)) {
        return null;
    }
    // invoke execute
    Object result = null;
    try {
        result = engine.invoke(spi, Functions.Execute, jsApi, consumer, request, response);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(request.getService().getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (result == null || (result instanceof Undefined)) {
        return null;
    }
    if (ApiOutput.class.isAssignableFrom(result.getClass())) {
        return (ApiOutput) result;
    }
    if (ScriptObjectMirror.class.isAssignableFrom(result.getClass())) {
        ScriptObjectMirror som = (ScriptObjectMirror) result;
        Object clazz = som.get(ClassField);
        if (clazz == null) {
            return new ApiSomOutput(som);
        }
        if (clazz.equals(ApiOutputClass)) {
            return (ApiOutput) som.getMember(ProxyField);
        }
    }
    Object converted = Converters.convert(result);
    if (converted instanceof JsonArray) {
        converted = new JsonObject().set(ApiOutput.Defaults.Items, converted);
    }
    if (!(converted instanceof JsonObject)) {
        throw new ApiServiceExecutionException("result should be a valid json object");
    }
    return new JsonApiOutput((JsonObject) converted);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Undefined(jdk.nashorn.internal.runtime.Undefined) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ApiOutput(com.bluenimble.platform.api.ApiOutput) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 4 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class ScriptableApiSpi method onError.

@Override
public void onError(Api api, ApiService service, ApiConsumer consumer, ApiRequest request, ApiResponse response, JsonObject error) {
    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.OnError)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (api == null || jsApi == null) {
        Object msg = error.get(ApiResponse.Error.Message);
        if (msg != null) {
            msg += Lang.ENDLN + "api or spi not attached on Api OnStart";
        } else {
            msg = Lang.ENDLN + "api or spi not attached on Api OnStart";
        }
        api.tracer().log(Level.Error, msg);
        error.set(ApiResponse.Error.Message, msg);
        return;
    }
    // invoke onError
    try {
        engine.invoke(spi, Functions.OnError, jsApi, service, consumer, request, response, error);
    } catch (ScriptingEngineException ex) {
        api.tracer().log(Level.Error, Lang.BLANK, ex);
        error.set(ApiResponse.Error.Message, ex.getMessage());
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 5 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class Converters method toObject.

private static JsonObject toObject(ScriptObjectMirror som) {
    JsonObject json = new JsonObject();
    if (som.isEmpty()) {
        return json;
    }
    String[] keys = som.getOwnKeys(true);
    for (String k : keys) {
        json.set(k, convert(som.get(k)));
    }
    return json.resolve();
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)230 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)40 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonArray (com.bluenimble.platform.json.JsonArray)37 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)34 Database (com.bluenimble.platform.db.Database)29 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 File (java.io.File)25 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)23 Map (java.util.Map)22 IOException (java.io.IOException)20 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)17 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 InputStream (java.io.InputStream)14 Date (java.util.Date)14 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)13 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)12 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)11 HashMap (java.util.HashMap)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)9