com.google.inject.spi.ProvisionListener Java Examples

The following examples show how to use com.google.inject.spi.ProvisionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextualProviderModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void configure() {
    bindListener(Matchers.any(), new ProvisionListener() {
        @Override
        public <T> void onProvision(ProvisionInvocation<T> provision) {
            final ProvisionListener.ProvisionInvocation<?> prior = ContextualProvider.provisionInvocation.get();
            ContextualProvider.provisionInvocation.set(provision);
            try {
                provision.provision();
            } finally {
                if(prior != null) {
                    ContextualProvider.provisionInvocation.set(prior);
                } else {
                    ContextualProvider.provisionInvocation.remove();
                }
            }
        }
    });
}
 
Example #2
Source File: DropwizardModule.java    From dropwizard-guicier with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final Binder binder) {
  binder.bindListener(Matchers.any(), new ProvisionListener() {
    @Override
    public <T> void onProvision(ProvisionInvocation<T> provision) {
      Object obj = provision.provision();

      if (obj instanceof Managed) {
        handle((Managed) obj);
      }

      if (obj instanceof Task) {
        handle((Task) obj);
      }

      if (obj instanceof HealthCheck) {
        handle((HealthCheck) obj);
      }

      if (obj instanceof ServerLifecycleListener) {
        handle((ServerLifecycleListener) obj);
      }
    }
  });
}
 
Example #3
Source File: ContextualProvider.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
final public T get() {
    final ProvisionListener.ProvisionInvocation<T> pi = (ProvisionListener.ProvisionInvocation<T>) provisionInvocation.get();
    if(pi == null) {
        return getWithoutContext();
    } else {
        return getFor(pi.getBinding(), pi.getDependencyChain());
    }
}
 
Example #4
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Adapt a {@link Consumer} to be a {@link ProvisionListener} (which is not lambda compatible)
 */
static ProvisionListener provisionListener(Consumer<ProvisionListener.ProvisionInvocation<?>> consumer) {
    return consumer::accept;
}
 
Example #5
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Adapt the given matcher and listener and pass them to {@link Binder#bindListener(Matcher, ProvisionListener...)}
 */
default void bindProvisionListener(Predicate<? super Binding<?>> matcher, Consumer<ProvisionListener.ProvisionInvocation<?>> listener) {
    bindListener(Matchers.predicate(matcher), provisionListener(listener));
}
 
Example #6
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
default <T> void bindProvisionSubtypesOfListener(TypeLiteral<T> type, Consumer<ProvisionListener.ProvisionInvocation<T>> listener) {
    bindProvisionListener(bindingsForSubtypesOf(type), (Consumer) listener);
}
 
Example #7
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
default <T> void bindProvisionSubtypesOfListener(Class<T> type, Consumer<ProvisionListener.ProvisionInvocation<T>> listener) {
    bindProvisionSubtypesOfListener(TypeLiteral.get(type), listener);
}
 
Example #8
Source File: InjectionLogger.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void configure() {
    final InjectionLogger listener = new InjectionLogger(logger);
    bindListener(Matchers.any(), (TypeListener) listener);
    bindListener(Matchers.any(), (ProvisionListener) listener);
}
 
Example #9
Source File: GuiceServiceModule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
  binder().bindListener(Matchers.any(), new ProvisionListener() {
    @Override
    public <T> void onProvision(ProvisionInvocation<T> provision) {
      final Binding<T> binding = provision.getBinding();
      logger.debug("provisioning {}", binding.getKey().getTypeLiteral());

      final T provisioned = provision.provision();

      if (provisioned != null && Service.class.isAssignableFrom(provisioned.getClass())) {
        final AtomicBoolean start = new AtomicBoolean(false);
        binding.acceptScopingVisitor(new DefaultBindingScopingVisitor<T>() {
          @Override
          public T visitEagerSingleton() {
            start.set(true);
            return super.visitEagerSingleton();
          }

          @Override
          public T visitScope(Scope scope) {
            if (scope instanceof SingletonScope) {
              start.set(true);
            }
            return super.visitScope(scope);
          }
        });

        if (start.get()) {
          serviceList.push(binding.getKey().getTypeLiteral().getRawType());
          try {
            logger.debug("starting {}", binding.getKey().getTypeLiteral());
            ((Service) provisioned).start();
          } catch (Exception e) {
            throwIfUnchecked(e);
            throw new RuntimeException(e);
          }
        }
      }
    }
  });
}