Search in sources :

Example 1 with GuiceComponentFactory

use of ru.vyarus.dropwizard.guice.module.jersey.support.GuiceComponentFactory in project dropwizard-guicey by xvik.

the class JerseyBinding method bindSpecificComponent.

/**
 * Binds jersey specific component (component implements jersey interface or extends class).
 * Specific binding is required for types directly supported by jersey (e.g. ExceptionMapper).
 * Such types must be bound to target interface directly, otherwise jersey would not be able to resolve them.
 * <p> If type is {@link HK2Managed}, binds directly.
 * Otherwise, use guice "bridge" factory to lazily bind type.</p>
 *
 * @param binder       hk binder
 * @param injector     guice injector
 * @param type         type which implements specific jersey interface or extends class
 * @param specificType specific jersey type (interface or abstract class)
 */
public static void bindSpecificComponent(final AbstractBinder binder, final Injector injector, final Class<?> type, final Class<?> specificType) {
    // resolve generics of specific type
    final GenericsContext context = GenericsResolver.resolve(type).type(specificType);
    final List<Type> genericTypes = context.genericTypes();
    final Type[] generics = genericTypes.toArray(new Type[0]);
    final Type binding = generics.length > 0 ? new ParameterizedTypeImpl(specificType, generics) : specificType;
    if (isHK2Managed(type)) {
        binder.bind(type).to(binding).in(Singleton.class);
    } else {
        // hk cant find different things in different situations, so uniform registration is impossible
        if (InjectionResolver.class.equals(specificType)) {
            binder.bindFactory(new GuiceComponentFactory<>(injector, type)).to(type).in(Singleton.class);
            binder.bind(type).to(binding).in(Singleton.class);
        } else {
            binder.bindFactory(new GuiceComponentFactory<>(injector, type)).to(type).to(binding).in(Singleton.class);
        }
    }
}
Also used : GenericsContext(ru.vyarus.java.generics.resolver.context.GenericsContext) Type(java.lang.reflect.Type) GuiceComponentFactory(ru.vyarus.dropwizard.guice.module.jersey.support.GuiceComponentFactory) ParameterizedTypeImpl(org.glassfish.hk2.utilities.reflection.ParameterizedTypeImpl)

Aggregations

Type (java.lang.reflect.Type)1 ParameterizedTypeImpl (org.glassfish.hk2.utilities.reflection.ParameterizedTypeImpl)1 GuiceComponentFactory (ru.vyarus.dropwizard.guice.module.jersey.support.GuiceComponentFactory)1 GenericsContext (ru.vyarus.java.generics.resolver.context.GenericsContext)1