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);
}
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()));
}
};
}
};
}
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;
}
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());
}
}
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());
}
Aggregations