Search in sources :

Example 6 with AbstractBinder

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

the class SaxParserFactoryInjectionProviderTest method createInjectionManager.

public static InjectionManager createInjectionManager(Binder... customBinders) {
    Binder[] binders = new Binder[customBinders.length + 2];
    binders[0] = new AbstractBinder() {

        @Override
        protected void configure() {
            bindFactory(() -> EMPTY_CONFIG).to(Configuration.class);
            bindFactory(SaxParserFactoryInjectionProvider.class, Singleton.class).to(SAXParserFactory.class).in(PerThread.class);
            bindAsContract(MySPFProvider.class).in(Singleton.class);
        }
    };
    System.arraycopy(customBinders, 0, binders, 2, customBinders.length);
    return Injections.createInjectionManager(binders);
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) PerThread(org.glassfish.jersey.internal.inject.PerThread) Configuration(javax.ws.rs.core.Configuration) Singleton(javax.inject.Singleton) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder)

Example 7 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder 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 8 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder 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 9 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder 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 10 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder in project dropwizard by dropwizard.

the class DropwizardResourceConfigTest method logEndpointBinder.

@Test
void logEndpointBinder() {
    rc.register(ResourceInterface.class);
    rc.register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(ImplementingResource.class).to(ResourceInterface.class);
        }
    });
    runJersey();
    final String expectedLog = String.format("The following paths were found for the configured resources:%n" + "%n" + "    GET     /another (io.dropwizard.jersey.DropwizardResourceConfigTest.ResourceInterface)");
    assertThat(rc.getEndpointsInfo()).contains(expectedLog);
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)19 Test (org.junit.Test)7 Singleton (javax.inject.Singleton)5 Binder (org.glassfish.jersey.internal.inject.Binder)4 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 Configuration (javax.ws.rs.core.Configuration)3 ArrayList (java.util.ArrayList)2 Response (javax.ws.rs.core.Response)2 Binding (org.glassfish.jersey.internal.inject.Binding)2 ClassBinding (org.glassfish.jersey.internal.inject.ClassBinding)2 RequestScope (org.glassfish.jersey.process.internal.RequestScope)2 HeaderDelegateProvider (org.glassfish.jersey.spi.HeaderDelegateProvider)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 ServletContext (javax.servlet.ServletContext)1 RuntimeType (javax.ws.rs.RuntimeType)1 ClassAnalyzer (org.glassfish.hk2.api.ClassAnalyzer)1 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)1 ServiceFinderBinder (org.glassfish.jersey.internal.ServiceFinderBinder)1 PerThread (org.glassfish.jersey.internal.inject.PerThread)1