All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mstream.trader.commons.config.ConfigModule Maven / Gradle / Ivy

package io.mstream.trader.commons.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import io.mstream.trader.commons.config.model.Version;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.concurrent.ScheduledExecutorService;

import static com.google.inject.Scopes.SINGLETON;
import static io.mstream.trader.commons.utils.Preconditions.checkNotEmpty;
import static java.lang.System.getProperty;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;


public class ConfigModule extends AbstractModule {

    private static final String ZK_CONNECTION_STRING_PROPERTY_NAME =
            "ZK_CONNECTION_STRING";

    private final TypeLiteral configTypeLiteral;
    private final TypeLiteral configSupplierTypeLiteral;
    private final String appName;

    public ConfigModule(
            String appName,
            TypeLiteral configSupplierTypeLiteral
    ) {
        Class configClass = extractConfigType(configSupplierTypeLiteral);
        this.configTypeLiteral = TypeLiteral.get(configClass);
        this.configSupplierTypeLiteral = configSupplierTypeLiteral;
        this.appName = appName;
    }

    private static Class extractConfigType(TypeLiteral typeLiteral) {
        ParameterizedType parameterizedType =
                (ParameterizedType) typeLiteral.getType();
        Type configType = parameterizedType.getActualTypeArguments()[0];
        return (Class) configType;
    }

    @Override
    protected void configure() {

        final String zkConnectionString =
                getProperty(ZK_CONNECTION_STRING_PROPERTY_NAME);

        checkNotEmpty(
                ZK_CONNECTION_STRING_PROPERTY_NAME,
                zkConnectionString
        );

        binder().requireExplicitBindings();

        bind(String.class)
                .annotatedWith(ApplicationName.class)
                .toInstance(appName);

        bind(Version.class)
                .annotatedWith(Application.class)
                .toProvider(ApplicationVersionProvider.class)
                .in(SINGLETON);

        bind(CuratorFrameworkSupplier.class)
                .in(SINGLETON);

        bind(ObjectMapper.class)
                .toInstance(new ObjectMapper());

        bind(String.class)
                .annotatedWith(ConnectionString.class)
                .toInstance(zkConnectionString);

        bind(ScheduledExecutorService.class)
                .toInstance(newSingleThreadScheduledExecutor());

        bind(ConfigClient.class)
                .in(SINGLETON);

        bind(Class.class)
                .annotatedWith(Config.class)
                .toInstance(configTypeLiteral.getRawType());

        bind(configSupplierTypeLiteral)
                .in(SINGLETON);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy