Search in sources :

Example 66 with Binder

use of com.google.inject.Binder in project roboguice by roboguice.

the class FactoryProvider2 method getBindingFromNewInjector.

/**
   * Creates a child injector that binds the args, and returns the binding for the method's result.
   */
public Binding<?> getBindingFromNewInjector(final Method method, final Object[] args, final AssistData data) {
    checkState(injector != null, "Factories.create() factories cannot be used until they're initialized by Guice.");
    final Key<?> returnType = data.returnType;
    // We ignore any pre-existing binding annotation.
    final Key<?> returnKey = Key.get(returnType.getTypeLiteral(), RETURN_ANNOTATION);
    Module assistedModule = new AbstractModule() {

        @Override
        @SuppressWarnings({ "unchecked", // raw keys are necessary for the args array and return value
        "rawtypes" })
        protected void configure() {
            Binder binder = binder().withSource(method);
            int p = 0;
            if (!data.optimized) {
                for (Key<?> paramKey : data.paramTypes) {
                    // Wrap in a Provider to cover null, and to prevent Guice from injecting the parameter
                    binder.bind((Key) paramKey).toProvider(Providers.of(args[p++]));
                }
            } else {
                for (Key<?> paramKey : data.paramTypes) {
                    // Bind to our ThreadLocalProviders.
                    binder.bind((Key) paramKey).toProvider(data.providers.get(p++));
                }
            }
            Constructor constructor = data.constructor;
            // message for the user.
            if (constructor != null) {
                binder.bind(returnKey).toConstructor(constructor, (TypeLiteral) data.implementationType).in(// make sure we erase any scope on the implementation type
                Scopes.NO_SCOPE);
            }
        }
    };
    Injector forCreate = injector.createChildInjector(assistedModule);
    Binding<?> binding = forCreate.getBinding(returnKey);
    // If we have providers cached in data, cache the binding for future optimizations.
    if (data.optimized) {
        data.cachedBinding = binding;
    }
    return binding;
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Constructor(java.lang.reflect.Constructor) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) InjectionPoint(com.google.inject.spi.InjectionPoint) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 67 with Binder

use of com.google.inject.Binder in project graylog2-server by Graylog2.

the class CmdLineTool method run.

@Override
public void run() {
    final Level logLevel = setupLogger();
    final PluginBindings pluginBindings = installPluginConfigAndBindings(getPluginPath(configFile), chainingClassLoader);
    if (isDumpDefaultConfig()) {
        dumpDefaultConfigAndExit();
    }
    final NamedConfigParametersModule configModule = readConfiguration(configFile);
    if (isDumpConfig()) {
        dumpCurrentConfigAndExit();
    }
    if (!validateConfiguration()) {
        LOG.error("Validating configuration file failed - exiting.");
        System.exit(1);
    }
    beforeStart();
    final List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
    LOG.info("Running with JVM arguments: {}", Joiner.on(' ').join(arguments));
    injector = setupInjector(configModule, pluginBindings, binder -> binder.bind(ChainingClassLoader.class).toInstance(chainingClassLoader));
    if (injector == null) {
        LOG.error("Injector could not be created, exiting! (Please include the previous error messages in bug reports.)");
        System.exit(1);
    }
    // This is holding all our metrics.
    final MetricRegistry metrics = injector.getInstance(MetricRegistry.class);
    addInstrumentedAppender(metrics, logLevel);
    // Report metrics via JMX.
    final JmxReporter reporter = JmxReporter.forRegistry(metrics).build();
    reporter.start();
    startCommand();
}
Also used : NamedConfigParametersModule(com.github.joschi.jadconfig.guice.NamedConfigParametersModule) Option(com.github.rvesse.airline.annotations.Option) Module(com.google.inject.Module) Plugin(org.graylog2.plugin.Plugin) Arrays(java.util.Arrays) ParameterException(com.github.joschi.jadconfig.ParameterException) NodeIdPersistenceException(org.graylog2.plugin.system.NodeIdPersistenceException) GuiceInjectorHolder(org.graylog2.shared.bindings.GuiceInjectorHolder) RepositoryException(com.github.joschi.jadconfig.RepositoryException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Tools(org.graylog2.plugin.Tools) LoggerFactory(org.slf4j.LoggerFactory) Level(org.apache.logging.log4j.Level) InstrumentedAppender(com.codahale.metrics.log4j2.InstrumentedAppender) Slf4JLoggerFactory(org.jboss.netty.logging.Slf4JLoggerFactory) Message(com.google.inject.spi.Message) PluginLoaderConfig(org.graylog2.plugin.PluginLoaderConfig) Map(java.util.Map) InternalLoggerFactory(org.jboss.netty.logging.InternalLoggerFactory) Version(org.graylog2.plugin.Version) JmxReporter(com.codahale.metrics.JmxReporter) PropertiesRepository(com.github.joschi.jadconfig.repositories.PropertiesRepository) Command(com.github.rvesse.airline.annotations.Command) ChainingClassLoader(org.graylog2.shared.plugins.ChainingClassLoader) PluginModule(org.graylog2.plugin.PluginModule) ExceptionUtils(org.graylog2.shared.utilities.ExceptionUtils) JodaTimeConverterFactory(com.github.joschi.jadconfig.jodatime.JodaTimeConverterFactory) Collection(java.util.Collection) Set(java.util.Set) ServerStatus(org.graylog2.plugin.ServerStatus) ValidationException(com.github.joschi.jadconfig.ValidationException) Sets(com.google.common.collect.Sets) List(java.util.List) PluginBindings(org.graylog2.shared.bindings.PluginBindings) PluginMetaData(org.graylog2.plugin.PluginMetaData) UI(org.graylog2.shared.UI) AccessDeniedException(java.nio.file.AccessDeniedException) GuavaConverterFactory(com.github.joschi.jadconfig.guava.GuavaConverterFactory) Joiner(com.google.common.base.Joiner) EnvironmentRepository(com.github.joschi.jadconfig.repositories.EnvironmentRepository) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) SystemPropertiesRepository(com.github.joschi.jadconfig.repositories.SystemPropertiesRepository) JadConfig(com.github.joschi.jadconfig.JadConfig) ManagementFactory(java.lang.management.ManagementFactory) NamedConfigParametersModule(com.github.joschi.jadconfig.guice.NamedConfigParametersModule) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PluginConfigBean(org.graylog2.plugin.PluginConfigBean) Names(com.google.inject.name.Names) PluginLoader(org.graylog2.shared.plugins.PluginLoader) File(java.io.File) Injector(com.google.inject.Injector) CreationException(com.google.inject.CreationException) Repository(com.github.joschi.jadconfig.Repository) BaseConfiguration(org.graylog2.plugin.BaseConfiguration) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) PluginBindings(org.graylog2.shared.bindings.PluginBindings) MetricRegistry(com.codahale.metrics.MetricRegistry) Level(org.apache.logging.log4j.Level) JmxReporter(com.codahale.metrics.JmxReporter) ChainingClassLoader(org.graylog2.shared.plugins.ChainingClassLoader)

Aggregations

Binder (com.google.inject.Binder)67 Module (com.google.inject.Module)64 Injector (com.google.inject.Injector)49 Test (org.junit.Test)40 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 Self (io.druid.guice.annotations.Self)12 DruidNode (io.druid.server.DruidNode)12 IOException (java.io.IOException)10 TypeLiteral (com.google.inject.TypeLiteral)9 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)9 Properties (java.util.Properties)8 RMContext (org.apache.hadoop.yarn.server.resourcemanager.RMContext)7 ResourceManager (org.apache.hadoop.yarn.server.resourcemanager.ResourceManager)7 ImmutableList (com.google.common.collect.ImmutableList)5 Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)5 List (java.util.List)5 Provides (com.google.inject.Provides)4 LifecycleModule (io.druid.guice.LifecycleModule)4 HashSet (java.util.HashSet)4 AnnotationIntrospectorPair (com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair)3