Search in sources :

Example 1 with ActiveDescriptor

use of org.glassfish.hk2.api.ActiveDescriptor in project jersey by jersey.

the class Hk2Helper method bindBinding.

/**
     * Binds the single descriptor using an external {@link DynamicConfiguration}.
     *
     * @param locator HK2 injection manager.
     * @param dc      HK2 Dynamic configuration to bind the object.
     * @param binding Jersey descriptor as a holder of information about an injection point.
     */
private static void bindBinding(ServiceLocator locator, DynamicConfiguration dc, Binding<?, ?> binding) {
    if (ClassBinding.class.isAssignableFrom(binding.getClass())) {
        ActiveDescriptor<?> activeDescriptor = translateToActiveDescriptor((ClassBinding<?>) binding);
        bindBinding(locator, dc, activeDescriptor, binding.getAliases());
    } else if (InstanceBinding.class.isAssignableFrom(binding.getClass())) {
        ActiveDescriptor<?> activeDescriptor = translateToActiveDescriptor((InstanceBinding<?>) binding);
        bindBinding(locator, dc, activeDescriptor, binding.getAliases());
    } else if (InjectionResolverBinding.class.isAssignableFrom(binding.getClass())) {
        InjectionResolverBinding resolverDescriptor = (InjectionResolverBinding) binding;
        bindBinding(locator, dc, wrapInjectionResolver(resolverDescriptor), binding.getAliases());
        bindBinding(locator, dc, translateToActiveDescriptor(resolverDescriptor), binding.getAliases());
    } else if (SupplierClassBinding.class.isAssignableFrom(binding.getClass())) {
        bindSupplierClassBinding(locator, (SupplierClassBinding<?>) binding);
    } else if (SupplierInstanceBinding.class.isAssignableFrom(binding.getClass())) {
        bindSupplierInstanceBinding(locator, (SupplierInstanceBinding<?>) binding);
    } else {
        throw new RuntimeException(LocalizationMessages.UNKNOWN_DESCRIPTOR_TYPE(binding.getClass().getSimpleName()));
    }
}
Also used : InstanceBinding(org.glassfish.jersey.internal.inject.InstanceBinding) SupplierInstanceBinding(org.glassfish.jersey.internal.inject.SupplierInstanceBinding) InjectionResolverBinding(org.glassfish.jersey.internal.inject.InjectionResolverBinding) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) AbstractActiveDescriptor(org.glassfish.hk2.utilities.AbstractActiveDescriptor) SupplierClassBinding(org.glassfish.jersey.internal.inject.SupplierClassBinding)

Example 2 with ActiveDescriptor

use of org.glassfish.hk2.api.ActiveDescriptor in project jersey by jersey.

the class ActiveBindingBindingTest method testReq.

@Test
@Ignore("At the time of ignoring this test, ResourceConfig does not support HK2 Binder registering.")
public void testReq() throws Exception {
    // bootstrap the test application
    ResourceConfig myConfig = new ResourceConfig();
    myConfig.register(ReqResource.class);
    myConfig.register(SingletonResource.class);
    final MyRequestDataDescriptor activeDescriptor = new MyRequestDataDescriptor();
    myConfig.register(new AbstractBinder() {

        @Override
        protected void configure() {
            addActiveDescriptor(activeDescriptor);
        }
    });
    initiateWebApplication(myConfig);
    activeDescriptor.injectionManager = app().getInjectionManager();
    // end bootstrap
    String response;
    response = getResponseEntity("/req");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/req/param");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, containsString(PROXY_TAG));
    response = getResponseEntity("/req");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, containsString(PROXY_TAG));
    response = getResponseEntity("/singleton/param");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton/param");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
}
Also used : AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ActiveDescriptor

use of org.glassfish.hk2.api.ActiveDescriptor in project jersey by jersey.

the class ContextInjectionResolverImpl method resolve.

@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
    Type requiredType = injectee.getRequiredType();
    boolean isHk2Factory = ReflectionHelper.isSubClassOf(requiredType, Factory.class);
    Injectee newInjectee;
    if (isHk2Factory) {
        newInjectee = getFactoryInjectee(injectee, ReflectionHelper.getTypeArgument(requiredType, 0));
    } else {
        newInjectee = foreignRequestScopedInjecteeCache.apply(injectee);
    }
    ActiveDescriptor<?> ad = descriptorCache.apply(newInjectee);
    if (ad != null) {
        final ServiceHandle handle = serviceLocator.getServiceHandle(ad, newInjectee);
        if (isHk2Factory) {
            return asFactory(handle);
        } else {
            return handle.getService();
        }
    }
    return null;
}
Also used : GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) Injectee(org.glassfish.hk2.api.Injectee) ServiceHandle(org.glassfish.hk2.api.ServiceHandle)

Example 4 with ActiveDescriptor

use of org.glassfish.hk2.api.ActiveDescriptor in project jersey by jersey.

the class ContextInjectionResolverImpl method resolve.

/**
     * Jersey Injection Resolver method that just populate HK2 injectee object and delegates the processing to HK2 Injection
     * Resolver.
     *
     * @param injectee The injection point this value is being injected into
     * @return result of the injection processing.
     */
@Override
public Object resolve(org.glassfish.jersey.internal.inject.Injectee injectee) {
    InjecteeImpl hk2injectee = new InjecteeImpl() {

        @Override
        public Class<?> getInjecteeClass() {
            return injectee.getInjecteeClass();
        }
    };
    hk2injectee.setRequiredType(injectee.getRequiredType());
    hk2injectee.setRequiredQualifiers(injectee.getRequiredQualifiers());
    if (injectee.getInjecteeDescriptor() != null) {
        hk2injectee.setInjecteeDescriptor((ActiveDescriptor<?>) injectee.getInjecteeDescriptor().get());
    }
    // Delegate the call to HK2 Resolver, Service Handle is not need in the delegated processing.
    return resolve(hk2injectee, null);
}
Also used : InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl)

Aggregations

Type (java.lang.reflect.Type)1 GenericType (javax.ws.rs.core.GenericType)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 Injectee (org.glassfish.hk2.api.Injectee)1 ServiceHandle (org.glassfish.hk2.api.ServiceHandle)1 AbstractActiveDescriptor (org.glassfish.hk2.utilities.AbstractActiveDescriptor)1 InjecteeImpl (org.glassfish.hk2.utilities.InjecteeImpl)1 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)1 InjectionResolverBinding (org.glassfish.jersey.internal.inject.InjectionResolverBinding)1 InstanceBinding (org.glassfish.jersey.internal.inject.InstanceBinding)1 SupplierClassBinding (org.glassfish.jersey.internal.inject.SupplierClassBinding)1 SupplierInstanceBinding (org.glassfish.jersey.internal.inject.SupplierInstanceBinding)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1