Search in sources :

Example 1 with Binder

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

the class CdiComponentProvider method bindHk2ClassAnalyzer.

private void bindHk2ClassAnalyzer() {
    ClassAnalyzer defaultClassAnalyzer = injectionManager.getInstance(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME);
    int skippedElements = methodsToSkip.size() + fieldsToSkip.size();
    ClassAnalyzer customizedClassAnalyzer = skippedElements > 0 ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip) : defaultClassAnalyzer;
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(customizedClassAnalyzer).analyzeWith(ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME).to(ClassAnalyzer.class).named(CDI_CLASS_ANALYZER);
        }
    };
    injectionManager.register(binder);
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) ClassAnalyzer(org.glassfish.hk2.api.ClassAnalyzer) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint)

Example 2 with Binder

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

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

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

the class SpringComponentProvider method initialize.

@Override
public void initialize(InjectionManager injectionManager) {
    this.injectionManager = injectionManager;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine(LocalizationMessages.CTX_LOOKUP_STARTED());
    }
    ServletContext sc = injectionManager.getInstance(ServletContext.class);
    if (sc != null) {
        // servlet container
        ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    } else {
        // non-servlet container
        ctx = createSpringContext();
    }
    if (ctx == null) {
        LOGGER.severe(LocalizationMessages.CTX_LOOKUP_FAILED());
        return;
    }
    LOGGER.config(LocalizationMessages.CTX_LOOKUP_SUCESSFUL());
    // initialize HK2 spring-bridge
    HK2InjectionManager hk2InjectionManager = (HK2InjectionManager) injectionManager;
    SpringBridge.getSpringBridge().initializeSpringBridge(hk2InjectionManager.getServiceLocator());
    SpringIntoHK2Bridge springBridge = injectionManager.getInstance(SpringIntoHK2Bridge.class);
    springBridge.bridgeSpringBeanFactory(ctx);
    // register Spring @Autowired annotation handler with HK2 ServiceLocator
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new AutowiredInjectResolver(ctx)).to(InjectionResolver.class);
            bind(ctx).to(ApplicationContext.class).named("SpringContext");
        }
    };
    injectionManager.register(binder);
    LOGGER.config(LocalizationMessages.SPRING_COMPONENT_PROVIDER_INITIALIZED());
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpringIntoHK2Bridge(org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ServletContext(javax.servlet.ServletContext) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Aggregations

AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)4 Binder (org.glassfish.jersey.internal.inject.Binder)4 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 Singleton (javax.inject.Singleton)1 ServletContext (javax.servlet.ServletContext)1 Configuration (javax.ws.rs.core.Configuration)1 ClassAnalyzer (org.glassfish.hk2.api.ClassAnalyzer)1 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)1 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)1 PerThread (org.glassfish.jersey.internal.inject.PerThread)1 RequestScope (org.glassfish.jersey.process.internal.RequestScope)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 Test (org.junit.Test)1 SpringIntoHK2Bridge (org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1