com.google.inject.binder.ScopedBindingBuilder Java Examples

The following examples show how to use com.google.inject.binder.ScopedBindingBuilder. 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: SecurityInternalModuleUnitTest.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Before
public void before() {
    PrivateBinder binder = mock(PrivateBinder.class);
    AnnotatedBindingBuilder ab = mock(AnnotatedBindingBuilder.class);
    when(binder.bind(any(Class.class))).thenReturn(ab);
    when(ab.annotatedWith(any(Annotation.class))).thenReturn(ab);
    AnnotatedElementBuilder aeb = mock(AnnotatedElementBuilder.class);
    when(binder.expose(any(Class.class))).thenReturn(aeb);
    ScopedBindingBuilder sb = mock(ScopedBindingBuilder.class);
    when(ab.toProvider(any(Provider.class))).thenReturn(sb);
    when(binder.bind(any(TypeLiteral.class))).thenReturn(ab);
    when(binder.skipSources(any(Class.class), any(Class.class))).thenReturn(binder);
    when(binder.skipSources(any(Class.class))).thenReturn(binder);
    securityConfigurer = mock(SecurityConfigurer.class);
    underTest = new SecurityInternalModule(securityConfigurer, new HashMap<>());
    Whitebox.setInternalState(underTest, "binder", binder);
}
 
Example #2
Source File: Binders.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
  public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, final Class<T> type, Comparator<T> comparator) {
	return bindListToInstancesOf(
			binder,
			(TypeLiteral<List<T>>) TypeLiteral.get(new ParameterizedType() {
  				final Type [] args = new Type[] { type };
				@Override
				public Type getRawType() {
					return List.class;
				}

				@Override
				public Type getOwnerType() {
					return null;
				}

				@Override
				public Type[] getActualTypeArguments() {
					return args;
				}
			}),
			comparator
		);
}
 
Example #3
Source File: InjectableMethod.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
public Module bindingModule() {
    return new KeyedManifest.Impl(method) {
        @Override
        public void configure() {
            final Binder binder = binder().withSource(method);
            if(!hasReturnValue()) {
                binder.addError("Cannot bind this method as a provider because it does not return a value");
                return;
            }

            install(InjectableMethod.this);

            final ScopedBindingBuilder builder = binder.bind(providedKey).toProvider(asProvider());
            if(scope != null) builder.in(scope);
        }
    };
}
 
Example #4
Source File: Binders.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a Binder for Map<String, T> to anything in the context
 * that keyed to a type that implements T.  Note that this has to be a literal class
 * because runtime reflection can't handle generics.
 * @param binder
 * @param type
 * @return
 */
@SuppressWarnings("unchecked")
  public static <T> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, final Class<T> type) {
	return bindMapToInstancesOf(
			binder,
			(TypeLiteral<Map<String, T>>) TypeLiteral.get(new ParameterizedType() {
  				final Type [] args = new Type[] { String.class, type };
				@Override
				public Type getRawType() {
					return Map.class;
				}

				@Override
				public Type getOwnerType() {
					return null;
				}

				@Override
				public Type[] getActualTypeArguments() {
					return args;
				}
			})
		);
}
 
Example #5
Source File: Binders.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <T> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, TypeLiteral<Map<String, T>> type) {
  	return bindMapToInstancesOf(binder, type, new Function<T,String>() {
  	   @Override
  	   public String apply(T service) {
  	      return Injectors.getServiceName(service);
  	   }
  	});
}
 
Example #6
Source File: ObjectMapperModule.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(Binder binder)
{
  final ScopedBindingBuilder builder = binder.bind(objectMapperKey)
          .toProvider(new ObjectMapperProvider(modulesToInject, modulesToAdd, objectMapper));

  if (scope != null) {
    builder.in(scope);
  }
}
 
Example #7
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindListToInstancesOf(TypeLiteral<List<T>> type, Comparator<T> comparator) {
return Binders.bindListToInstancesOf(this.binder(), type, comparator);
 }
 
Example #8
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor) {
    return toConstructor(constructor, TypeLiteral.get(constructor.getDeclaringClass()));
}
 
Example #9
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ScopedBindingBuilder toProvider(TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType) {
    return toProvider(Key.get(providerType));
}
 
Example #10
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ScopedBindingBuilder toProvider(Class<? extends javax.inject.Provider<? extends T>> providerType) {
    return toProvider(Key.get(providerType));
}
 
Example #11
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ScopedBindingBuilder toProvider(Provider<? extends T> provider) {
    return toProvider((javax.inject.Provider<? extends T>) provider);
}
 
Example #12
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ScopedBindingBuilder to(TypeLiteral<? extends T> implementation) {
    return to(Key.get(implementation));
}
 
Example #13
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ScopedBindingBuilder to(Class<? extends T> implementation) {
    return to(Key.get(implementation));
}
 
Example #14
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindListToInstancesOf(TypeLiteral<List<T>> type) {
return Binders.bindListToInstancesOf(this.binder(), type);
 }
 
Example #15
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindListToInstancesOf(Class<T> containedType, Comparator<T> comparator) {
	return Binders.bindListToInstancesOf(this.binder(), containedType, comparator);
}
 
Example #16
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindListToInstancesOf(Class<T> containedType) {
	return Binders.bindListToInstancesOf(this.binder(), containedType);
}
 
Example #17
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <K, V> ScopedBindingBuilder bindMapToInstancesOf(TypeLiteral<Map<K, V>> type, Function<V, K> keyFunction) {
return Binders.bindMapToInstancesOf(binder(), type, keyFunction);
 }
 
Example #18
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindMapToInstancesOf(TypeLiteral<Map<String, T>> type) {
return Binders.bindMapToInstancesOf(this.binder(), type);
 }
 
Example #19
Source File: AbstractIrisModule.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected <T> ScopedBindingBuilder bindMapToInstancesOf(Class<T> containedType) {
	return Binders.bindMapToInstancesOf(this.binder(), containedType);
}
 
Example #20
Source File: Binders.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, TypeLiteral<List<T>> type, Comparator<T> comparator) {
  	Class<T> containedType = extractContainedListType(type);
	return binder
				.bind(type)
				.toProvider(new ListProvider<T>(containedType, comparator));
}
 
Example #21
Source File: Binders.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, TypeLiteral<List<T>> type) {
	return bindListToInstancesOf(binder, type, null);
}
 
Example #22
Source File: Binders.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, Class<T> type) {
	return bindListToInstancesOf(binder, type, null);
}
 
Example #23
Source File: Binders.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <K, V> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, TypeLiteral<Map<K, V>> type, Function<V, K> keyFunction) {
  	Class<V> containedType = extractContainedMapType(type);
	return binder
				.bind(type)
				.toProvider(new MapProvider<K, V>(containedType, keyFunction));
}
 
Example #24
Source File: GuiceBindingsModule.java    From dropwizard-guicey with MIT License 3 votes vote down vote up
/**
 * Important moment: request scoped jersey objects must be bound to guice request scope (if guice web used)
 * because otherwise scope delegation to other thread will not work
 * (see {@link com.google.inject.servlet.ServletScopes#transferRequest(java.util.concurrent.Callable)}).
 * <p>
 * WARNING: bean instance must be obtained in current (request) thread in order to be us used later
 * inside transferred thread (simply call {@code provider.get()} (for jersey-managed bean like {@link UriInfo})
 * before {@code ServletScopes.transferRequest()}.
 *
 * @param type   jersey type to bind
 * @param global true for global type binding
 */
private void jerseyToGuiceBinding(final Class<?> type, final boolean global) {
    final ScopedBindingBuilder binding = bindJerseyComponent(binder(), provider, type);
    if (!global && guiceServletSupport) {
        binding.in(RequestScoped.class);
    }
}
 
Example #25
Source File: PolyBind.java    From druid-api with Apache License 2.0 3 votes vote down vote up
/**
 * Sets up a "choice" for the injector to resolve at injection time.
 *
 * @param binder the binder for the injector that is being configured
 * @param property the property that will be checked to determine the implementation choice
 * @param interfaceKey the interface that will be injected using this choice
 * @param defaultKey the default instance to be injected if the property doesn't match a choice.  Can be null
 * @param <T> interface type
 * @return A ScopedBindingBuilder so that scopes can be added to the binding, if required.
 */
public static <T> ScopedBindingBuilder createChoice(
    Binder binder,
    String property,
    Key<T> interfaceKey,
    Key<? extends T> defaultKey
)
{
  return createChoiceWithDefault(binder, property, interfaceKey, defaultKey, null);
}
 
Example #26
Source File: PolyBind.java    From druid-api with Apache License 2.0 3 votes vote down vote up
/**
 * Sets up a "choice" for the injector to resolve at injection time.
 *
 * @param binder the binder for the injector that is being configured
 * @param property the property that will be checked to determine the implementation choice
 * @param interfaceKey the interface that will be injected using this choice
 * @param defaultKey the default instance to be injected if the property doesn't match a choice.  Can be null
 * @param defaultPropertyValue the default property value to use if the property is not set.
 * @param <T> interface type
 * @return A ScopedBindingBuilder so that scopes can be added to the binding, if required.
 */
public static <T> ScopedBindingBuilder createChoiceWithDefault(
    Binder binder,
    String property,
    Key<T> interfaceKey,
    Key<? extends T> defaultKey,
    String defaultPropertyValue
)
{
  return binder.bind(interfaceKey).toProvider(new ConfiggedProvider<T>(interfaceKey, property, defaultKey, defaultPropertyValue));
}
 
Example #27
Source File: JerseyBinding.java    From dropwizard-guicey with MIT License 2 votes vote down vote up
/**
 * Used to bind jersey beans in guice context (lazily). Guice context is started first, so there is
 * no way to bind instances. Instead "lazy bridge" installed, which will resolve target type on first call.
 * Guice is not completely started and direct injector lookup is impossible here, so lazy injector provider used.
 *
 * @param binder   guice binder
 * @param provider provider for guice injector
 * @param type     jersey type to register
 * @param <T>      type
 * @return scoped binder object to optionally define binding scope.
 * @see ru.vyarus.dropwizard.guice.injector.lookup.InjectorProvider
 */
public static <T> ScopedBindingBuilder bindJerseyComponent(final Binder binder, final Provider<Injector> provider,
                                                           final Class<T> type) {
    return binder.bind(type).toProvider(new JerseyComponentProvider<>(provider, type));
}