use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class FileSystemApiServer method start.
@Override
public void start() throws ServerStartupException {
long startTime = System.currentTimeMillis();
// check under tenant
File keysFile = null;
if (tenantHome != null) {
keysFile = new File(tenantHome, ConfigKeys.RootKeysFile);
}
if (keysFile == null || !keysFile.isFile() || !keysFile.exists()) {
// check under the runtime home
keysFile = new File(runtimeHome, ConfigKeys.RootKeysFile);
}
if (!keysFile.exists()) {
// check under the install home
keysFile = new File(installHome, ConfigKeys.RootKeysFile);
}
ClassLoader bluenimbleClassLoader = FileSystemApiServer.class.getClassLoader();
try {
boolean rootKeysEncrypted = Json.getBoolean(descriptor, ConfigKeys.RootKeysEncrypted, true);
// exit if not found
if (!keysFile.exists()) {
// create
keys = JsonKeyPair.create();
((JsonKeyPair) keys).store(new File(installHome, ConfigKeys.RootKeysFile), rootKeysEncrypted);
}
setup();
if (keys == null) {
keys = new JsonKeyPair(keysFile, rootKeysEncrypted);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
FileSystemApiServer.this.stop();
}
});
// messages
File mFile = new File(installHome, ConfigKeys.DefaultMessages);
if (mFile.exists()) {
messages.putAll(Json.load(mFile));
}
pluginsRegistry = (PluginsRegistry) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.PluginsRegistry));
pluginsRegistry.init(this, new File(installHome, ConfigKeys.Folders.Plugins));
// init tracer
JsonObject oTracer = Json.getObject(descriptor, ConfigKeys.Tracer);
if (!Json.isNullOrEmpty(oTracer)) {
tracer = (Tracer) BeanUtils.create(bluenimbleClassLoader, oTracer, pluginsRegistry);
}
if (tracer != null) {
tracer.onInstall(this);
} else {
tracer = NoTracing.Instance;
}
tracer.log(Tracer.Level.Info, "Instance Config:\n{0}", descriptor);
mapProvider = (MapProvider) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.MapProvider), pluginsRegistry);
if (mapProvider == null) {
mapProvider = new DefaultMapProvider();
}
ClusterPeerFactory clusterPeerFactory = (ClusterPeerFactory) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.ClusterPeerFactory), pluginsRegistry);
if (clusterPeerFactory == null) {
clusterPeerFactory = new DefaultClusterPeerFactory();
}
peer = clusterPeerFactory.create();
interceptor = (ApiInterceptor) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.Interceptor), pluginsRegistry);
if (interceptor == null) {
interceptor = new DefaultApiInterceptor();
}
interceptor.init(this);
serviceValidator = (ApiServiceValidator) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.ServiceValidator), pluginsRegistry);
if (serviceValidator == null) {
serviceValidator = new DefaultApiServiceValidator();
}
requestSigner = (ApiRequestSigner) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.RequestSigner), pluginsRegistry);
if (requestSigner == null) {
requestSigner = new DefaultApiRequestSigner();
}
requestVisitor = (ApiRequestVisitor) BeanUtils.create(bluenimbleClassLoader, Json.getObject(descriptor, ConfigKeys.RequestVisitor), pluginsRegistry);
if (requestVisitor == null) {
requestVisitor = new DefaultApiRequestVisitor();
}
pluginsRegistry.start();
} catch (Throwable th) {
throw new ServerStartupException(th.getMessage(), th);
}
installSpaces();
if (keyStoreManager != null) {
keyStoreManager.start();
}
tracer.log(Tracer.Level.Info, "Instance started @ {0} - in {1} Millis", new Date(), String.valueOf(System.currentTimeMillis() - startTime));
tracer.log(Tracer.Level.Info, "With Root Keys {0} [{1}]", keys.accessKey(), keys.expiryDate() == null ? "Never Expires" : Lang.toString(keys.expiryDate(), Lang.DEFAULT_DATE_FORMAT));
if (!failed.isEmpty()) {
Iterator<String> names = failed.keySet().iterator();
while (names.hasNext()) {
String name = names.next();
printFailed(name, failed.get(name));
}
}
// clear failed exceptions
failed.clear();
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DefaultPluginsRegistry method create.
private Plugin create(String pluginName, File pluginFile, File pluginHome, long timestamp, JsonObject descriptor) throws Exception {
PluginClassLoader pcl = new PluginClassLoader(pluginName, InstallUtils.toUrls(pluginHome, Json.getArray(descriptor, ConfigKeys.Classpath)));
Plugin plugin = (Plugin) BeanUtils.create(pcl, Json.getObject(descriptor, ConfigKeys.Spi), this);
if (plugin == null) {
plugin = new MockPlugin();
}
if (!(plugin instanceof MockPlugin)) {
pcl.setPlugin(plugin);
}
plugin.setName(pluginName);
plugin.setHome(pluginHome);
plugin.setTitle(Json.getString(descriptor, ConfigKeys.Title));
plugin.setDescription(Json.getString(descriptor, ConfigKeys.Description));
System.setProperty(PluginsHomePrefix + plugin.getName() + PluginsHomePostfix, pluginHome.getAbsolutePath());
classLoaders.put(pluginName, pcl);
// init tracer
Tracer plTracer = null;
JsonObject oTracer = Json.getObject(descriptor, ConfigKeys.Tracer);
if (!Json.isNullOrEmpty(oTracer)) {
plTracer = (Tracer) BeanUtils.create(pcl, oTracer, this);
}
if (plTracer == null) {
plTracer = server.tracer();
} else {
plTracer.onInstall(plugin);
}
plugin.setTracer(plTracer);
return plugin;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class AbstractDateValidator method validate.
@Override
public Object validate(Api api, ApiConsumer consumer, ApiRequest request, DefaultApiServiceValidator validator, String name, String label, JsonObject spec, Object value) {
JsonObject message = isRequired(validator, api, request.getLang(), label, spec, value);
if (message != null) {
return message;
}
if (value == null) {
return null;
}
Date date = null;
if (Date.class.isAssignableFrom(value.getClass())) {
date = (Date) value;
}
if (date != null) {
return null;
}
String format = Json.getString(spec, Spec.Format);
if (Lang.isNullOrEmpty(format)) {
format = getDefaultFormat();
}
String tz = Json.getString(spec, Spec.TimeZone);
try {
date = Lang.toDate(String.valueOf(value), format, tz == null ? Lang.UTC_TZ : TimeZone.getTimeZone(tz));
} catch (ParseException nfex) {
}
if (date == null) {
return ValidationUtils.feedback(null, spec, Spec.Type, validator.getMessage(api, request.getLang(), TypeMessage, label, format));
}
JsonObject feedback = null;
String sMin = Json.getString(spec, Spec.Min);
if (!Lang.isNullOrEmpty(sMin)) {
Date min = null;
try {
min = Lang.toDate(sMin, format);
} catch (ParseException nfex) {
}
if (min != null && date.getTime() < min.getTime()) {
feedback = ValidationUtils.feedback(feedback, spec, Spec.Min, validator.getMessage(api, request.getLang(), MinMessage, label, sMin));
}
}
String sMax = Json.getString(spec, Spec.Max);
if (!Lang.isNullOrEmpty(sMax)) {
Date max = null;
try {
max = Lang.toDate(sMax, format);
} catch (ParseException nfex) {
}
if (max != null && date.getTime() > max.getTime()) {
feedback = ValidationUtils.feedback(feedback, spec, Spec.Max, validator.getMessage(api, request.getLang(), MaxMessage, label, sMax));
}
}
if (feedback == null) {
return date;
}
return feedback;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DefaultApiServiceValidator method valueOf.
public Object valueOf(String name, JsonObject fieldSpec, ApiRequest request, ApiConsumer consumer, Map<String, Object> data) {
if (data != null) {
return data.get(name);
}
Object value = null;
String s = fieldSpec.getString(Spec.Scope);
if (Lang.isNullOrEmpty(s)) {
s = DefaultScope;
}
Object defaultValue = fieldSpec.get(Spec.Value);
Scope scope = null;
s = s.trim();
for (int i = 0; i < s.length(); i++) {
char sc = s.charAt(i);
if (ConsumerScope == sc) {
if (defaultValue != null) {
value = consumer.get(defaultValue.toString());
}
if (value != null) {
request.set(name, value);
}
continue;
}
scope = Scopes.get(String.valueOf(sc));
if (scope == null) {
continue;
}
value = request.get(name, scope);
if (value != null) {
break;
}
}
if (value == null) {
value = defaultValue;
if (value != null) {
request.set(name, value);
}
}
return value;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class HttpApiResponse method error.
@Override
public ApiResponse error(Status status, Object message) {
if (this.status == null || (this.status != null && this.status.getCode() < ApiResponse.BAD_REQUEST.getCode())) {
this.status = status;
}
error = new JsonObject();
error.set(ApiRequest.Fields.Node.class.getSimpleName().toLowerCase(), node);
error.set(ApiRequest.Fields.Id, id);
error.set(Error.Code, this.status.getCode());
if (message != null && (message instanceof Object[])) {
Object[] aMessage = (Object[]) message;
error.set(Error.Message, aMessage[0]);
error.set(Error.Trace, aMessage[1]);
} else {
error.set(Error.Message, message);
}
return this;
}
Aggregations