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);
}
}
}
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);
}
}
}
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);
}
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());
}
}
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();
}
Aggregations