Search in sources :

Example 1 with Binding

use of org.glassfish.jersey.internal.inject.Binding in project jersey by jersey.

the class AbstractBinderTest method testWithInstall.

@Test
public void testWithInstall() {
    AbstractBinder internalBinder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(MediaTypeProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(NewCookieProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(StringHeaderProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(UriProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
        }
    };
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(CacheControlProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(CookieProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(DateProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(EntityTagProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(LinkProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            install(internalBinder);
        }
    };
    List<Binding> bindings = new ArrayList<>(binder.getBindings());
    assertEquals(9, bindings.size());
    // Keep ordering.
    assertEquals(MediaTypeProvider.class, ((ClassBinding) bindings.get(0)).getService());
    assertEquals(NewCookieProvider.class, ((ClassBinding) bindings.get(1)).getService());
    assertEquals(StringHeaderProvider.class, ((ClassBinding) bindings.get(2)).getService());
    assertEquals(UriProvider.class, ((ClassBinding) bindings.get(3)).getService());
    assertEquals(CacheControlProvider.class, ((ClassBinding) bindings.get(4)).getService());
    assertEquals(CookieProvider.class, ((ClassBinding) bindings.get(5)).getService());
    assertEquals(DateProvider.class, ((ClassBinding) bindings.get(6)).getService());
    assertEquals(EntityTagProvider.class, ((ClassBinding) bindings.get(7)).getService());
    assertEquals(LinkProvider.class, ((ClassBinding) bindings.get(8)).getService());
}
Also used : Binding(org.glassfish.jersey.internal.inject.Binding) ClassBinding(org.glassfish.jersey.internal.inject.ClassBinding) HeaderDelegateProvider(org.glassfish.jersey.spi.HeaderDelegateProvider) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with Binding

use of org.glassfish.jersey.internal.inject.Binding in project jersey by jersey.

the class SpringComponentProvider method bind.

// detect JAX-RS classes that are also Spring @Components.
// register these with HK2 ServiceLocator to manage their lifecycle using Spring.
@Override
public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
    if (ctx == null) {
        return false;
    }
    if (AnnotationUtils.findAnnotation(component, Component.class) != null) {
        String[] beanNames = ctx.getBeanNamesForType(component);
        if (beanNames == null || beanNames.length != 1) {
            LOGGER.severe(LocalizationMessages.NONE_OR_MULTIPLE_BEANS_AVAILABLE(component));
            return false;
        }
        String beanName = beanNames[0];
        Binding binding = Bindings.supplier(new SpringManagedBeanFactory(ctx, injectionManager, beanName)).to(component).to(providerContracts);
        injectionManager.register(binding);
        LOGGER.config(LocalizationMessages.BEAN_REGISTERED(beanNames[0]));
        return true;
    }
    return false;
}
Also used : Binding(org.glassfish.jersey.internal.inject.Binding) Component(org.springframework.stereotype.Component)

Example 3 with Binding

use of org.glassfish.jersey.internal.inject.Binding in project jersey by jersey.

the class TransactionalExceptionInterceptorProvider method bindWaeRestoringExceptionMapper.

private void bindWaeRestoringExceptionMapper() {
    GenericCdiBeanSupplier beanSupplier = new GenericCdiBeanSupplier(TransactionalExceptionMapper.class, injectionManager, beanManager, true);
    Binding binding = Bindings.supplier(beanSupplier).to(ExceptionMapper.class);
    injectionManager.register(binding);
}
Also used : Binding(org.glassfish.jersey.internal.inject.Binding) GenericCdiBeanSupplier(org.glassfish.jersey.ext.cdi1x.internal.GenericCdiBeanSupplier)

Example 4 with Binding

use of org.glassfish.jersey.internal.inject.Binding in project jersey by jersey.

the class EjbComponentProvider method bind.

// ComponentProvider
@SuppressWarnings("unchecked")
@Override
public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine(LocalizationMessages.EJB_CLASS_BEING_CHECKED(component));
    }
    if (injectionManager == null) {
        throw new IllegalStateException(LocalizationMessages.EJB_COMPONENT_PROVIDER_NOT_INITIALIZED_PROPERLY());
    }
    if (!isEjbComponent(component)) {
        return false;
    }
    if (!ejbInterceptorRegistered) {
        registerEjbInterceptor();
    }
    Binding binding = Bindings.supplier(new EjbFactory(component, initialContext, EjbComponentProvider.this)).to(component).to(providerContracts);
    injectionManager.register(binding);
    if (LOGGER.isLoggable(Level.CONFIG)) {
        LOGGER.config(LocalizationMessages.EJB_CLASS_BOUND_WITH_CDI(component));
    }
    return true;
}
Also used : InstanceBinding(org.glassfish.jersey.internal.inject.InstanceBinding) Binding(org.glassfish.jersey.internal.inject.Binding)

Example 5 with Binding

use of org.glassfish.jersey.internal.inject.Binding in project jersey by jersey.

the class JerseyResourceContext method unsafeBindResource.

/**
     * Bind a resource instance in a HK2 context.
     *
     * The bound resource instance is internally cached to make sure any sub-sequent attempts to service the
     * class are silently ignored.
     * <p>
     * WARNING: This version of method is not synchronized as well as the cache is not checked for existing
     * bindings before the resource is bound and cached.
     * </p>
     *
     * @param resource resource instance to be bound.
     * @param providerModel provider model for the resource class. If not {@code null}, the class
     *                      wil be bound as a contract provider too.
     */
public void unsafeBindResource(Object resource, ContractProvider providerModel, InjectionManager injectionManager) {
    Binding binding;
    Class<?> resourceClass = resource.getClass();
    if (providerModel != null) {
        Class<? extends Annotation> scope = providerModel.getScope();
        binding = Bindings.service(resource).to(resourceClass);
        for (Class contract : Providers.getProviderContracts(resourceClass)) {
            binding.addAlias(contract.getName()).in(scope.getName()).qualifiedBy(CustomAnnotationLiteral.INSTANCE);
        }
    } else {
        binding = Bindings.serviceAsContract(resourceClass);
    }
    injectionManager.register(binding);
    bindingCache.add(resourceClass);
}
Also used : Binding(org.glassfish.jersey.internal.inject.Binding) ClassBinding(org.glassfish.jersey.internal.inject.ClassBinding)

Aggregations

Binding (org.glassfish.jersey.internal.inject.Binding)8 ClassBinding (org.glassfish.jersey.internal.inject.ClassBinding)4 ArrayList (java.util.ArrayList)2 GenericCdiBeanSupplier (org.glassfish.jersey.ext.cdi1x.internal.GenericCdiBeanSupplier)2 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)2 InstanceBinding (org.glassfish.jersey.internal.inject.InstanceBinding)2 HeaderDelegateProvider (org.glassfish.jersey.spi.HeaderDelegateProvider)2 Test (org.junit.Test)2 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)1 AliasBinding (org.glassfish.jersey.internal.inject.AliasBinding)1 InjectionResolverBinding (org.glassfish.jersey.internal.inject.InjectionResolverBinding)1 SupplierClassBinding (org.glassfish.jersey.internal.inject.SupplierClassBinding)1 SupplierInstanceBinding (org.glassfish.jersey.internal.inject.SupplierInstanceBinding)1 Component (org.springframework.stereotype.Component)1