Search in sources :

Example 1 with StartService

use of in.erail.glue.annotation.StartService in project api-framework by vinscom.

the class RateLimiterProcessor method start.

@StartService
public void start() {
    for (BridgeEventType type : BridgeEventType.values()) {
        Cache<String, Bucket> cache = CacheBuilder.newBuilder().recordStats().expireAfterAccess(getExpireAfterAccess(), TimeUnit.SECONDS).maximumSize(getMaximumSize()).build();
        getCache().put(type, cache);
    }
}
Also used : Bucket(io.github.bucket4j.Bucket) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) StartService(in.erail.glue.annotation.StartService)

Example 2 with StartService

use of in.erail.glue.annotation.StartService in project api-framework by vinscom.

the class VerticalDeployer method start.

@StartService
public void start() {
    // Deploy using name
    Arrays.stream(getVerticalNames()).forEach((path) -> {
        JsonObject deployMsg = new JsonObject();
        deployMsg.put("name", path);
        getVertx().getDelegate().deployVerticle(path, getDeploymentOptions(), (result) -> {
            if (result.succeeded()) {
                String deploymentId = result.result();
                deployMsg.put("deploymentId", deploymentId);
                deployMsg.put("success", true);
                getVertx().eventBus().publish(getDeployStatusTopicName(), deployMsg);
                getLog().info(() -> "Deployed:" + path + ":" + deploymentId);
            } else {
                deployMsg.put("success", false);
                getVertx().eventBus().publish(getDeployStatusTopicName(), deployMsg);
                getLog().error("Vertical Deployment failed", result.cause());
            }
        });
    });
    // Deploy using Java class instance
    getVerticalInstances().getServices().stream().forEach((vertical) -> {
        JsonObject deployMsg = new JsonObject();
        deployMsg.put("name", vertical.getClass().getCanonicalName());
        getVertx().getDelegate().deployVerticle((Verticle) vertical, getDeploymentOptions(), (result) -> {
            if (result.succeeded()) {
                String deploymentId = result.result();
                deployMsg.put("deploymentId", deploymentId);
                deployMsg.put("success", true);
                getVertx().eventBus().publish(getDeployStatusTopicName(), deployMsg);
                getLog().info(() -> "Deployed:" + vertical.getClass().getCanonicalName() + ":" + deploymentId);
            } else {
                deployMsg.put("success", false);
                getVertx().eventBus().publish(getDeployStatusTopicName(), deployMsg);
                getLog().error("Vertical Deployment failed", result.cause());
            }
        });
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) StartService(in.erail.glue.annotation.StartService)

Example 3 with StartService

use of in.erail.glue.annotation.StartService in project api-framework by vinscom.

the class SecurityTools method startup.

@SuppressWarnings("unchecked")
@StartService
public void startup() {
    setRandom(new SecureRandom());
    if (!getVertx().isClustered()) {
        mGlobalUniqueString.complete("A" + mRandom.nextInt());
        return;
    }
    Map<String, Object> cryptCtx = new HashMap<>();
    Single<Lock> lock = getVertx().sharedData().rxGetLockWithTimeout("_in.erail.security", 5000);
    getVertx().sharedData().<String, byte[]>rxGetClusterWideMap("_in.erail.security").flatMap((m) -> {
        cryptCtx.put("map", m);
        return m.rxGet("key");
    }).map((k) -> {
        cryptCtx.put("key", k);
        return cryptCtx;
    }).flatMap((ctx) -> {
        if (ctx.get("key") == null) {
            return lock.map((l) -> {
                ctx.put("lock", l);
                return ctx;
            });
        }
        return Single.just(ctx);
    }).flatMap(ctx -> {
        if (ctx.get("lock") != null) {
            return ((AsyncMap<String, Object>) (ctx.get("map"))).rxGet("key").map((k) -> {
                ctx.put("key", k);
                return ctx;
            });
        }
        return Single.just(ctx);
    }).flatMap((ctx) -> {
        if (ctx.get("key") == null) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            byte[] key = keygen.generateKey().getEncoded();
            return ((AsyncMap<String, Object>) (ctx.get("map"))).rxPut("key", key).doOnComplete(() -> ctx.put("key", key)).toSingleDefault(ctx);
        }
        return Single.just(ctx);
    }).map(ctx -> (byte[]) ctx.get("key")).doFinally(() -> {
        if (cryptCtx.containsKey("lock")) {
            Lock l = (Lock) cryptCtx.get("lock");
            l.release();
        }
    }).subscribe((key) -> {
        mKeySpec.complete(new SecretKeySpec(key, "AES"));
        String unique = Base64.getEncoder().encodeToString(Arrays.copyOfRange(key, 0, 5));
        mGlobalUniqueString.complete(unique.replace("=", ""));
        getLog().info(() -> String.format("GlobalUniqueString:[%s]", unique));
    });
}
Also used : Arrays(java.util.Arrays) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncMap(io.vertx.reactivex.core.shareddata.AsyncMap) Single(io.reactivex.Single) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) ExecutionException(java.util.concurrent.ExecutionException) Vertx(io.vertx.reactivex.core.Vertx) Base64(java.util.Base64) BadPaddingException(javax.crypto.BadPaddingException) Logger(org.apache.logging.log4j.Logger) StartService(in.erail.glue.annotation.StartService) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Map(java.util.Map) InvalidKeyException(java.security.InvalidKeyException) Lock(io.vertx.reactivex.core.shareddata.Lock) HashMap(java.util.HashMap) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Lock(io.vertx.reactivex.core.shareddata.Lock) StartService(in.erail.glue.annotation.StartService)

Example 4 with StartService

use of in.erail.glue.annotation.StartService in project api-framework by vinscom.

the class Server method start.

@StartService
public void start() {
    HttpServer server = getVertx().createHttpServer(new HttpServerOptions().setPort(getPort()).setHost(getHost()));
    Router router = Router.router(getVertx());
    // Logging
    if (getLog().isDebugEnabled()) {
        router.route("/*").handler(LoggerHandler.create());
    }
    if (getSockJSHandler() != null) {
        router.route("/eventbus/*").handler(getSockJSHandler());
    }
    for (int i = 0; i < mMountPath.length; i++) {
        router.mountSubRouter(mMountPath[i], mRouter[i]);
    }
    server.requestHandler(router::accept).rxListen().blockingGet();
    getLog().debug(() -> String.format("---------------Server[%s:%s] is ready-----------------", getHost(), getPort()));
}
Also used : HttpServer(io.vertx.reactivex.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.reactivex.ext.web.Router) StartService(in.erail.glue.annotation.StartService)

Example 5 with StartService

use of in.erail.glue.annotation.StartService in project api-framework by vinscom.

the class LoadMongoDataService method start.

@StartService
public void start() {
    if (!isEnable()) {
        return;
    }
    Set<String> collections = mData.fieldNames();
    for (String collection : collections) {
        mMongoClient.rxDropCollection(collection).andThen(mMongoClient.rxCreateCollection(collection)).blockingAwait();
        JsonArray data = mData.getJsonArray(collection);
        Observable.fromIterable(data).map(m -> (JsonObject) m).doOnNext((t) -> {
            mMongoClient.rxSave(collection, t).blockingGet();
        }).blockingSubscribe();
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonArray(io.vertx.core.json.JsonArray) StartService(in.erail.glue.annotation.StartService) Set(java.util.Set) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) StartService(in.erail.glue.annotation.StartService)

Aggregations

StartService (in.erail.glue.annotation.StartService)7 JsonObject (io.vertx.core.json.JsonObject)2 RESTService (in.erail.service.RESTService)1 Bucket (io.github.bucket4j.Bucket)1 Observable (io.reactivex.Observable)1 Single (io.reactivex.Single)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 JsonArray (io.vertx.core.json.JsonArray)1 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)1 SockJSHandlerOptions (io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions)1 Vertx (io.vertx.reactivex.core.Vertx)1 HttpServer (io.vertx.reactivex.core.http.HttpServer)1 AsyncMap (io.vertx.reactivex.core.shareddata.AsyncMap)1 Lock (io.vertx.reactivex.core.shareddata.Lock)1 Router (io.vertx.reactivex.ext.web.Router)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 Arrays (java.util.Arrays)1