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

com.netflix.karyon.transport.AbstractServerModule Maven / Gradle / Ivy

There is a newer version: 2.1.00-RC6
Show newest version
package com.netflix.karyon.transport;

import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.netflix.karyon.transport.AbstractServerModule.ServerConfigBuilder;
import io.reactivex.netty.metrics.MetricEventsListenerFactory;
import io.reactivex.netty.pipeline.PipelineConfigurator;

/**
 * @author Tomasz Bak
 */
@SuppressWarnings("rawtypes")
public abstract class AbstractServerModule extends AbstractModule {

    protected final Named nameAnnotation;
    protected final Class iType;
    protected final Class oType;

    protected final Key pipelineConfiguratorKey;
    protected final Key metricEventsListenerFactoryKey;

    protected final Key serverConfigKey;
    protected final B serverConfigBuilder;

    protected AbstractServerModule(String moduleName, Class iType, Class oType) {
        nameAnnotation = Names.named(moduleName);
        this.iType = iType;
        this.oType = oType;

        pipelineConfiguratorKey = Key.get(PipelineConfigurator.class, nameAnnotation);
        metricEventsListenerFactoryKey = Key.get(MetricEventsListenerFactory.class, nameAnnotation);
        serverConfigKey = Key.get(ServerConfig.class, nameAnnotation);

        serverConfigBuilder = newServerConfigBuilder();
    }

    protected abstract void configureServer();

    protected abstract B newServerConfigBuilder();

    protected LinkedBindingBuilder bindPipelineConfigurator() {
        return bind(pipelineConfiguratorKey);
    }

    protected LinkedBindingBuilder bindEventsListenerFactory() {
        return bind(metricEventsListenerFactoryKey);
    }

    protected B server() {
        return serverConfigBuilder;
    }

    public static class ServerConfig {
        private final int port;

        public ServerConfig(int port) {
            this.port = port;
        }

        public int getPort() {
            return port;
        }
    }

    @SuppressWarnings("unchecked")
    public static class ServerConfigBuilder {

        protected int port = 8080;

        public B port(int port) {
            this.port = port;
            return (B) this;
        }

        public C build() {
            return (C) new ServerConfig(port);
        }
    }
}