Search in sources :

Example 1 with HK2InjectionManager

use of org.glassfish.jersey.hk2.HK2InjectionManager in project jersey by jersey.

the class JettyContainerTest method testParentServiceLocator.

/**
     * Test that defined ServiceLocator becomes a parent of the newly created service locator.
     */
@Test
public void testParentServiceLocator() {
    final ServiceLocator locator = new ServiceLocatorImpl("MyServiceLocator", null);
    final Server server = JettyHttpContainerFactory.createServer(URI.create("http://localhost:9876"), new ResourceConfig(Resource.class), false, locator);
    JettyHttpContainer container = (JettyHttpContainer) server.getHandler();
    InjectionManager injectionManager = container.getApplicationHandler().getInjectionManager();
    HK2InjectionManager hk2InjectionManager = (HK2InjectionManager) injectionManager;
    ServiceLocator serviceLocator = hk2InjectionManager.getServiceLocator();
    assertTrue("Application injection manager was expected to have defined parent locator", serviceLocator.getParent() == locator);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Server(org.eclipse.jetty.server.Server) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) JettyHttpContainer(org.glassfish.jersey.jetty.JettyHttpContainer) ServiceLocatorImpl(org.jvnet.hk2.internal.ServiceLocatorImpl) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with HK2InjectionManager

use of org.glassfish.jersey.hk2.HK2InjectionManager in project jersey by jersey.

the class RequestResponseWrapperProvider method getRequestScopedInitializerProvider.

@Override
public RequestScopedInitializerProvider getRequestScopedInitializerProvider() {
    return new RequestScopedInitializerProvider() {

        @Override
        public RequestScopedInitializer get(final RequestContextProvider context) {
            return new RequestScopedInitializer() {

                @Override
                public void initialize(InjectionManager injectionManager) {
                    ServiceLocator locator = ((HK2InjectionManager) injectionManager).getServiceLocator();
                    locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(finalWrap(context.getHttpServletRequest()));
                    locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(finalWrap(context.getHttpServletResponse()));
                }
            };
        }
    };
}
Also used : RequestScopedInitializer(org.glassfish.jersey.server.spi.RequestScopedInitializer) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestScopedInitializerProvider(org.glassfish.jersey.servlet.internal.spi.RequestScopedInitializerProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) RequestContextProvider(org.glassfish.jersey.servlet.internal.spi.RequestContextProvider) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Example 3 with HK2InjectionManager

use of org.glassfish.jersey.hk2.HK2InjectionManager in project jersey by jersey.

the class Injections method _injectionManager.

private static InjectionManager _injectionManager(String name, Object parent, Binder... binders) {
    Iterator<InjectionManager> iterator = ServiceLoader.load(InjectionManager.class).iterator();
    InjectionManager injectionManager;
    if (iterator.hasNext()) {
        injectionManager = iterator.next();
    } else {
        // TODO: Log that there is no explicitly configured InjectionManager, default is used.
        injectionManager = new HK2InjectionManager();
    }
    injectionManager.initialize(name, parent, binders);
    return injectionManager;
}
Also used : HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Example 4 with HK2InjectionManager

use of org.glassfish.jersey.hk2.HK2InjectionManager in project jersey by jersey.

the class BeanParamMemoryLeakTest method testBeanParam.

@Test
public void testBeanParam() throws Exception {
    initiateWebApplication(BeanParamInjectionResource.class);
    final InjectionManager injectionManager = app().getInjectionManager();
    if (!(injectionManager instanceof HK2InjectionManager)) {
        throw new RuntimeException("Bean Manager is not an injection manager");
    }
    HK2InjectionManager hk2BeanManager = (HK2InjectionManager) injectionManager;
    ServiceLocator serviceLocator = hk2BeanManager.getServiceLocator();
    // we do not expect any descriptor registered yet
    assertEquals(0, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // now make one registered via this call
    assertEquals("one", resource("/jaxrs?q=one").getEntity());
    // make sure it got registered
    assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // make another call
    assertEquals("two", resource("/jaxrs?q=two").getEntity());
    assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // and some more
    for (int i = 0; i < 20; i++) {
        assertEquals(Integer.toString(i), resource("/jaxrs?q=" + i).getEntity());
        assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 5 with HK2InjectionManager

use of org.glassfish.jersey.hk2.HK2InjectionManager 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

HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)5 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 Test (org.junit.Test)2 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Server (org.eclipse.jetty.server.Server)1 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)1 Binder (org.glassfish.jersey.internal.inject.Binder)1 JettyHttpContainer (org.glassfish.jersey.jetty.JettyHttpContainer)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 RequestScopedInitializer (org.glassfish.jersey.server.spi.RequestScopedInitializer)1 RequestContextProvider (org.glassfish.jersey.servlet.internal.spi.RequestContextProvider)1 RequestScopedInitializerProvider (org.glassfish.jersey.servlet.internal.spi.RequestScopedInitializerProvider)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 ServiceLocatorImpl (org.jvnet.hk2.internal.ServiceLocatorImpl)1 SpringIntoHK2Bridge (org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1