use of com.bluenimble.platform.Recyclable in project serverless by bluenimble.
the class ApiSpaceImpl method feature.
@SuppressWarnings("unchecked")
@Override
public <T> T feature(Class<T> t, String name, ApiContext context) {
if (context == null) {
throw new FeatureNotFoundException("context not available");
}
if (Lang.isNullOrEmpty(name)) {
throw new FeatureNotFoundException("provider can't be null or empty");
}
Feature aFeature = t.getAnnotation(Feature.class);
if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
throw new FeatureNotFoundException("feature " + t.getSimpleName() + " not registered in this instance");
}
String recyclableKey = contextRecyclableKey(aFeature.name(), name);
Recyclable feature = context.getRecyclable(recyclableKey);
if (feature != null) {
return (T) feature;
}
T f = (T) server.getFeature(this, t, name);
if (f == null) {
throw new FeatureNotFoundException("feature " + name + " not found");
}
if (Recyclable.class.isAssignableFrom(f.getClass())) {
context.addRecyclable(recyclableKey, (Recyclable) f);
}
return f;
}
use of com.bluenimble.platform.Recyclable in project serverless by bluenimble.
the class ApiImpl method start.
public void start(boolean pause) {
// create classLoader
try {
if (new File(home, ConfigKeys.Folders.Lib).exists() || Json.getArray(descriptor, ConfigKeys.Classpath) != null) {
this.classLoader = new ApiClassLoader(space.getNamespace() + Lang.DOT + getNamespace(), InstallUtils.toUrls(home, Json.getArray(descriptor, ConfigKeys.Classpath)));
}
} catch (Exception ex) {
failed(ex);
}
// start resources manager
try {
resourcesManager.onStart();
} catch (Exception ex) {
failed(ex);
}
// load messages
try {
loadI18n();
} catch (Exception ex) {
failed(ex);
}
// create spi
try {
spi = (ApiSpi) BeanUtils.create(this.getClassLoader(), Json.getObject(descriptor, ConfigKeys.Spi), space.getServer().getPluginsRegistry());
} catch (Exception ex) {
failed(ex);
}
if (spi == null) {
spi = DefaultApiSpi;
}
space.tracer().log(Tracer.Level.Info, "\t Namespace: {0}", getNamespace());
space.tracer().log(Tracer.Level.Info, "\t Name: {0}", getName());
space.tracer().log(Tracer.Level.Info, "\tDescription: {0}", getDescription());
// init tracer
JsonObject oTracer = Json.getObject(descriptor, ConfigKeys.Tracer);
if (!Json.isNullOrEmpty(oTracer)) {
try {
tracer = (Tracer) BeanUtils.create(this.getClassLoader(), oTracer, space.getServer().getPluginsRegistry());
} catch (Exception ex) {
failed(ex);
}
}
tracer.onInstall(this);
ApiContext context = new DefaultApiContext();
try {
// start spi
try {
// initialize any linked datasource
JsonArray datasources = Json.getArray(getRuntime(), DataSources);
if (datasources != null && !datasources.isEmpty()) {
// change classloader
for (int i = 0; i < datasources.count(); i++) {
Recyclable recyclable = space.getRecyclable(RemoteDataSource.class.getAnnotation(Feature.class).name() + Lang.DOT + space.getNamespace() + Lang.DOT + (String) datasources.get(i));
if (recyclable == null) {
continue;
}
// create factory
recyclable.set(space, this.getClassLoader(), (String) datasources.get(i));
}
}
// start api
spi.onStart(this, context);
} catch (Exception ex) {
failed(ex);
}
// start services manager
try {
servicesManager.onStart(context);
} catch (Exception ex) {
failed(ex);
}
} finally {
context.recycle();
}
// update/save status
if (!ApiStatus.Failed.equals(status)) {
setStatus(pause ? ApiStatus.Paused : ApiStatus.Running, true);
if (Lang.isDebugMode()) {
final StringBuilder sb = new StringBuilder(space.getNamespace() + Lang.SLASH + getNamespace() + " available services\n");
servicesManager.list(new Selector() {
@Override
public boolean select(ApiService service) {
sb.append(Lang.TAB).append(Lang.PARENTH_OPEN).append(service.status().name().substring(0, 1)).append(Lang.PARENTH_CLOSE).append(Lang.SPACE).append(service.getVerb()).append(Lang.COLON).append(Json.getString(service.toJson(), ApiService.Spec.Endpoint));
if (ApiStatus.Failed.equals(service.status())) {
sb.append(Lang.ENDLN).append(service.getFailure().toString(2));
}
sb.append(Lang.ENDLN);
return false;
}
});
space.tracer().log(Tracer.Level.Info, sb);
sb.setLength(0);
}
}
space.tracer().log(Tracer.Level.Info, "\tapi {0} {1}", getNamespace(), status);
}
Aggregations