Search in sources :

Example 11 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class Hk2Helper method bind.

/**
     * Bind descriptors to Hk2-like {@link Binder}.
     *
     * @param locator     HK2 locator.
     * @param descriptors collection of descriptors.
     */
static void bind(ServiceLocator locator, Iterable<Binding> descriptors) {
    DynamicConfiguration dc = getDynamicConfiguration(locator);
    for (Binding binding : descriptors) {
        bindBinding(locator, dc, binding);
    }
    dc.commit();
}
Also used : SupplierClassBinding(org.glassfish.jersey.internal.inject.SupplierClassBinding) AliasBinding(org.glassfish.jersey.internal.inject.AliasBinding) ClassBinding(org.glassfish.jersey.internal.inject.ClassBinding) InstanceBinding(org.glassfish.jersey.internal.inject.InstanceBinding) Binding(org.glassfish.jersey.internal.inject.Binding) SupplierInstanceBinding(org.glassfish.jersey.internal.inject.SupplierInstanceBinding) InjectionResolverBinding(org.glassfish.jersey.internal.inject.InjectionResolverBinding) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 12 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class InjectionManagerTest method testServiceLocatorParent.

@Test
public void testServiceLocatorParent() {
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bindAsContract(EnglishGreeting.class);
        }
    };
    ServiceLocator parentLocator = ServiceLocatorUtilities.bind(binder);
    InjectionManager injectionManager = new HK2InjectionManager();
    injectionManager.initialize(parentLocator);
    assertNotNull(injectionManager.getInstance(EnglishGreeting.class));
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 13 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator 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 14 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project graylog2-server by Graylog2.

the class Graylog2ServiceLocatorGenerator method create.

@Override
public ServiceLocator create(String name, ServiceLocator parent) {
    final ServiceLocator serviceLocator = super.create(name, parent);
    final Injector injector = GuiceInjectorHolder.getInjector().createChildInjector(new HK2IntoGuiceBridge(serviceLocator));
    GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
    final GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
    guiceBridge.bridgeGuiceInjector(injector);
    return serviceLocator;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) GuiceIntoHK2Bridge(org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge) Injector(com.google.inject.Injector) HK2IntoGuiceBridge(org.jvnet.hk2.guice.bridge.api.HK2IntoGuiceBridge)

Example 15 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project API by ca-cwds.

the class JerseyGuiceRule method before.

@Override
protected void before() throws Throwable {
    Injector baseInjector = Guice.createInjector(Stage.PRODUCTION, new ServletModule());
    JerseyGuiceUtils.install(new ServiceLocatorGenerator() {

        @Override
        public ServiceLocator create(String name, ServiceLocator parent) {
            if (!name.startsWith("__HK2_Generated_")) {
                return null;
            }
            return baseInjector.createChildInjector(new JerseyGuiceModule(name)).getInstance(ServiceLocator.class);
        }
    });
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ServiceLocatorGenerator(org.glassfish.hk2.extension.ServiceLocatorGenerator) Injector(com.google.inject.Injector) JerseyGuiceModule(com.squarespace.jersey2.guice.JerseyGuiceModule) ServletModule(com.google.inject.servlet.ServletModule)

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