Search in sources :

Example 96 with DynamicConfiguration

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;
}
Also used : Type(java.lang.reflect.Type) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 97 with DynamicConfiguration

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"));
    }
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) MultiException(org.glassfish.hk2.api.MultiException) Test(org.junit.Test)

Example 98 with DynamicConfiguration

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());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Binder(org.glassfish.hk2.utilities.Binder) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ValidationService(org.glassfish.hk2.api.ValidationService) Test(org.junit.Test)

Example 99 with DynamicConfiguration

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

Example 100 with DynamicConfiguration

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();
    }
}
Also used : DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) Instance(org.glassfish.hk2.configuration.hub.api.Instance) WriteableType(org.glassfish.hk2.configuration.hub.api.WriteableType) WriteableBeanDatabase(org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase)

Aggregations

DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)142 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)93 Test (org.junit.Test)72 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)56 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 Factory (org.glassfish.hk2.api.Factory)8 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)8 WriteableBeanDatabase (org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase)8 LinkedList (java.util.LinkedList)7 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