Exemplo n.º 1
0
  @Nonnull
  @Override
  public <T> Collection<Qualified<T>> getQualifiedInstances(@Nonnull Class<T> type)
      throws InstanceNotFoundException {
    requireNonNull(type, ERROR_TYPE_NULL);

    if (isClosed()) {
      throw new InstanceNotFoundException(type, new ClosedInjectorException(this));
    }

    List<Qualified<T>> instances = new ArrayList<>();

    List<com.google.inject.Binding<T>> bindings;
    try {
      bindings = delegate.findBindingsByType(TypeLiteral.get(type));
    } catch (RuntimeException e) {
      throw new InstanceNotFoundException(type, e);
    }
    if (bindings == null) {
      throw new InstanceNotFoundException(type);
    }

    for (com.google.inject.Binding<T> binding : bindings) {
      try {
        Key<T> key = binding.getKey();
        final T instance = delegate.getInstance(key);
        instances.add(new Qualified<>(instance, translate(key.getAnnotation())));
      } catch (RuntimeException e) {
        throw new InstanceNotFoundException(type, e);
      }
    }

    return instances;
  }
 private ContextListenerDefinition[] collectContextListenerDefinitions(Injector injector) {
   List<ContextListenerDefinition> contextListeners = new ArrayList<ContextListenerDefinition>();
   TypeLiteral<ContextListenerDefinition> LISTENER_DEFS =
       TypeLiteral.get(ContextListenerDefinition.class);
   for (Binding<ContextListenerDefinition> entry : injector.findBindingsByType(LISTENER_DEFS)) {
     contextListeners.add(entry.getProvider().get());
   }
   // Convert to a fixed size array for speed.
   return contextListeners.toArray(new ContextListenerDefinition[contextListeners.size()]);
 }
 private HttpSessionListenerDefinition[] collectListenerDefinitions(Injector injector) {
   List<HttpSessionListenerDefinition> sessionListeners =
       new ArrayList<HttpSessionListenerDefinition>();
   TypeLiteral<HttpSessionListenerDefinition> LISTENER_DEFS =
       TypeLiteral.get(HttpSessionListenerDefinition.class);
   for (Binding<HttpSessionListenerDefinition> entry :
       injector.findBindingsByType(LISTENER_DEFS)) {
     sessionListeners.add(entry.getProvider().get());
   }
   // Convert to a fixed size array for speed.
   return sessionListeners.toArray(new HttpSessionListenerDefinition[sessionListeners.size()]);
 }
Exemplo n.º 4
0
    @Inject
    public void inject(Injector injector) {
      final List<Binding<Emitter>> emitterBindings =
          injector.findBindingsByType(new TypeLiteral<Emitter>() {});

      emitter = findEmitter(emitterType, emitterBindings);

      if (emitter == null) {
        emitter = findEmitter(LogEmitterModule.EMITTER_TYPE, emitterBindings);
      }

      if (emitter == null) {
        List<String> knownTypes = Lists.newArrayList();
        for (Binding<Emitter> binding : emitterBindings) {
          final Annotation annotation = binding.getKey().getAnnotation();
          if (annotation != null) {
            knownTypes.add(((Named) annotation).value());
          }
        }
        throw new ISE(
            "Uknown emitter type[%s]=[%s], known types[%s]",
            EMITTER_PROPERTY, emitterType, knownTypes);
      }
    }
Exemplo n.º 5
0
  private Module create(
      final Logger LOG,
      final LifecycleManager manager,
      final Collection<Module> loadedModules,
      final List<Module> rootModules,
      final boolean isBootstrap,
      final Module bootstrapModule)
      throws Exception {
    LOG.info("Creating {} injector", isBootstrap ? "bootstrap" : "main");
    // Populate all the bootstrap state from the main module
    final List<Element> elements = Elements.getElements(Stage.DEVELOPMENT, rootModules);
    final Set<Key<?>> keys = ElementsEx.getAllInjectionKeys(elements);
    final List<String> moduleNames = ElementsEx.getAllSourceModules(elements);

    final Injector injector =
        Guice.createInjector(
            stage,
            new LifecycleModule(),
            new AbstractModule() {
              @Override
              protected void configure() {
                bind(LifecycleManager.class).toInstance(manager);
                requestInjection(manager);
              }
            },
            Modules.override(
                    new DefaultModule() {
                      @Provides
                      public AutoContext getContext() {
                        return new AutoContext() {
                          @Override
                          public boolean hasProfile(String profile) {
                            return profiles.contains(profile);
                          }

                          @Override
                          public boolean hasModule(String className) {
                            return moduleNames.contains(className);
                          }

                          @Override
                          public boolean hasBinding(Key<?> key) {
                            return keys.contains(key);
                          }
                        };
                      }
                    })
                .with(bootstrapModule));

    PropertySource propertySource = injector.getInstance(PropertySource.class);

    // Iterate through all loaded modules and filter out any modules that
    // have failed the condition check.  Also, keep track of any override modules
    // for already installed modules.
    final List<Module> overrideModules = new ArrayList<>();
    final List<Module> moreModules = new ArrayList<>();
    for (Module module : loadedModules) {
      if (!isEnabled(propertySource, module.getClass().getName())) {
        LOG.info("Ignoring module {}", module.getClass().getName());
        continue;
      }

      Bootstrap bs = module.getClass().getAnnotation(Bootstrap.class);
      if (isBootstrap == (bs != null) && evaluateConditions(LOG, injector, module)) {
        OverrideModule override = module.getClass().getAnnotation(OverrideModule.class);
        if (override != null) {
          if (moduleNames.contains(override.value().getName())) {
            LOG.info("    Adding override module {}", module.getClass().getSimpleName());
            overrideModules.add(module);
          }
        } else {
          LOG.info("    Adding conditional module {}", module.getClass().getSimpleName());
          moreModules.add(module);
        }
      }
    }

    final List<Module> extModules = new ArrayList<>();
    List<Binding<ModuleProvider>> moduleProviders =
        injector.findBindingsByType(TypeLiteral.get(ModuleProvider.class));
    for (Binding<ModuleProvider> binding : moduleProviders) {
      Module module = binding.getProvider().get().get();
      LOG.debug("Adding exposed bootstrap module {}", module.getClass().getName());
      extModules.add(module);
    }

    LOG.debug("Root Modules     : " + rootModules);
    LOG.debug("More Modules     : " + moreModules);
    LOG.debug("Override Modules : " + overrideModules);
    LOG.debug("Ext Modules      : " + extModules);

    LOG.debug("Created {} injector", isBootstrap ? "bootstrap" : "main");

    Module m =
        Modules.override(
                new AbstractModule() {
                  @Override
                  protected void configure() {
                    install(Modules.combine(rootModules));
                    install(Modules.combine(moreModules));
                  }
                })
            .with(Modules.override(overrideModules).with(Modules.combine(extModules)));
    return m;
  }