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

com.palominolabs.config.ConfigModule Maven / Gradle / Ivy

The newest version!
package com.palominolabs.config;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import org.skife.config.ConfigurationObjectFactory;

/**
 * Guice module that binds a ConfigurationObjectFactory.
 *
 * Create a ConfigModule using ConfigModuleBuilder, then use bindConfigBean from your own modules to bind any config
 * beans.
 */
public final class ConfigModule extends AbstractModule {

    private final ConfigurationObjectFactory configurationObjectFactory;

    ConfigModule(ConfigurationObjectFactory configurationObjectFactory) {
        this.configurationObjectFactory = configurationObjectFactory;
    }

    @Override
    protected void configure() {
        bind(ConfigurationObjectFactory.class).annotatedWith(ConfigInjectFactory.class)
            .toInstance(configurationObjectFactory);
    }

    /**
     * Binds a provider for the type configBeanType.
     *
     * This is intended to be called from other modules' configure() methods.
     *
     * @param binder         binder from caller module
     * @param configBeanType config bean type to bind
     * @param             type of configBeanType
     */
    public static  void bindConfigBean(Binder binder, Class configBeanType) {
        binder.bind(configBeanType).toProvider(new ConfigProvider(configBeanType)).in(Scopes.SINGLETON);
    }

    /**
     * Instantiate a config bean directly.
     *
     * Useful for getting config objects during module initialization time.
     *
     * @param klass the bean class
     * @param    klass's type
     * @return a populated config bean
     */
    public  T getConfigBean(Class klass) {
        return configurationObjectFactory.build(klass);
    }

    private static class ConfigProvider implements Provider {

        @Inject
        @ConfigInjectFactory
        private ConfigurationObjectFactory beanFactory;

        private final Class configClass;

        private ConfigProvider(Class configClass) {
            this.configClass = configClass;
        }

        @Override
        public T get() {
            return beanFactory.build(configClass);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy