Search in sources :

Example 16 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project jersey by eclipse-ee4j.

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 17 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.

the class AliasDescriptorTest method testRemoveAliasDescriptorsWithFilterDescriptor.

/**
 * Tests removing alias descriptors to the system (via Filter)
 *
 * @throws Exception
 */
@Test
public void testRemoveAliasDescriptorsWithFilterDescriptor() throws Exception {
    ServiceLocator locator = ServiceLocatorFactory.getInstance().create("testRemoveAliasDescriptorsOriginal");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    Descriptor originalDescriptor = BuilderHelper.link(MyService.class).to(MyInterface1.class).in(Singleton.class.getName()).build();
    ActiveDescriptor<MyService> descriptor = (ActiveDescriptor<MyService>) config.<MyService>bind(originalDescriptor);
    config.commit();
    config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(new AliasDescriptor<MyService>(locator, descriptor, MyInterface2.class.getName(), "foo"));
    config.addActiveDescriptor(new AliasDescriptor<MyService>(locator, descriptor, MyInterface3.class.getName(), "bar"));
    config.commit();
    {
        MyInterface2 s2 = locator.getService(MyInterface2.class, "foo");
        Assert.assertNotNull(s2);
        MyInterface3 s3 = locator.getService(MyInterface3.class, "bar");
        Assert.assertNotNull(s3);
    }
    // Should remove the alias descriptors as well
    ServiceLocatorUtilities.removeFilter(locator, BuilderHelper.createContractFilter(MyInterface1.class.getName()), true);
    {
        MyInterface2 s2 = locator.getService(MyInterface2.class, "foo");
        Assert.assertNull(s2);
        MyInterface3 s3 = locator.getService(MyInterface3.class, "bar");
        Assert.assertNull(s3);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) AliasDescriptor(org.glassfish.hk2.utilities.AliasDescriptor) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Descriptor(org.glassfish.hk2.api.Descriptor) Test(org.junit.Test)

Example 18 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.

the class AliasDescriptorTest method testRemoveAliasDescriptors.

/**
 * Tests removing alias descriptors to the system
 *
 * @throws Exception
 */
@Test
public void testRemoveAliasDescriptors() throws Exception {
    ServiceLocator locator = ServiceLocatorFactory.getInstance().create("testRemoveAliasDescriptors");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    ActiveDescriptor<MyService> descriptor = (ActiveDescriptor<MyService>) config.<MyService>bind(BuilderHelper.link(MyService.class).to(MyInterface1.class).in(Singleton.class.getName()).build());
    config.commit();
    config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(new AliasDescriptor<MyService>(locator, descriptor, MyInterface2.class.getName(), "foo"));
    config.addActiveDescriptor(new AliasDescriptor<MyService>(locator, descriptor, MyInterface3.class.getName(), "bar"));
    config.commit();
    {
        MyInterface2 s2 = locator.getService(MyInterface2.class, "foo");
        Assert.assertNotNull(s2);
        MyInterface3 s3 = locator.getService(MyInterface3.class, "bar");
        Assert.assertNotNull(s3);
    }
    // Should remove the alias descriptors as well
    ServiceLocatorUtilities.removeOneDescriptor(locator, descriptor, true);
    {
        MyInterface2 s2 = locator.getService(MyInterface2.class, "foo");
        Assert.assertNull(s2);
        MyInterface3 s3 = locator.getService(MyInterface3.class, "bar");
        Assert.assertNull(s3);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Test(org.junit.Test)

Example 19 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.

the class AliasDescriptorTest method testCreate.

@Test
public void testCreate() throws Exception {
    ServiceLocator locator = ServiceLocatorFactory.getInstance().create("testCreate");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    ActiveDescriptor<MyService> descriptor = (ActiveDescriptor<MyService>) config.<MyService>bind(BuilderHelper.link(MyService.class).to(MyInterface1.class).in(Singleton.class.getName()).build());
    AliasDescriptor<MyService> aliasDescriptor = new AliasDescriptor<MyService>(locator, descriptor, MyInterface2.class.getName(), "foo");
    config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(aliasDescriptor);
    config.commit();
    MyService s1 = locator.getService(descriptor, null);
    MyService s2 = aliasDescriptor.create(null);
    assertSame(s1, s2);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) AliasDescriptor(org.glassfish.hk2.utilities.AliasDescriptor) Test(org.junit.Test)

Example 20 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.

the class ClassAnalysisTest method testLongestConstructorWithNoZeroArgConstructor.

/**
 * This test also ensures that the analyzer field of Service
 * is honored by the automatic analysis in addActiveDescriptor
 */
@Test
public void testLongestConstructorWithNoZeroArgConstructor() {
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(ServiceWithNoValidHK2Constructor.class);
    config.commit();
    ServiceWithNoValidHK2Constructor service = locator.getService(ServiceWithNoValidHK2Constructor.class);
    service.check();
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) Test(org.junit.Test)

Aggregations

DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)140 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)91 Test (org.junit.Test)72 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)55 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)29 MultiException (org.glassfish.hk2.api.MultiException)18 Singleton (jakarta.inject.Singleton)14 List (java.util.List)12 Descriptor (org.glassfish.hk2.api.Descriptor)9 Filter (org.glassfish.hk2.api.Filter)9 DescriptorImpl (org.glassfish.hk2.utilities.DescriptorImpl)9 Type (java.lang.reflect.Type)8 LinkedList (java.util.LinkedList)8 Factory (org.glassfish.hk2.api.Factory)8 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)8 WriteableBeanDatabase (org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase)8 AliasDescriptor (org.glassfish.hk2.utilities.AliasDescriptor)7 ArrayList (java.util.ArrayList)6 FactoryDescriptors (org.glassfish.hk2.api.FactoryDescriptors)6 BaseHK2JAXBBean (org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean)5