Search in sources :

Example 1 with RatpackBaseRegistryModule

use of ratpack.guice.internal.RatpackBaseRegistryModule in project ratpack by ratpack.

the class Guice method buildInjector.

static Injector buildInjector(Registry baseRegistry, Action<? super BindingsSpec> bindingsAction, Function<? super Module, ? extends Injector> injectorFactory) throws Exception {
    List<Action<? super Binder>> binderActions = Lists.newLinkedList();
    List<Module> modules = Lists.newLinkedList();
    ServerConfig serverConfig = baseRegistry.get(ServerConfig.class);
    BindingsSpec bindings = new DefaultBindingsSpec(serverConfig, binderActions, modules);
    bindings.module(new RatpackBaseRegistryModule(baseRegistry));
    try {
        bindingsAction.execute(bindings);
    } catch (Exception e) {
        throw uncheck(e);
    }
    modules.add(new AdHocModule(binderActions));
    modules.add(new ConfigModule(serverConfig.getRequiredConfig()));
    Optional<BindingsImposition> bindingsImposition = Impositions.current().get(BindingsImposition.class);
    if (bindingsImposition.isPresent()) {
        BindingsImposition imposition = bindingsImposition.get();
        List<Action<? super Binder>> imposedBinderActions = Lists.newLinkedList();
        List<Module> imposedModules = Lists.newLinkedList();
        BindingsSpec imposedBindings = new DefaultBindingsSpec(serverConfig, imposedBinderActions, imposedModules);
        imposition.getBindings().execute(imposedBindings);
        imposedModules.add(new AdHocModule(imposedBinderActions));
        Module imposedModule = imposedModules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
        modules.add(imposedModule);
    }
    Module masterModule = modules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
    return injectorFactory.apply(masterModule);
}
Also used : Function(ratpack.func.Function) Module(com.google.inject.Module) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule) Context(ratpack.handling.Context) Modules(com.google.inject.util.Modules) Guice.createInjector(com.google.inject.Guice.createInjector) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Injector(com.google.inject.Injector) Stage(com.google.inject.Stage) Chain(ratpack.handling.Chain) ConfigObject(ratpack.config.ConfigObject) List(java.util.List) Lists(com.google.common.collect.Lists) Binder(com.google.inject.Binder) Impositions(ratpack.impose.Impositions) GuiceUtil(ratpack.guice.internal.GuiceUtil) InjectorRegistryBacking(ratpack.guice.internal.InjectorRegistryBacking) Action(ratpack.func.Action) Handler(ratpack.handling.Handler) ServerConfig(ratpack.server.ServerConfig) Registry(ratpack.registry.Registry) Optional(java.util.Optional) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) Action(ratpack.func.Action) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) Binder(com.google.inject.Binder) ServerConfig(ratpack.server.ServerConfig) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule) Module(com.google.inject.Module) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule)

Aggregations

Lists (com.google.common.collect.Lists)1 Binder (com.google.inject.Binder)1 Guice.createInjector (com.google.inject.Guice.createInjector)1 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 Stage (com.google.inject.Stage)1 Modules (com.google.inject.util.Modules)1 List (java.util.List)1 Optional (java.util.Optional)1 ConfigObject (ratpack.config.ConfigObject)1 Action (ratpack.func.Action)1 Function (ratpack.func.Function)1 DefaultBindingsSpec (ratpack.guice.internal.DefaultBindingsSpec)1 GuiceUtil (ratpack.guice.internal.GuiceUtil)1 InjectorRegistryBacking (ratpack.guice.internal.InjectorRegistryBacking)1 RatpackBaseRegistryModule (ratpack.guice.internal.RatpackBaseRegistryModule)1 Chain (ratpack.handling.Chain)1 Context (ratpack.handling.Context)1 Handler (ratpack.handling.Handler)1 Impositions (ratpack.impose.Impositions)1