Search in sources :

Example 6 with ServiceLocator

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);
}
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 7 with ServiceLocator

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);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HappyService(org.glassfish.jersey.tests.integration.jersey2704.services.HappyService)

Example 8 with ServiceLocator

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());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 9 with ServiceLocator

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();
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 10 with ServiceLocator

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();
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ServiceLocatorRuntimeBean(org.jvnet.hk2.external.runtime.ServiceLocatorRuntimeBean)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)12 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)4 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)3 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)3 Test (org.junit.Test)3 Injector (com.google.inject.Injector)2 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 InjectionResolverBinding (org.glassfish.jersey.internal.inject.InjectionResolverBinding)2 InstanceBinding (org.glassfish.jersey.internal.inject.InstanceBinding)2 SupplierClassBinding (org.glassfish.jersey.internal.inject.SupplierClassBinding)2 SupplierInstanceBinding (org.glassfish.jersey.internal.inject.SupplierInstanceBinding)2 ServletModule (com.google.inject.servlet.ServletModule)1 JerseyGuiceModule (com.squarespace.jersey2.guice.JerseyGuiceModule)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Server (org.eclipse.jetty.server.Server)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 ServiceLocatorGenerator (org.glassfish.hk2.extension.ServiceLocatorGenerator)1