use of com.bluenimble.platform.api.ApiManagementException 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);
}
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ScriptableApiSpi method onStop.
@Override
public void onStop(Api api, ApiContext context) throws ApiManagementException {
Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
if (spi == null) {
return;
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
if (!engine.has(spi, Functions.OnStop)) {
return;
}
Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
if (jsApi == null) {
return;
}
// invoke onStop
try {
engine.invoke(spi, Functions.OnStop, jsApi, context);
} catch (Exception ex) {
api.tracer().log(Level.Error, Lang.BLANK, ex);
}
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class FileSystemApiServer method drop.
@Override
public void drop(String spaceNs) throws ApiManagementException {
if (Spaces.Sys.equals(spaceNs)) {
throw new ApiManagementException("access denied");
}
ApiSpace space = space(spaceNs);
if (space == null) {
throw new ApiManagementException("space " + spaceNs + " not found");
}
ApiSpaceImpl spaceImpl = (ApiSpaceImpl) space;
// stop space
spaceImpl.stop();
try {
FileUtils.delete(spaceImpl.home());
} catch (IOException e) {
throw new ApiManagementException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ApiSpaceImpl method addFeature.
@Override
public void addFeature(String name, String feature, String provider, JsonObject spec) throws ApiManagementException {
feature = feature.toLowerCase();
provider = provider.toLowerCase();
tracer.log(Tracer.Level.Info, "add feature [{0}] -> [{1} / provider {2}] ", name, feature, provider);
JsonObject oFeature = Json.getObject(getFeatures(), feature);
if (oFeature == null) {
oFeature = new JsonObject();
getFeatures().set(feature, oFeature);
}
if (oFeature.containsKey(name)) {
throw new ApiManagementException("feature '" + feature + "/" + name + "/" + provider + "' already available for space " + getNamespace());
}
JsonObject uFeature = new JsonObject();
uFeature.set(ApiSpace.Features.Provider, provider).set(ApiSpace.Features.Spec, spec);
oFeature.set(name, uFeature);
try {
server.getPluginsRegistry().onEvent(Event.AddFeature, this);
} catch (PluginRegistryException e) {
throw new ApiManagementException(e.getMessage(), e);
}
// save
save();
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ApiSpaceImpl method install.
/*
@Override
public Api install (ApiStreamSource source) throws ApiManagementException {
boolean started = isStarted ();
// if the space is new, start it before
if (!started) {
// start executor (this is important if the space just created since it will be down)
try {
started = start ();
} catch (Exception e) {
throw new ApiManagementException (e.getMessage (), e);
}
// notify space creation if it's new
if (started) {
try {
server.getPluginsRegistry ().onEvent (Event.Create, this);
} catch (Exception ex) {
throw new ApiManagementException (ex.getMessage (), ex);
}
}
}
if (!started) {
throw new ApiManagementException ("Can't install Api, couldn't start owner space");
}
return _install (source);
}
*/
@Override
public Api install(ApiStreamSource source) throws ApiManagementException {
if (source == null) {
throw new ApiManagementException("missing api payload");
}
InputStream stream = source.stream();
if (stream == null) {
throw new ApiManagementException("missing api stream");
}
String name = source.name();
if (Lang.isNullOrEmpty(name)) {
name = getNamespace() + Lang.UNDERSCORE + Lang.UUID(40);
}
tracer.log(Tracer.Level.Info, "install api {0} / {1}", getNamespace(), source.name());
File apiHome = new File(home, name);
try {
ArchiveUtils.decompress(stream, apiHome);
} catch (Exception e) {
throw new ApiManagementException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(stream);
}
Api api = install(apiHome);
return api;
}
Aggregations