Search in sources :

Example 6 with PluginRegistryException

use of com.bluenimble.platform.plugins.PluginRegistryException in project serverless by bluenimble.

the class ApiSpaceImpl method stop.

@Override
public void stop(String apiNs) throws ApiManagementException {
    ApiImpl api = (ApiImpl) api(apiNs);
    if (api == null) {
        throw new ApiManagementException("api " + apiNs + " not found");
    }
    if (!ApiStatus.Running.equals(api.status())) {
        throw new ApiManagementException("can't stop api " + apiNs + ". Status=" + api.status());
    }
    // notify plugins
    try {
        server.getPluginsRegistry().onEvent(Event.Stop, api);
    } catch (PluginRegistryException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    // stop api
    api.stop(true);
}
Also used : ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 7 with PluginRegistryException

use of com.bluenimble.platform.plugins.PluginRegistryException in project serverless by bluenimble.

the class ApiSpaceImpl method uninstall.

/*
	@Override
	public Api install (JsonObject descriptor) throws ApiManagementException {
		if (Lang.isNullOrEmpty (getNamespace ())) {
			throw new ApiManagementException (
				"Api " + Api.Spec.Namespace + " not found in descriptor "
			);
		}
		// create api home
		File apiHome = new File (home, getNamespace () + Lang.UNDERSCORE + Lang.UUID (40));
		
		return install (apiHome, descriptor);
	}
	*/
@Override
public void uninstall(String apiNs) throws ApiManagementException {
    if (Lang.isNullOrEmpty(apiNs)) {
        throw new ApiManagementException("api namespace is null or empty");
    }
    ApiImpl api = (ApiImpl) api(apiNs.trim());
    if (api == null) {
        throw new ApiManagementException("api '" + apiNs + "' not found");
    }
    // notify before uninstall
    try {
        server.getPluginsRegistry().onEvent(Event.Uninstall, api);
    } catch (PluginRegistryException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    // stop api
    if (!api.status().equals(ApiStatus.Stopped)) {
        api.stop(false);
    }
    // remove status
    statusManager.delete(api);
    // clear memory
    api.clear();
    try {
        FileUtils.delete(((ApiImpl) api).getHome());
    } catch (IOException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    remove(apiNs);
    tracer.log(Tracer.Level.Info, "\tApi [{0}] uninstalled", api.getNamespace());
}
Also used : IOException(java.io.IOException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 8 with PluginRegistryException

use of com.bluenimble.platform.plugins.PluginRegistryException in project serverless by bluenimble.

the class MemCachedPlugin method createClients.

private void createClients(ApiSpace space) throws PluginRegistryException {
    // create sessions
    JsonObject msgFeature = Json.getObject(space.getFeatures(), feature);
    if (msgFeature == null || msgFeature.isEmpty()) {
        return;
    }
    Iterator<String> keys = msgFeature.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject feature = Json.getObject(msgFeature, key);
        if (!this.getName().equalsIgnoreCase(Json.getString(feature, ApiSpace.Features.Provider))) {
            continue;
        }
        String sessionKey = createKey(key);
        if (space.containsRecyclable(sessionKey)) {
            continue;
        }
        JsonObject spec = Json.getObject(feature, ApiSpace.Features.Spec);
        String[] nodes = Lang.split(Json.getString(spec, Spec.Cluster), Lang.COMMA);
        if (nodes == null) {
            continue;
        }
        final JsonObject oAuth = Json.getObject(spec, Spec.Auth);
        if (oAuth == null || oAuth.isEmpty()) {
            continue;
        }
        final String user = Json.getString(oAuth, Spec.User);
        final String password = Json.getString(oAuth, Spec.Password);
        AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" }, new PlainCallbackHandler(user, password));
        try {
            MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(Arrays.asList(nodes)));
            space.addRecyclable(sessionKey, new RecyclableCacheClient(client));
        } catch (IOException e) {
            throw new PluginRegistryException(e.getMessage(), e);
        }
    }
}
Also used : PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 9 with PluginRegistryException

use of com.bluenimble.platform.plugins.PluginRegistryException in project serverless by bluenimble.

the class DataSourcePlugin method createDataSource.

private DataSource createDataSource(String name, ApiSpace space, JsonObject spec) throws PluginRegistryException {
    String dataSourceKey = dataSourceKey(name, space);
    tracer().log(Tracer.Level.Info, "Create datasource {0}", dataSourceKey);
    if (space.containsRecyclable(dataSourceKey)) {
        return null;
    }
    String sVendor = Json.getString(spec, Spec.Vendor);
    tracer().log(Tracer.Level.Info, "\tDS Vendor {0}", sVendor);
    DataSourceVendor vendor = vendors.get(sVendor);
    if (vendor == null) {
        File vendorHome = new File(home, Vendors + Lang.SLASH + sVendor);
        if (vendorHome.exists()) {
            try {
                vendor = new DataSourceVendor(vendorHome);
            } catch (Exception e) {
                throw new PluginRegistryException(e.getMessage(), e);
            }
            vendors.put(sVendor, vendor);
        }
    }
    tracer().log(Tracer.Level.Info, "\tDS Vendor Instance {0}", vendor);
    if (vendor == null) {
        return null;
    }
    DataSource datasource = null;
    ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(vendor.classLoader());
    try {
        HikariConfig config = new HikariConfig();
        config.setPoolName(dataSourceKey);
        config.setDriverClassName(vendor.driver());
        config.setJdbcUrl(vendor.url(Json.getString(spec, Spec.Host), Json.getInteger(spec, Spec.Port, 0), Json.getString(spec, Spec.Database)));
        config.setUsername(Json.getString(spec, Spec.User));
        config.setPassword(Json.getString(spec, Spec.Password));
        config.setAutoCommit(false);
        config.setMaximumPoolSize(weight);
        JsonObject props = Json.getObject(spec, Spec.Properties);
        if (!Json.isNullOrEmpty(props)) {
            Iterator<String> keys = props.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                config.addDataSourceProperty(key, props.get(key));
            }
        }
        datasource = new HikariDataSource(config);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
    tracer().log(Tracer.Level.Info, "\tSpace DataSource {0}", datasource);
    space.addRecyclable(dataSourceKey, new RecyclableDataSource(datasource));
    space.addRecyclable(factoryKey(name, space), new RecyclableEntityManagerFactory(null));
    return datasource;
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) JsonObject(com.bluenimble.platform.json.JsonObject) HikariConfig(com.zaxxer.hikari.HikariConfig) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException) RemoteDataSource(com.bluenimble.platform.datasource.RemoteDataSource) DataSource(javax.sql.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) File(java.io.File) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 10 with PluginRegistryException

use of com.bluenimble.platform.plugins.PluginRegistryException 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();
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Aggregations

PluginRegistryException (com.bluenimble.platform.plugins.PluginRegistryException)14 JsonObject (com.bluenimble.platform.json.JsonObject)7 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)6 IOException (java.io.IOException)6 Plugin (com.bluenimble.platform.plugins.Plugin)3 File (java.io.File)3 SchedulerException (org.quartz.SchedulerException)2 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)1 Api (com.bluenimble.platform.api.Api)1 ApiStatus (com.bluenimble.platform.api.ApiStatus)1 RemoteDataSource (com.bluenimble.platform.datasource.RemoteDataSource)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 DataSource (javax.sql.DataSource)1 ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)1 MemcachedClient (net.spy.memcached.MemcachedClient)1 AuthDescriptor (net.spy.memcached.auth.AuthDescriptor)1