Search in sources :

Example 1 with ServerStartupException

use of com.bluenimble.platform.server.ServerStartupException 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();
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) DefaultApiRequestVisitor(com.bluenimble.platform.server.impls.DefaultApiRequestVisitor) Date(java.util.Date) DefaultClusterPeerFactory(com.bluenimble.platform.cluster.impls.DefaultClusterPeerFactory) DefaultApiServiceValidator(com.bluenimble.platform.validation.impls.DefaultApiServiceValidator) JsonKeyPair(com.bluenimble.platform.security.impls.JsonKeyPair) ClusterPeerFactory(com.bluenimble.platform.cluster.ClusterPeerFactory) DefaultClusterPeerFactory(com.bluenimble.platform.cluster.impls.DefaultClusterPeerFactory) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) DefaultMapProvider(com.bluenimble.platform.server.maps.impls.DefaultMapProvider) DefaultApiInterceptor(com.bluenimble.platform.server.interceptor.impls.DefaultApiInterceptor) DefaultApiRequestSigner(com.bluenimble.platform.server.security.impls.DefaultApiRequestSigner) File(java.io.File)

Example 2 with ServerStartupException

use of com.bluenimble.platform.server.ServerStartupException in project serverless by bluenimble.

the class FileSystemApiServer method installSpace.

private void installSpace(File spaceHome) throws ServerStartupException {
    tracer.log(Tracer.Level.Info, "Install Space {0}", spaceHome.getName());
    JsonObject oSpace = null;
    File fDescriptor = new File(spaceHome, ConfigKeys.Descriptor.Space);
    if (fDescriptor.exists()) {
        try {
            oSpace = resolve(Json.load(fDescriptor));
        } catch (Exception ex) {
            failed.put("unnable to load space '" + spaceHome.getName() + "'", ex);
            return;
        }
    }
    if (oSpace == null) {
        throw new ServerStartupException("space descriptor for " + spaceHome.getName() + " not found");
    }
    if (!oSpace.containsKey(ApiSpace.Spec.Namespace)) {
        oSpace.set(ApiSpace.Spec.Namespace, spaceHome.getName());
    }
    ApiSpaceImpl space;
    try {
        space = (ApiSpaceImpl) create(oSpace, false);
    // save space descriptor, change maybe made by plugins onEvent/Create
    // Json.store (oSpace, fDescriptor);
    } catch (Exception ex) {
        throw new ServerStartupException(ex.getMessage(), ex);
    }
    File[] apis = spaceHome.listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            return file.isDirectory() || (file.isFile() && file.getAbsolutePath().endsWith(ConfigKeys.ApiExt));
        }
    });
    if (apis == null || apis.length == 0) {
        tracer.log(Tracer.Level.Info, "\tno apis found in space [{0}]", spaceHome.getName());
        return;
    }
    tracer.log(Tracer.Level.Info, "\tfound ({0}) Api(s) in [{1}]", apis.length, spaceHome.getName());
    for (File aFile : apis) {
        Api api = null;
        if (aFile.isDirectory()) {
            try {
                api = space.install(aFile);
            } catch (Exception ex) {
                failed.put(space.getNamespace() + " > " + aFile.getName(), ex);
                continue;
            }
        } else if (aFile.isFile()) {
            ApiFileStreamSource is = new ApiFileStreamSource(aFile, ConfigKeys.ApiExt);
            try {
                api = space.install(is);
            } catch (Exception ex) {
                failed.put(space.getNamespace() + " > " + aFile.getName(), ex);
                continue;
            } finally {
                IOUtils.closeQuietly(is.stream());
            }
            try {
                FileUtils.delete(aFile);
            } catch (IOException ex) {
                tracer.log(Tracer.Level.Error, "\tcan't delete api file {0} / {1} > ", spaceHome.getName(), aFile.getName());
            }
        }
        if (api != null && ApiStatus.Failed.equals(api.status())) {
            failed.put(space.getNamespace() + "/" + aFile.getName(), api.getFailure());
        }
    }
}
Also used : ApiFileStreamSource(com.bluenimble.platform.api.impls.ApiFileStreamSource) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) JsonObject(com.bluenimble.platform.json.JsonObject) Api(com.bluenimble.platform.api.Api) IOException(java.io.IOException) FileFilter(java.io.FileFilter) File(java.io.File) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) IOException(java.io.IOException)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)2 ServerStartupException (com.bluenimble.platform.server.ServerStartupException)2 File (java.io.File)2 Api (com.bluenimble.platform.api.Api)1 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)1 ApiFileStreamSource (com.bluenimble.platform.api.impls.ApiFileStreamSource)1 ApiSpaceImpl (com.bluenimble.platform.api.impls.ApiSpaceImpl)1 ClusterPeerFactory (com.bluenimble.platform.cluster.ClusterPeerFactory)1 DefaultClusterPeerFactory (com.bluenimble.platform.cluster.impls.DefaultClusterPeerFactory)1 JsonKeyPair (com.bluenimble.platform.security.impls.JsonKeyPair)1 DefaultApiRequestVisitor (com.bluenimble.platform.server.impls.DefaultApiRequestVisitor)1 DefaultApiInterceptor (com.bluenimble.platform.server.interceptor.impls.DefaultApiInterceptor)1 DefaultMapProvider (com.bluenimble.platform.server.maps.impls.DefaultMapProvider)1 DefaultApiRequestSigner (com.bluenimble.platform.server.security.impls.DefaultApiRequestSigner)1 DefaultApiServiceValidator (com.bluenimble.platform.validation.impls.DefaultApiServiceValidator)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Date (java.util.Date)1