Search in sources :

Example 21 with DynamicConfiguration

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

the class ClassAnalysisTest method testLongestConstructorWithValidHK2Constructor.

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

Example 22 with DynamicConfiguration

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

the class ServiceLocatorUtilities method removeOneDescriptor.

/**
 * This method will attempt to remove descriptors matching the passed in descriptor from
 * the given locator.  If the descriptor has its locatorId and serviceId values set then
 * only a descriptor matching those exact locatorId and serviceId will be removed.  Otherwise
 * any descriptor that returns true from the {@link DescriptorImpl#equals(Object)} method
 * will be removed from the locator.  Note that if more than one descriptor matches they
 * will all be removed.  Hence more than one descriptor may be removed by this method.
 *
 * @param locator The non-null locator to remove the descriptor from
 * @param descriptor The non-null descriptor to remove from the locator
 * @param includeAliasDescriptors If set to true all {@link AliasDescriptor}s that point
 * to any descriptors found by filter will also be removed
 */
public static void removeOneDescriptor(ServiceLocator locator, Descriptor descriptor, boolean includeAliasDescriptors) {
    if (locator == null || descriptor == null)
        throw new IllegalArgumentException();
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    if (descriptor.getLocatorId() != null && descriptor.getServiceId() != null) {
        Filter destructionFilter = BuilderHelper.createSpecificDescriptorFilter(descriptor);
        config.addUnbindFilter(destructionFilter);
        if (includeAliasDescriptors == true) {
            List<ActiveDescriptor<?>> goingToDie = locator.getDescriptors(destructionFilter);
            if (!goingToDie.isEmpty()) {
                AliasFilter af = new AliasFilter(goingToDie);
                config.addUnbindFilter(af);
            }
        }
        config.commit();
        return;
    }
    // Must use second algorithm, which is not as precise, but which still mainly works
    final DescriptorImpl di;
    if (descriptor instanceof DescriptorImpl) {
        di = (DescriptorImpl) descriptor;
    } else {
        di = new DescriptorImpl(descriptor);
    }
    Filter destructionFilter = new Filter() {

        @Override
        public boolean matches(Descriptor d) {
            return di.equals(d);
        }
    };
    config.addUnbindFilter(destructionFilter);
    if (includeAliasDescriptors == true) {
        List<ActiveDescriptor<?>> goingToDie = locator.getDescriptors(destructionFilter);
        if (!goingToDie.isEmpty()) {
            AliasFilter af = new AliasFilter(goingToDie);
            config.addUnbindFilter(af);
        }
    }
    config.commit();
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) Filter(org.glassfish.hk2.api.Filter) IndexedFilter(org.glassfish.hk2.api.IndexedFilter) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Descriptor(org.glassfish.hk2.api.Descriptor)

Example 23 with DynamicConfiguration

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

the class ServiceLocatorUtilities method addClasses.

/**
 * It is very often the case that one wishes to add classes that hk2
 * will automatically analyze for contracts and qualifiers to
 * a service locator.  This method adds those classes.
 * <p>
 * If the class to add implements {@link Factory} then two descriptors
 * will be added, one for the {@link Factory} class itself, and one for
 * the {@link Factory#provide()} method of the factory.  In the output
 * list the descriptor for the {@link Factory} will be added first, followed
 * by the descriptor for the {@link Factory#provide()} method
 *
 * @param locator The non-null locator to add this descriptor to
 * @param toAdd The classes to add to the locator.  If a class in this list implements
 * {@link Factory} then two descriptors will be added for that class
 * @return The list of descriptors added to the system.  Will not return null but
 * may return an empty list
 * @throws MultiException On a commit failure.  If idempotent is true the commit failure
 * may be due to duplicate descriptors found in the locator
 */
@SuppressWarnings("unchecked")
public static List<ActiveDescriptor<?>> addClasses(ServiceLocator locator, boolean idempotent, Class<?>... toAdd) {
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    LinkedList<ActiveDescriptor<?>> retVal = new LinkedList<ActiveDescriptor<?>>();
    for (Class<?> addMe : toAdd) {
        if (Factory.class.isAssignableFrom(addMe)) {
            FactoryDescriptors fds = config.addActiveFactoryDescriptor((Class<Factory<Object>>) addMe);
            if (idempotent) {
                config.addIdempotentFilter(BuilderHelper.createDescriptorFilter(fds.getFactoryAsAService(), false));
                config.addIdempotentFilter(BuilderHelper.createDescriptorFilter(fds.getFactoryAsAFactory(), false));
            }
            retVal.add((ActiveDescriptor<?>) fds.getFactoryAsAService());
            retVal.add((ActiveDescriptor<?>) fds.getFactoryAsAFactory());
        } else {
            ActiveDescriptor<?> ad = config.addActiveDescriptor(addMe);
            if (idempotent) {
                config.addIdempotentFilter(BuilderHelper.createDescriptorFilter(ad, false));
            }
            retVal.add(ad);
        }
    }
    config.commit();
    return retVal;
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) FactoryDescriptors(org.glassfish.hk2.api.FactoryDescriptors) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory) Factory(org.glassfish.hk2.api.Factory) LinkedList(java.util.LinkedList)

Example 24 with DynamicConfiguration

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

the class ServiceLocatorUtilities method addFactoryDescriptors.

/**
 * Adds the given factory descriptors to the service locator
 *
 * @param locator The locator to add the factories to.  May not be null
 * @param requiresDeepCopy This is false ONLY if every one of the factories given to this method can be used without a copy
 * @param factories The list of factory descriptors to add to the system.  May not be null
 * @return A list of the FactoryDescriptor descriptors that were added to the service locator
 * @throws MultiException On a commit failure
 */
public static List<FactoryDescriptors> addFactoryDescriptors(ServiceLocator locator, boolean requiresDeepCopy, FactoryDescriptors... factories) {
    if (factories == null || locator == null)
        throw new IllegalArgumentException();
    List<FactoryDescriptors> retVal = new ArrayList<FactoryDescriptors>(factories.length);
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    for (FactoryDescriptors factory : factories) {
        FactoryDescriptors addMe = config.bind(factory, requiresDeepCopy);
        retVal.add(addMe);
    }
    config.commit();
    return retVal;
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) FactoryDescriptors(org.glassfish.hk2.api.FactoryDescriptors) ArrayList(java.util.ArrayList)

Example 25 with DynamicConfiguration

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

the class ServiceLocatorUtilities method removeFilter.

/**
 * Removes all the descriptors from the given locator that match the
 * given filter
 *
 * @param locator The non-null locator to remove the descriptors from
 * @param filter The non-null filter which will determine what descriptors to remove
 * @param includeAliasDescriptors If set to true all {@link AliasDescriptor}s that point
 * to any descriptors found by filter will also be removed
 */
public static void removeFilter(ServiceLocator locator, Filter filter, boolean includeAliasDescriptors) {
    if (locator == null || filter == null)
        throw new IllegalArgumentException();
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    config.addUnbindFilter(filter);
    if (includeAliasDescriptors == true) {
        List<ActiveDescriptor<?>> goingToDie = locator.getDescriptors(filter);
        if (!goingToDie.isEmpty()) {
            AliasFilter af = new AliasFilter(goingToDie);
            config.addUnbindFilter(af);
        }
    }
    config.commit();
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor)

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