use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.
the class SimpleServiceJITResolver method justInTimeResolution.
/* (non-Javadoc)
* @see org.glassfish.hk2.api.JustInTimeInjectionResolver#justInTimeResolution(org.glassfish.hk2.api.Configuration, org.glassfish.hk2.api.Injectee)
*/
@Override
public boolean justInTimeResolution(Injectee failedInjectionPoint) {
DynamicConfiguration configuration = dcs.createDynamicConfiguration();
Type injectionType = failedInjectionPoint.getRequiredType();
if (!(injectionType instanceof Class))
return false;
Class<?> injectionClass = (Class<?>) injectionType;
if (!SimpleService.class.equals(injectionClass))
return false;
configuration.bind(BuilderHelper.link(SimpleService.class).build());
configuration.commit();
numTimesCalled++;
return true;
}
use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.
the class ValidatingTest method testBindValidationFailure.
/**
* Tests a bind that should fail validation
*/
@Test
public void testBindValidationFailure() {
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration configuration = dcs.createDynamicConfiguration();
configuration.bind(BuilderHelper.link(NeverBindMeService.class).build());
try {
configuration.commit();
Assert.fail("Bind should have failed due to validation problem");
} catch (MultiException me) {
Assert.assertTrue(me.getMessage(), me.getMessage().contains(" did not pass the BIND validation"));
}
}
use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.
the class ValidatingTest method testStateBasedValidation.
/**
* Tests a state based validation using getService
*/
@Test
public void testStateBasedValidation() {
// Overrides global one
ServiceLocator locator = LocatorHelper.create("ValidationTest_2", null);
ServiceLocatorUtilities.bind(locator, new Binder() {
@Override
public void bind(DynamicConfiguration config) {
config.bind(BuilderHelper.link(DynamicServiceImpl1.class.getName()).to(DynamicService.class.getName()).in(Singleton.class.getName()).build());
config.bind(BuilderHelper.link(DynamicServiceImpl2.class.getName()).to(DynamicService.class.getName()).in(Singleton.class.getName()).build());
config.bind(BuilderHelper.link(StateBasedValidationService.class.getName()).to(ValidationService.class.getName()).in(Singleton.class.getName()).build());
}
});
// First get one
StateBasedValidationService sbvs = (StateBasedValidationService) locator.getService(ValidationService.class);
Assert.assertNotNull(sbvs);
sbvs.setCurrentState(1);
DynamicService d1 = locator.getService(DynamicService.class);
Assert.assertNotNull(d1);
Assert.assertEquals(1, d1.getImplNumber());
// Now switch the state to state 2
sbvs.setCurrentState(2);
DynamicService d2 = locator.getService(DynamicService.class);
Assert.assertNotNull(d2);
Assert.assertEquals(2, d2.getImplNumber());
}
use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.
the class LocatorHelper method getServiceLocator.
/**
* Creates a ServiceLocator equipped with a RunLevelService and the set of
* classes given
*
* @param classes
* The set of classes to also add to the descriptor (should
* probably contain some run level services, right?)
* @return The ServiceLocator to use
*/
@SuppressWarnings("unchecked")
public static ServiceLocator getServiceLocator(Class<?>... classes) {
ServiceLocator locator = ServiceLocatorFactory.getInstance().create(null);
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration config = dcs.createDynamicConfiguration();
for (Class<?> clazz : classes) {
if (Factory.class.isAssignableFrom(clazz)) {
Class<? extends Factory<Object>> fClass = (Class<? extends Factory<Object>>) clazz;
config.addActiveFactoryDescriptor(fClass);
} else {
config.addActiveDescriptor(clazz);
}
}
config.commit();
return locator;
}
use of org.glassfish.hk2.api.DynamicConfiguration in project glassfish-hk2 by eclipse-ee4j.
the class HubTestBase method addTypeAndInstance.
protected void addTypeAndInstance(String typeName, String instanceKey, Object instanceValue, boolean asResource) {
WriteableBeanDatabase wbd = hub.getWriteableDatabaseCopy();
WriteableType wt = wbd.findOrAddWriteableType(typeName);
Instance added = wt.addInstance(instanceKey, instanceValue);
Assert.assertNotNull(added);
if (asResource) {
DynamicConfiguration dc = dcs.createDynamicConfiguration();
dc.registerTwoPhaseResources(wbd.getTwoPhaseResource());
dc.commit();
} else {
wbd.commit();
}
}
Aggregations