Search in sources :

Example 71 with Binder

use of com.google.inject.Binder in project kernel by exoplatform.

the class GuiceContainer method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() {
    ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
    // We check if the component has been defined in the configuration of the current container
    // The goal is to enable the GuicegContainer only if it is needed
    Component component = cm.getComponent(ModuleProvider.class);
    if (component == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No ModuleProvider has been defined, thus the GuiceContainer will be disabled." + " To enable the Guice Integration please define a ModuleProvider");
        }
    } else {
        ModuleProvider provider = super.getComponentInstanceOfType(ModuleProvider.class, false);
        injector = Guice.createInjector(provider.getModule(), new AbstractModule() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            protected void configure() {
                Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
                Binder binder = binder();
                for (ComponentAdapter<?> adapter : adapters) {
                    Object key = adapter.getComponentKey();
                    Class<?> type;
                    Annotation annotation = null;
                    Class<? extends Annotation> annotationType = null;
                    if (key instanceof Class<?> && !((Class<?>) key).isAnnotation()) {
                        type = (Class<?>) key;
                    } else {
                        if (key instanceof String) {
                            annotation = Names.named((String) key);
                        } else if (key instanceof Class<?>) {
                            annotationType = (Class<? extends Annotation>) key;
                        }
                        type = adapter.getComponentImplementation();
                    }
                    if (annotation == null && annotationType == null) {
                        binder.bind(type).toProvider(new ComponentAdapterProvider(type, adapter));
                    } else {
                        // As we don't know the type, we will bind it for each super classes and interfaces too
                        ComponentAdapterProvider provider = new ComponentAdapterProvider(type, adapter);
                        bindAll(binder, type, provider, annotation, annotationType);
                    }
                }
            }
        });
        LOG.info("A GuiceContainer has been enabled using the ModuleProvider " + provider.getClass());
    }
    super.start();
}
Also used : Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Binder(com.google.inject.Binder) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) AbstractComponentAdapter(org.exoplatform.container.AbstractComponentAdapter) ComponentAdapter(org.exoplatform.container.spi.ComponentAdapter)

Example 72 with Binder

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

the class AbstractLanguageServerTest method getServerModule.

protected com.google.inject.Module getServerModule() {
    ServerModule _serverModule = new ServerModule();
    final com.google.inject.Module _function = (Binder it) -> {
        AnnotatedBindingBuilder<RequestManager> _bind = it.<RequestManager>bind(RequestManager.class);
        _bind.toInstance(new RequestManager() {

            @Override
            public <V extends Object> CompletableFuture<V> runRead(final Function1<? super CancelIndicator, ? extends V> request) {
                final CompletableFuture<V> result = new CompletableFuture<V>();
                try {
                    final CancelIndicator _function = () -> {
                        return false;
                    };
                    result.complete(request.apply(_function));
                } catch (final Throwable _t) {
                    if (_t instanceof Throwable) {
                        final Throwable e = (Throwable) _t;
                        result.completeExceptionally(e);
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
                return result;
            }

            @Override
            public <U extends Object, V extends Object> CompletableFuture<V> runWrite(final Function0<? extends U> nonCancellable, final Function2<? super CancelIndicator, ? super U, ? extends V> request) {
                final CompletableFuture<V> result = new CompletableFuture<V>();
                try {
                    final CancelIndicator _function = () -> {
                        return false;
                    };
                    result.complete(request.apply(_function, nonCancellable.apply()));
                } catch (final Throwable _t) {
                    if (_t instanceof Throwable) {
                        final Throwable e = (Throwable) _t;
                        result.completeExceptionally(e);
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
                return result;
            }
        });
    };
    return Modules2.mixin(_serverModule, _function);
}
Also used : Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Function0(org.eclipse.xtext.xbase.lib.Functions.Function0) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) ServerModule(org.eclipse.xtext.ide.server.ServerModule) Binder(com.google.inject.Binder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) RequestManager(org.eclipse.xtext.ide.server.concurrent.RequestManager) CancelIndicator(org.eclipse.xtext.util.CancelIndicator)

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