Search in sources :

Example 31 with DynamicConfiguration

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

the class TestServiceLocator method initialize.

/**
 * This method initializes the service locator with services from the given list
 * of packages (in "." format) and with the set of classes given.
 *
 * @param name         The name of the service locator to create.  If there is already a
 *                     service locator of this name then the remaining fields will be ignored and the existing
 *                     locator with this name will be returned.  May not be null
 * @param packages     The list of packages (in "." format, i.e. "com.acme.test.services") that
 *                     we should hunt through the classpath for in order to find services.  If null this is considered
 *                     to be the empty set
 * @param clazzes      A set of classes that should be analyzed as services, whether they declare
 *                     @Service or not.  If null this is considered to be the empty set
 * @param excludes     A set of implementations that should be excluded from being added.  This list is
 *                     NOT checked against the clazzes list (the explicit include wins), but instead against the set of
 *                     things coming from packages or from the hk2-locator/default file
 * @param locatorFiles A set of locator inhabitant files to search the classpath for to load.  If
 *                     this value is null then only META-INF/hk2-locator/default files on the classpath will be searched.
 *                     If this value is an empty set then no inhabitant files will be loaded.  If this value contains
 *                     values those will be searched as resources from the jars in the classpath to load the registry with
 */
public void initialize(String name, List<String> packages, List<Class<?>> clazzes, Set<String> excludes, Set<String> locatorFiles) {
    if (name == null)
        name = testClass.getName();
    if (packages == null)
        packages = getDefaultPackages();
    if (clazzes == null)
        clazzes = getDefaultClazzes();
    if (excludes == null)
        excludes = getDefaultExcludes();
    if (locatorFiles == null)
        locatorFiles = getDefaultLocatorFiles();
    ServiceLocator found = ServiceLocatorFactory.getInstance().find(name);
    if (found != null) {
        serviceLocator = found;
        serviceLocator.inject(test);
        return;
    }
    serviceLocator = ServiceLocatorFactory.getInstance().create(name);
    ServiceLocatorUtilities.addClasses(serviceLocator, ErrorServiceImpl.class);
    final JustInTimeInjectionResolverImpl jitResolver = new JustInTimeInjectionResolverImpl(excludes);
    serviceLocator.inject(jitResolver);
    ServiceLocatorUtilities.addOneConstant(serviceLocator, jitResolver);
    DynamicConfigurationService dcs = serviceLocator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    addServicesFromDefault(config, excludes, locatorFiles);
    addServicesFromPackage(config, packages, excludes);
    for (Class<?> clazz : clazzes) {
        config.addActiveDescriptor(clazz);
    }
    config.commit();
    serviceLocator.inject(test);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 32 with DynamicConfiguration

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

the class HK2TestListenerAdapter method initializeServiceLocator.

private static void initializeServiceLocator(ServiceLocator locator, HK2 hk2) {
    if (hk2.enableImmediate()) {
        ServiceLocatorUtilities.enableImmediateScope(locator);
    }
    if (hk2.enablePerThread()) {
        ServiceLocatorUtilities.enablePerThreadScope(locator);
    }
    if (hk2.enableInheritableThread()) {
        ServiceLocatorUtilities.enableInheritableThreadScope(locator);
    }
    if (hk2.enableLookupExceptions()) {
        ServiceLocatorUtilities.enableLookupExceptions(locator);
    } else {
        final DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
        final DynamicConfiguration cfg = dcs.createDynamicConfiguration();
        cfg.bind(BuilderHelper.createDescriptorFromClass(IgnoringErrorService.class));
        cfg.commit();
    }
    if (hk2.enableEvents()) {
        ExtrasUtilities.enableTopicDistribution(locator);
    }
}
Also used : IgnoringErrorService(org.glassfish.hk2.utilities.IgnoringErrorService) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 33 with DynamicConfiguration

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

the class AbstractOSGiModulesRegistryImpl method parseInhabitants.

@Override
public List<ActiveDescriptor> parseInhabitants(HK2Module module, String name, ServiceLocator serviceLocator, List<PopulatorPostProcessor> postProcessors) throws IOException, BootException {
    OSGiModuleImpl osgiModuleImpl = (OSGiModuleImpl) module;
    List<ActiveDescriptor> activeDescriptors;
    Map<String, List<Descriptor>> descriptorMap = module.getModuleDefinition().getMetadata().getDescriptors();
    List<Descriptor> descriptors = descriptorMap.get(name);
    if (descriptors == null) {
        activeDescriptors = osgiModuleImpl.parseInhabitants(name, serviceLocator, postProcessors);
        if (activeDescriptors != null) {
            // use the copy constructor to create (nonactive) descriptor for serialization into the cache
            descriptors = new ArrayList<>();
            for (Descriptor d : activeDescriptors) {
                descriptors.add(new DescriptorImpl(d));
            }
            module.getModuleDefinition().getMetadata().addDescriptors(name, descriptors);
        }
    } else {
        activeDescriptors = new ArrayList<>();
        DynamicConfiguration dcs = createDynamicConfiguration(serviceLocator);
        for (Descriptor descriptor : descriptors) {
            DescriptorImpl di = (descriptor instanceof DescriptorImpl) ? (DescriptorImpl) descriptor : new DescriptorImpl(descriptor);
            // set the hk2loader
            DescriptorImpl descriptorImpl = new OsgiPopulatorPostProcessor(osgiModuleImpl).process(serviceLocator, di);
            if (descriptorImpl != null) {
                activeDescriptors.add(dcs.bind(descriptorImpl, false));
            }
        }
        dcs.commit();
    }
    return activeDescriptors;
}
Also used : ServiceLocatorUtilities.createDynamicConfiguration(org.glassfish.hk2.utilities.ServiceLocatorUtilities.createDynamicConfiguration) DescriptorImpl(org.glassfish.hk2.utilities.DescriptorImpl)

Example 34 with DynamicConfiguration

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

the class Utilities method createSpringTestLocator.

/**
 * Creates a ServiceLocator with the SpringBridge initialized
 *
 * @param xmlFileName The name of the spring configuration file
 * @param uniqueName The name that the service locator should have
 * @param classes Classes to add to the locator
 * @return An anonymous service locator with the given classes as services
 */
public static LocatorAndContext createSpringTestLocator(String xmlFileName, String uniqueName, Class<?>... classes) {
    ServiceLocator locator = ServiceLocatorFactory.getInstance().create(uniqueName);
    SpringBridge.getSpringBridge().initializeSpringBridge(locator);
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    for (Class<?> clazz : classes) {
        config.addActiveDescriptor(clazz);
    }
    config.commit();
    // Now setup the bridge
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(xmlFileName);
    SpringIntoHK2Bridge bridge = locator.getService(SpringIntoHK2Bridge.class);
    bridge.bridgeSpringBeanFactory(context);
    return new LocatorAndContext(locator, context);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpringIntoHK2Bridge(org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge)

Example 35 with DynamicConfiguration

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

the class DynamicChangeInfo method startOrContinueChange.

/**
 * Write lock MUST be held!
 *
 * @return
 */
public XmlDynamicChange startOrContinueChange(BaseHK2JAXBBean participant) {
    changeDepth++;
    if (participant != null) {
        participants.add(participant);
    }
    if (dynamicChange != null)
        return dynamicChange;
    globalSuccess = true;
    DynamicConfiguration change = null;
    DynamicConfiguration systemChange = null;
    if (dynamicService != null) {
        systemChange = dynamicService.createDynamicConfiguration();
        if (advertiseInLocator) {
            change = systemChange;
        }
    }
    WriteableBeanDatabase wbd = null;
    if (hub != null && advertiseInHub) {
        wbd = hub.getWriteableDatabaseCopy();
        wbd.setCommitMessage(new XmlHubCommitMessage() {
        });
        if (systemChange != null) {
            systemChange.registerTwoPhaseResources(wbd.getTwoPhaseResource());
        // Now the hub transaction is tied to the registry transaction
        }
    }
    dynamicChange = new XmlDynamicChange(wbd, change, systemChange);
    return dynamicChange;
}
Also used : XmlHubCommitMessage(org.glassfish.hk2.xml.api.XmlHubCommitMessage) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) 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