Search in sources :

Example 11 with InjectionManager

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

the class InjectionManagerFactory method createInjectionManager.

/**
     * Create new initialized server injection manager.
     *
     * @param applicationProperties map of application-specific properties.
     * @return new initialized server injection manager.
     */
public static InjectionManager createInjectionManager(Map<String, Object> applicationProperties) {
    InjectionManager injectionManager = Injections.createInjectionManager();
    injectionManager.register(new ServerBinder(applicationProperties, injectionManager));
    return injectionManager;
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager)

Example 12 with InjectionManager

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

the class CustomInjectablesApplicationTest method plainHK2DynamicTest.

@Test
public void plainHK2DynamicTest() throws Exception {
    InjectionManager injectionManager = Injections.createInjectionManager(new RequestScope.Binder());
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bindAsContract(MyInjectablePerRequest.class).in(RequestScoped.class);
            bindAsContract(MyInjectableSingleton.class).in(Singleton.class);
        }
    };
    injectionManager.register(binder);
    final RequestScope requestScope = injectionManager.getInstance(RequestScope.class);
    final MyInjectableSingleton myInjectableSingleton = injectionManager.getInstance(MyInjectableSingleton.class);
    assertEquals(myInjectableSingleton, injectionManager.getInstance(MyInjectableSingleton.class));
    final MyInjectablePerRequest myInjectablePerRequest = requestScope.runInScope(new Callable<MyInjectablePerRequest>() {

        @Override
        public MyInjectablePerRequest call() throws Exception {
            final MyInjectablePerRequest myInjectablePerRequest = injectionManager.getInstance(MyInjectablePerRequest.class);
            assertEquals(myInjectablePerRequest, injectionManager.getInstance(MyInjectablePerRequest.class));
            return myInjectablePerRequest;
        }
    });
    requestScope.runInScope(new Runnable() {

        @Override
        public void run() {
            assertNotSame(myInjectablePerRequest, injectionManager.getInstance(MyInjectablePerRequest.class));
        }
    });
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 13 with InjectionManager

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

the class CustomInjectablesApplicationTest method plainHK2Test.

@Test
public void plainHK2Test() throws Exception {
    final InjectionManager locator = Injections.createInjectionManager(new RequestScope.Binder(), new AbstractBinder() {

        @Override
        protected void configure() {
            bindAsContract(MyInjectablePerRequest.class).in(RequestScoped.class);
            bindAsContract(MyInjectableSingleton.class).in(Singleton.class);
        }
    });
    final RequestScope requestScope = locator.getInstance(RequestScope.class);
    final MyInjectableSingleton myInjectableSingleton = locator.getInstance(MyInjectableSingleton.class);
    assertEquals(myInjectableSingleton, locator.getInstance(MyInjectableSingleton.class));
    final MyInjectablePerRequest myInjectablePerRequest = requestScope.runInScope(new Callable<MyInjectablePerRequest>() {

        @Override
        public MyInjectablePerRequest call() throws Exception {
            final MyInjectablePerRequest myInjectablePerRequest = locator.getInstance(MyInjectablePerRequest.class);
            assertEquals(myInjectablePerRequest, locator.getInstance(MyInjectablePerRequest.class));
            return myInjectablePerRequest;
        }
    });
    requestScope.runInScope(new Runnable() {

        @Override
        public void run() {
            assertNotSame(myInjectablePerRequest, locator.getInstance(MyInjectablePerRequest.class));
        }
    });
}
Also used : Singleton(javax.inject.Singleton) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) RequestScoped(org.glassfish.jersey.process.internal.RequestScoped) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 14 with InjectionManager

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

the class CommonConfigTest method testConfigureFeatureInstancesProviderInstances.

@Test
public void testConfigureFeatureInstancesProviderInstances() throws Exception {
    final InstanceFeatureA f1 = new InstanceFeatureA();
    config.register(f1);
    final InstanceFeatureA f2 = new InstanceFeatureA(true);
    config.register(f2);
    InjectionManager injectionManager = Injections.createInjectionManager();
    config.configureMetaProviders(injectionManager);
    assertTrue(config.getConfiguration().isEnabled(f1));
    assertFalse(config.getConfiguration().isEnabled(f2));
    final Set<Object> providerInstances = config.getInstances();
    assertEquals(2, providerInstances.size());
    final Set<Object> pureProviderInstances = config.getComponentBag().getInstances(ComponentBag.excludeMetaProviders(injectionManager));
    assertEquals(1, pureProviderInstances.size());
    int a = 0;
    int b = 0;
    for (final Object instance : pureProviderInstances) {
        if (instance instanceof CustomReaderA) {
            a++;
        } else {
            b++;
        }
    }
    assertEquals(1, a);
    assertEquals(0, b);
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 15 with InjectionManager

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

the class CommonConfigTest method testFeatureInjectionsBindInFeature.

@Test
@Ignore
public void testFeatureInjectionsBindInFeature() throws Exception {
    config.register(new BindInjectMeInFeature());
    config.register(InjectIntoFeatureClass.class);
    config.register(new InjectIntoFeatureInstance());
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.configureMetaProviders(injectionManager);
    assertThat("Feature instance not injected", config.getProperty("instance-injected").toString(), is("true"));
    assertThat("Feature class not injected", config.getProperty("class-injected").toString(), is("true"));
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)50 Test (org.junit.Test)41 WadlGenerator (org.glassfish.jersey.server.wadl.WadlGenerator)10 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)8 ProviderBinder (org.glassfish.jersey.internal.inject.ProviderBinder)7 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)5 File (java.io.File)4 URL (java.net.URL)4 Properties (java.util.Properties)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)4 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)4 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Function (java.util.function.Function)2