Search in sources :

Example 1 with AnnotatedBindingBuilder

use of com.google.inject.binder.AnnotatedBindingBuilder in project vespa by vespa-engine.

the class TestDriver method newModuleList.

private static List<Module> newModuleList(final Application app, final Class<? extends Application> appClass, Module... guiceModules) {
    List<Module> lst = new LinkedList<>();
    lst.addAll(Arrays.asList(guiceModules));
    lst.add(new AbstractModule() {

        @Override
        public void configure() {
            AnnotatedBindingBuilder<Application> builder = bind(Application.class);
            if (app != null) {
                builder.toInstance(app);
            } else {
                builder.to(appClass);
            }
        }
    });
    return lst;
}
Also used : AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) LinkedList(java.util.LinkedList) AbstractModule(com.google.inject.AbstractModule)

Example 2 with AnnotatedBindingBuilder

use of com.google.inject.binder.AnnotatedBindingBuilder in project guice by google.

the class BinderTestSuite method suite.

public static Test suite() {
    TestSuite suite = new TestSuite();
    new Builder().name("bind A").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(A.class);
        }
    }).creationException("No implementation for BinderTestSuite$A was bound.").addToSuite(suite);
    new Builder().name("bind PlainA named apple").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(PlainA.class).annotatedWith(named("apple"));
        }
    }).creationException("No implementation for BinderTestSuite$PlainA annotated with " + shortNamed(named("apple")) + " was bound").addToSuite(suite);
    new Builder().name("bind A to new PlainA(1)").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(A.class).toInstance(new PlainA(1));
        }
    }).creationTime(CreationTime.NONE).expectedValues(new PlainA(1), new PlainA(1), new PlainA(1)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).addToSuite(suite);
    new Builder().name("no binding, ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy named apple").key(Key.get(AWithProvidedBy.class, named("apple")), InjectsAWithProvidedByNamedApple.class).configurationException("No implementation for BinderTestSuite$AWithProvidedBy annotated with " + shortNamed(named("apple")) + " was bound").addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy named apple").key(Key.get(AWithImplementedBy.class, named("apple")), InjectsAWithImplementedByNamedApple.class).configurationException("No implementation for BinderTestSuite$AWithImplementedBy annotated with " + shortNamed(named("apple")) + " was bound").addToSuite(suite);
    new Builder().name("no binding, ScopedA named apple").key(Key.get(ScopedA.class, named("apple")), InjectsScopedANamedApple.class).configurationException("No implementation for BinderTestSuite$ScopedA annotated with " + shortNamed(named("apple")) + " was bound").addToSuite(suite);
    for (final Scoper scoper : Scoper.values()) {
        new Builder().name("bind PlainA").key(Key.get(PlainA.class), InjectsPlainA.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                AnnotatedBindingBuilder<PlainA> abb = bind(PlainA.class);
                scoper.configure(abb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainA").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainAProvider.class").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to new PlainAProvider()").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(ScopedA.class);
                scoper.configure(sbb);
            }
        }).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(AWithProvidedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for BinderTestSuite$AWithProvidedBy annotated with " + shortNamed(named("apple")) + " was bound").scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(AWithImplementedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for BinderTestSuite$AWithImplementedBy annotated with " + shortNamed(named("apple")) + " was bound").scoper(scoper).addToSuite(suite);
        new Builder().name("bind ScopedA named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(ScopedA.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for BinderTestSuite$ScopedA annotated with " + shortNamed(named("apple")) + " was bound").scoper(scoper).addToSuite(suite);
    }
    return suite;
}
Also used : ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) TestSuite(junit.framework.TestSuite)

Example 3 with AnnotatedBindingBuilder

use of com.google.inject.binder.AnnotatedBindingBuilder in project platformlayer by platformlayer.

the class UserAuthServletModule method configureServlets.

@Override
protected void configureServlets() {
    extensions.addHttpExtensions(new HttpConfiguration() {

        @Override
        public FilterKeyBindingBuilder filter(String urlPattern) {
            return UserAuthServletModule.this.filter(urlPattern);
        }

        @Override
        public ServletKeyBindingBuilder serve(String urlPattern) {
            return UserAuthServletModule.this.serve(urlPattern);
        }

        @Override
        public <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
            return UserAuthServletModule.this.bind(clazz);
        }
    });
    bind(CORSFilter.class).asEagerSingleton();
    filter("/api/*").through(CORSFilter.class);
    bind(RegisterResource.class);
    bind(PingResource.class);
    serve("/api/tokens").with(RestLoginServlet.class);
    Map<String, String> params = Maps.newHashMap();
    params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "org.codehaus.jackson.jaxrs;com.fathomdb.jersey");
    serve("/*").with(GuiceContainer.class, params);
}
Also used : AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) HttpConfiguration(org.platformlayer.extensions.HttpConfiguration) CORSFilter(org.platformlayer.web.CORSFilter)

Example 4 with AnnotatedBindingBuilder

use of com.google.inject.binder.AnnotatedBindingBuilder in project platformlayer by platformlayer.

the class PlatformLayerServletModule method configureServlets.

@Override
protected void configureServlets() {
    // switch (ApplicationMode.getMode()) {
    // case DEVELOPMENT:
    // bind(OpenstackAuthenticationFilterBase.class).to(DevelopmentOpenstackAuthenticationFilter.class).in(Scopes.SINGLETON);
    // break;
    // 
    // default:
    // throw new IllegalStateException("Unhandled application mode: " + ApplicationMode.getMode());
    // }
    bind(MetricDataSourceWriter.class);
    bind(CORSFilter.class).asEagerSingleton();
    filter("/*").through(CORSFilter.class);
    bind(ScopeFilter.class).asEagerSingleton();
    filter("/*").through(ScopeFilter.class);
    extensions.addHttpExtensions(new HttpConfiguration() {

        @Override
        public FilterKeyBindingBuilder filter(String urlPattern) {
            return PlatformLayerServletModule.this.filter(urlPattern);
        }

        @Override
        public ServletKeyBindingBuilder serve(String urlPattern) {
            return PlatformLayerServletModule.this.serve(urlPattern);
        }

        @Override
        public <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
            return PlatformLayerServletModule.this.bind(clazz);
        }
    });
    bind(ProjectAuthorization.class).toProvider(ScopeProjectAuthorizationProvider.class);
    bind(Scope.class).toProvider(ScopeProvider.class);
    // if (ApplicationMode.isDevelopment()) {
    // bind(AuthenticationTokenValidator.class).to(DevelopmentTokenValidator.class);
    // } else {
    // bind(AuthenticationTokenValidator.class).to(PlatformLayerTokenValidator.class);
    // }
    bind(OpsAuthenticationFilter.class).asEagerSingleton();
    // /* bind the REST resources */
    // bind(ManagedItemCollectionResource.class);
    // bind(ManagedItemResource.class);
    bind(RootResource.class);
    // bind(ServiceAuthorizationCollectionResource.class);
    // bind(ServiceAuthorizationResource.class);
    // bind(ServiceResource.class);
    // bind(ServicesCollectionResource.class);
    filter("/v0/*").through(OpsAuthenticationFilter.class);
    Map<String, String> params = Maps.newHashMap();
    List<String> packages = Lists.newArrayList();
    packages.add("org.platformlayer.xaas.web.jaxrs");
    packages.add("org.platformlayer.xaas.web.resources");
    // packages.add("org.codehaus.jackson.jaxrs");
    TypeFactory typeFactory = TypeFactory.defaultInstance();
    ObjectMapper objectMapper = JsonHelper.buildObjectMapper(typeFactory, false);
    // TypeResolverBuilder<?> typer = new PlatformLayerTypeResolverBuilder();
    // TypeIdResolver typeIdResolver = new PlatformLayerTypeIdResolver(null, typeFactory);
    // this.requestInjection(typeIdResolver);
    // typer = typer.init(JsonTypeInfo.Id.CLASS, typeIdResolver);
    // typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
    // typer = typer.typeProperty("type");
    // objectMapper.setDefaultTyping(typer);
    bind(ObjectMapper.class).toInstance(objectMapper);
    bind(ObjectMapperProvider.class).in(Scopes.SINGLETON);
    bind(PlatformLayerJsonProvider.class).in(Scopes.SINGLETON);
    params.put(PackagesResourceConfig.PROPERTY_PACKAGES, Joiner.on(';').join(packages));
    serve("/v0/*").with(GuiceContainer.class, params);
// ImmutableMap.of(JSONConfiguration.FEATURE_POJO_MAPPING, "true"));
}
Also used : HttpConfiguration(org.platformlayer.extensions.HttpConfiguration) CORSFilter(org.platformlayer.web.CORSFilter) ObjectMapperProvider(org.platformlayer.xaas.web.jaxrs.ObjectMapperProvider) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) ScopeFilter(org.platformlayer.ScopeFilter) Scope(org.platformlayer.Scope) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with AnnotatedBindingBuilder

use of com.google.inject.binder.AnnotatedBindingBuilder 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

AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)8 AbstractModule (com.google.inject.AbstractModule)2 ScopedBindingBuilder (com.google.inject.binder.ScopedBindingBuilder)2 TestSuite (junit.framework.TestSuite)2 HttpConfiguration (org.platformlayer.extensions.HttpConfiguration)2 CORSFilter (org.platformlayer.web.CORSFilter)2 OrchidModule (com.eden.orchid.api.registration.OrchidModule)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 Binder (com.google.inject.Binder)1 Module (com.google.inject.Module)1 LinkedList (java.util.LinkedList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 EventBus (org.apache.shiro.event.EventBus)1 DefaultEventBus (org.apache.shiro.event.support.DefaultEventBus)1 ServerModule (org.eclipse.xtext.ide.server.ServerModule)1 RequestManager (org.eclipse.xtext.ide.server.concurrent.RequestManager)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 Function0 (org.eclipse.xtext.xbase.lib.Functions.Function0)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1