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