Search in sources :

Example 1 with ApiSpaceImpl

use of com.bluenimble.platform.api.impls.ApiSpaceImpl in project serverless by bluenimble.

the class FileSystemApiServer method create.

private ApiSpace create(JsonObject oSpace, boolean save) throws ApiManagementException {
    String spaceNs = Json.getString(oSpace, Spec.Namespace);
    if (Lang.isNullOrEmpty(spaceNs)) {
        throw new ApiManagementException("space namespace not found");
    }
    if (!InstallUtils.isValidSpaceNs(spaceNs)) {
        throw new ApiManagementException("invalid space namespace '" + spaceNs + "'");
    }
    File spacesHome = new File(runtimeHome, ConfigKeys.Folders.Spaces);
    File spaceHome = new File(spacesHome, spaceNs);
    if (!spaceHome.exists()) {
        spaceHome.mkdir();
    }
    ApiSpace space = null;
    try {
        space = new ApiSpaceImpl(this, oSpace, spaceHome);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    addSpace(space);
    if (save) {
        try {
            Json.store(oSpace, new File(spaceHome, ConfigKeys.Descriptor.Space));
        } catch (Exception ex) {
            throw new ApiManagementException(ex.getMessage(), ex);
        }
    }
    // notify space creation
    try {
        if (space.isStarted()) {
            getPluginsRegistry().onEvent(Event.Create, space);
        }
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    return space;
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) File(java.io.File) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) IOException(java.io.IOException)

Example 2 with ApiSpaceImpl

use of com.bluenimble.platform.api.impls.ApiSpaceImpl in project serverless by bluenimble.

the class FileSystemApiServer method stop.

@Override
public void stop() {
    tracer.log(Tracer.Level.Info, "Shutting down BlueNimble Node");
    Collection<ApiSpace> spaces = spaces();
    if (spaces != null) {
        for (ApiSpace space : spaces) {
            ((ApiSpaceImpl) space).shutdown();
        }
    }
    if (pluginsRegistry != null) {
        pluginsRegistry.shutdown();
    }
    if (keyStoreManager != null) {
        keyStoreManager.stop();
    }
    tracer.onShutdown(this);
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl)

Example 3 with ApiSpaceImpl

use of com.bluenimble.platform.api.impls.ApiSpaceImpl 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)

Example 4 with ApiSpaceImpl

use of com.bluenimble.platform.api.impls.ApiSpaceImpl 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);
    }
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) IOException(java.io.IOException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Aggregations

ApiSpaceImpl (com.bluenimble.platform.api.impls.ApiSpaceImpl)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 ApiSpace (com.bluenimble.platform.api.ApiSpace)3 IOException (java.io.IOException)3 ServerStartupException (com.bluenimble.platform.server.ServerStartupException)2 File (java.io.File)2 Api (com.bluenimble.platform.api.Api)1 ApiFileStreamSource (com.bluenimble.platform.api.impls.ApiFileStreamSource)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 FileFilter (java.io.FileFilter)1