use of org.glassfish.hk2.api.ServiceLocator 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.hk2.api.ServiceLocator in project jersey by jersey.
the class ServiceLocatorSetup method contextInitialized.
@Override
public void contextInitialized(ServletContextEvent event) {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.addOneConstant(locator, new HappyService());
event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
}
use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.
the class Main method main.
public static void main(String[] args) {
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("myLocator");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bind(HelloServiceImpl.class).to(GreetingService.class);
}
};
locator.inject(binder);
binder.bind(dc);
dc.commit();
GreetingWrapper wrapper = new GreetingWrapper(locator);
System.out.println("result: " + wrapper.getInjectedGreeting());
}
use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.
the class MyServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("myLocator");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bind(HelloServiceImpl.class).to(GreetingService.class);
}
};
binder.bind(dc);
dc.commit();
GreetingWrapper wrapper = new GreetingWrapper(locator);
response.getWriter().write("Greeting: " + wrapper.getInjectedGreeting());
locator.shutdown();
}
use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.
the class HK2InjectionManager method initialize.
@Override
public void initialize(String name, Object parent, Binder... binders) {
ServiceLocator parentLocator = resolveServiceLocatorParent(parent);
this.locator = createLocator(name, parentLocator);
// Register all components needed for proper HK2 locator bootstrap
Hk2Helper.bind(locator, new Hk2BootstrapBinder(this, CompositeBinder.wrap(binders)));
this.locator.setDefaultClassAnalyzerName(JerseyClassAnalyzer.NAME);
// clear HK2 caches
ServiceLocatorRuntimeBean serviceLocatorRuntimeBean = locator.getService(ServiceLocatorRuntimeBean.class);
if (serviceLocatorRuntimeBean != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(LocalizationMessages.HK_2_CLEARING_CACHE(serviceLocatorRuntimeBean.getServiceCacheSize(), serviceLocatorRuntimeBean.getReflectionCacheSize()));
}
serviceLocatorRuntimeBean.clearReflectionCache();
serviceLocatorRuntimeBean.clearServiceCache();
}
}
Aggregations