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());
}
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;
}
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);
}
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;
}
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);
}
Aggregations