Search in sources :

Example 36 with Binder

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

the class CmdLineTool method setupInjector.

protected Injector setupInjector(NamedConfigParametersModule configModule, Module... otherModules) {
    try {
        final ImmutableList.Builder<Module> modules = ImmutableList.builder();
        modules.add(configModule);
        modules.addAll(getSharedBindingsModules());
        modules.addAll(getCommandBindings());
        modules.addAll(Arrays.asList(otherModules));
        modules.add(new Module() {

            @Override
            public void configure(Binder binder) {
                binder.bind(String.class).annotatedWith(Names.named("BootstrapCommand")).toInstance(commandName);
            }
        });
        return GuiceInjectorHolder.createInjector(modules.build());
    } catch (CreationException e) {
        annotateInjectorCreationException(e);
        return null;
    } catch (Exception e) {
        LOG.error("Injector creation failed!", e);
        return null;
    }
}
Also used : Binder(com.google.inject.Binder) ImmutableList(com.google.common.collect.ImmutableList) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) PluginModule(org.graylog2.plugin.PluginModule) NamedConfigParametersModule(com.github.joschi.jadconfig.guice.NamedConfigParametersModule) ParameterException(com.github.joschi.jadconfig.ParameterException) NodeIdPersistenceException(org.graylog2.plugin.system.NodeIdPersistenceException) RepositoryException(com.github.joschi.jadconfig.RepositoryException) ValidationException(com.github.joschi.jadconfig.ValidationException) AccessDeniedException(java.nio.file.AccessDeniedException) CreationException(com.google.inject.CreationException)

Example 37 with Binder

use of com.google.inject.Binder in project xtext-core by eclipse.

the class WrappingInjectorProvider method createInjector.

protected Injector createInjector() {
    Injector delegateInjector = delegate.getInjector();
    final Map<Key<?>, Binding<?>> bindings = delegateInjector.getBindings();
    Injector injector = Guice.createInjector(Modules.override(new Module() {

        @Override
        public void configure(Binder binder) {
            for (Binding<?> binding : bindings.values()) {
                Type typeLiteral = binding.getKey().getTypeLiteral().getType();
                if (!Injector.class.equals(typeLiteral) && !Logger.class.equals(typeLiteral)) {
                    binding.applyTo(binder);
                }
            }
        }
    }).with(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bind(IParser.class).toInstance(new TestDataProvider());
        }
    }));
    return injector;
}
Also used : Binding(com.google.inject.Binding) Binder(com.google.inject.Binder) Type(java.lang.reflect.Type) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Logger(java.util.logging.Logger) Key(com.google.inject.Key) IParser(org.eclipse.xtext.parser.IParser)

Example 38 with Binder

use of com.google.inject.Binder in project xtext-core by eclipse.

the class XtextGeneratorTemplates method createBindingMethod.

private StringConcatenationClient createBindingMethod(final GuiceModuleAccess.Binding it) {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            {
                if (((!it.getValue().isProvider()) && it.getValue().getStatements().isEmpty())) {
                    _builder.append("// contributed by ");
                    String _contributedBy = it.getContributedBy();
                    _builder.append(_contributedBy);
                    _builder.newLineIfNotEmpty();
                    {
                        boolean _isSingleton = it.getKey().isSingleton();
                        if (_isSingleton) {
                            _builder.append("@");
                            _builder.append(SingletonBinding.class);
                            {
                                boolean _isEagerSingleton = it.getKey().isEagerSingleton();
                                if (_isEagerSingleton) {
                                    _builder.append("(eager=true)");
                                }
                            }
                        }
                    }
                    _builder.newLineIfNotEmpty();
                    _builder.append("public ");
                    {
                        Object _expression = it.getValue().getExpression();
                        boolean _tripleEquals = (_expression == null);
                        if (_tripleEquals) {
                            _builder.append("Class<? extends ");
                            TypeReference _type = it.getKey().getType();
                            _builder.append(_type);
                            _builder.append(">");
                        } else {
                            TypeReference _type_1 = it.getKey().getType();
                            _builder.append(_type_1);
                        }
                    }
                    _builder.append(" ");
                    CharSequence _bindMethodName = XtextGeneratorTemplates.this.getBindMethodName(it);
                    _builder.append(_bindMethodName);
                    _builder.append("() {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("return ");
                    {
                        Object _expression_1 = it.getValue().getExpression();
                        boolean _tripleNotEquals = (_expression_1 != null);
                        if (_tripleNotEquals) {
                            Object _expression_2 = it.getValue().getExpression();
                            _builder.append(_expression_2, "\t");
                        } else {
                            TypeReference _type_2 = it.getValue().getType();
                            _builder.append(_type_2, "\t");
                            _builder.append(".class");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    _builder.append("}");
                    _builder.newLine();
                } else {
                    boolean _isEmpty = it.getValue().getStatements().isEmpty();
                    if (_isEmpty) {
                        _builder.append("// contributed by ");
                        String _contributedBy_1 = it.getContributedBy();
                        _builder.append(_contributedBy_1);
                        _builder.newLineIfNotEmpty();
                        {
                            boolean _isSingleton_1 = it.getKey().isSingleton();
                            if (_isSingleton_1) {
                                _builder.append("@");
                                _builder.append(SingletonBinding.class);
                                {
                                    boolean _isEagerSingleton_1 = it.getKey().isEagerSingleton();
                                    if (_isEagerSingleton_1) {
                                        _builder.append("(eager=true)");
                                    }
                                }
                            }
                        }
                        _builder.newLineIfNotEmpty();
                        _builder.append("public ");
                        {
                            Object _expression_3 = it.getValue().getExpression();
                            boolean _tripleEquals_1 = (_expression_3 == null);
                            if (_tripleEquals_1) {
                                _builder.append("Class<? extends ");
                                _builder.append(Provider.class);
                                _builder.append("<? extends ");
                                TypeReference _type_3 = it.getKey().getType();
                                _builder.append(_type_3);
                                _builder.append(">>");
                            } else {
                                _builder.append(Provider.class);
                                _builder.append("<? extends ");
                                TypeReference _type_4 = it.getKey().getType();
                                _builder.append(_type_4);
                                _builder.append(">");
                            }
                        }
                        _builder.append(" ");
                        CharSequence _bindMethodName_1 = XtextGeneratorTemplates.this.getBindMethodName(it);
                        _builder.append(_bindMethodName_1);
                        _builder.append("() {");
                        _builder.newLineIfNotEmpty();
                        _builder.append("\t");
                        _builder.append("return ");
                        {
                            Object _expression_4 = it.getValue().getExpression();
                            boolean _tripleNotEquals_1 = (_expression_4 != null);
                            if (_tripleNotEquals_1) {
                                Object _expression_5 = it.getValue().getExpression();
                                _builder.append(_expression_5, "\t");
                            } else {
                                TypeReference _type_5 = it.getValue().getType();
                                _builder.append(_type_5, "\t");
                                _builder.append(".class");
                            }
                        }
                        _builder.append(";");
                        _builder.newLineIfNotEmpty();
                        _builder.append("}");
                        _builder.newLine();
                    } else {
                        _builder.append("// contributed by ");
                        String _contributedBy_2 = it.getContributedBy();
                        _builder.append(_contributedBy_2);
                        _builder.newLineIfNotEmpty();
                        _builder.append("public void ");
                        CharSequence _bindMethodName_2 = XtextGeneratorTemplates.this.getBindMethodName(it);
                        _builder.append(_bindMethodName_2);
                        _builder.append("(");
                        _builder.append(Binder.class);
                        _builder.append(" binder) {");
                        _builder.newLineIfNotEmpty();
                        {
                            List<Object> _statements = it.getValue().getStatements();
                            for (final Object statement : _statements) {
                                _builder.append("\t");
                                _builder.append(statement, "\t");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                        _builder.append("}");
                        _builder.newLine();
                    }
                }
            }
        }
    };
    return _client;
}
Also used : Binder(com.google.inject.Binder) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Example 39 with Binder

use of com.google.inject.Binder in project xtext-core by eclipse.

the class UriBasedReaderTest method testIssuesInOtherResource.

@Test
public void testIssuesInOtherResource() throws Exception {
    UriBasedReader reader = new UriBasedReader() {

        {
            getValidator().setStopOnError(false);
        }
    };
    reader.setClasspathURIContext(getClass());
    reader.addRegister(new XtextStandaloneSetup() {

        @Override
        public Injector createInjector() {
            return Guice.createInjector(new org.eclipse.xtext.XtextRuntimeModule() {

                @Override
                public void configureFileExtensions(Binder binder) {
                    binder.bind(String.class).annotatedWith(Names.named(Constants.FILE_EXTENSIONS)).toInstance("xtexterror");
                }
            });
        }

        @Override
        public void register(Injector injector) {
            EPackage.Registry.INSTANCE.put(XtextPackage.eINSTANCE.getNsURI(), XtextPackage.eINSTANCE);
            IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
            IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
            Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xtexterror", resourceFactory);
            IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("xtexterror", serviceProvider);
        }
    });
    final String errorURI = (getClass().getName() + "ResourceWithError").replace('.', '/') + ".xtexterror";
    final String loadingURI = "classpath:/" + (getClass().getName() + "LoadingResourceWithError").replace('.', '/') + ".xtexterror";
    reader.addUri(loadingURI);
    SlotEntry slotEntry = new SlotEntry();
    slotEntry.setType("Grammar");
    slotEntry.setFirstOnly(true);
    reader.addLoad(slotEntry);
    WorkflowContextDefaultImpl ctx = new WorkflowContextDefaultImpl();
    IssuesImpl issues = new IssuesImpl();
    reader.checkConfiguration(issues);
    assertFalse(issues.toString(), issues.hasErrors());
    reader.invoke(ctx, new NullProgressMonitor(), issues);
    assertTrue(ctx.get(slotEntry.getSlot()) instanceof Grammar);
    assertEquals(1, issues.getErrors().length);
    final String errorString = issues.getErrors()[0].toString();
    assertFalse(errorString, errorString.contains(loadingURI));
    assertTrue(errorString, errorString.contains(errorURI));
}
Also used : NullProgressMonitor(org.eclipse.emf.mwe.core.monitor.NullProgressMonitor) WorkflowContextDefaultImpl(org.eclipse.emf.mwe.core.WorkflowContextDefaultImpl) Grammar(org.eclipse.xtext.Grammar) Binder(com.google.inject.Binder) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Injector(com.google.inject.Injector) IssuesImpl(org.eclipse.emf.mwe.core.issues.IssuesImpl) IResourceFactory(org.eclipse.xtext.resource.IResourceFactory) XtextStandaloneSetup(org.eclipse.xtext.XtextStandaloneSetup) Test(org.junit.Test)

Example 40 with Binder

use of com.google.inject.Binder in project druid by druid-io.

the class SpnegoFilterConfigTest method testserde.

@Test
public void testserde() {
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.install(new PropertiesModule(Arrays.asList("test.runtime.properties")));
            binder.install(new ConfigModule());
            binder.install(new DruidGuiceExtensions());
            JsonConfigProvider.bind(binder, "druid.hadoop.security.spnego", SpnegoFilterConfig.class);
        }

        @Provides
        @LazySingleton
        public ObjectMapper jsonMapper() {
            return new DefaultObjectMapper();
        }
    });
    Properties props = injector.getInstance(Properties.class);
    SpnegoFilterConfig config = injector.getInstance(SpnegoFilterConfig.class);
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.principal"), config.getPrincipal());
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.keytab"), config.getKeytab());
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.authToLocal"), config.getAuthToLocal());
}
Also used : DruidGuiceExtensions(io.druid.guice.DruidGuiceExtensions) ConfigModule(io.druid.guice.ConfigModule) Provides(com.google.inject.Provides) Properties(java.util.Properties) Binder(com.google.inject.Binder) LazySingleton(io.druid.guice.LazySingleton) Injector(com.google.inject.Injector) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Module(com.google.inject.Module) PropertiesModule(io.druid.guice.PropertiesModule) ConfigModule(io.druid.guice.ConfigModule) PropertiesModule(io.druid.guice.PropertiesModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Aggregations

Binder (com.google.inject.Binder)72 Module (com.google.inject.Module)65 Injector (com.google.inject.Injector)51 Test (org.junit.Test)41 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 Key (com.google.inject.Key)4 Provides (com.google.inject.Provides)4 LifecycleModule (io.druid.guice.LifecycleModule)4 HashSet (java.util.HashSet)4