Search in sources :

Example 1 with FactoryDescriptors

use of org.glassfish.hk2.api.FactoryDescriptors 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 2 with FactoryDescriptors

use of org.glassfish.hk2.api.FactoryDescriptors 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 3 with FactoryDescriptors

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

the class DescriptorBuilderTest method testBuildFactoryNoArg.

/**
 * Tests factory production
 */
@Test
public void testBuildFactoryNoArg() {
    BlueImpl blue = new BlueImpl();
    HK2LoaderImpl loader = new HK2LoaderImpl();
    FactoryDescriptors fds = BuilderHelper.link(FACTORY_CLASS_NAME).named(NAME).to(CONTRACT_NAME).in(Singleton.class).qualifiedBy(blue).ofRank(MY_RANK).andLoadWith(loader).has(KEY, VALUE).proxy(false).localOnly().buildFactory();
    {
        // Now ensure the resulting descriptors have the expected results
        Descriptor asService = fds.getFactoryAsAService();
        // The javadoc says the return from buildFactory will have DescriptorImpl
        Assert.assertTrue(asService instanceof DescriptorImpl);
        Assert.assertEquals(DescriptorType.CLASS, asService.getDescriptorType());
        Assert.assertEquals(DescriptorVisibility.NORMAL, asService.getDescriptorVisibility());
        Assert.assertEquals(FACTORY_CLASS_NAME, asService.getImplementation());
        Assert.assertEquals(PerLookup.class.getName(), asService.getScope());
        Assert.assertEquals(MY_RANK, asService.getRanking());
        Assert.assertNull(asService.getName());
        Assert.assertTrue(asService.getQualifiers().isEmpty());
        Assert.assertTrue(asService.getMetadata().isEmpty());
        Assert.assertEquals(loader, asService.getLoader());
        Assert.assertNull(asService.isProxiable());
        Set<String> serviceContracts = asService.getAdvertisedContracts();
        Assert.assertEquals(2, serviceContracts.size());
        Assert.assertTrue(serviceContracts.contains(FACTORY_CLASS_NAME));
        Assert.assertTrue(serviceContracts.contains(Factory.class.getName()));
    }
    {
        // Now ensure the resulting descriptors have the expected results
        Descriptor asFactory = fds.getFactoryAsAFactory();
        // The javadoc says the return from buildFactory will have DescriptorImpl
        Assert.assertTrue(asFactory instanceof DescriptorImpl);
        Assert.assertEquals(DescriptorType.PROVIDE_METHOD, asFactory.getDescriptorType());
        Assert.assertEquals(DescriptorVisibility.LOCAL, asFactory.getDescriptorVisibility());
        Assert.assertEquals(FACTORY_CLASS_NAME, asFactory.getImplementation());
        Assert.assertEquals(Singleton.class.getName(), asFactory.getScope());
        Assert.assertEquals(MY_RANK, asFactory.getRanking());
        Assert.assertEquals(NAME, asFactory.getName());
        Assert.assertEquals(loader, asFactory.getLoader());
        Assert.assertEquals(false, asFactory.isProxiable().booleanValue());
        Set<String> qualifiers = asFactory.getQualifiers();
        Assert.assertEquals(2, qualifiers.size());
        Assert.assertTrue(qualifiers.contains(Blue.class.getName()));
        Assert.assertTrue(qualifiers.contains(Named.class.getName()));
        Map<String, List<String>> metadata = asFactory.getMetadata();
        Assert.assertEquals(1, metadata.size());
        Assert.assertTrue(metadata.containsKey(KEY));
        List<String> values = metadata.get(KEY);
        Assert.assertEquals(1, values.size());
        Assert.assertEquals(VALUE, values.get(0));
        Set<String> serviceContracts = asFactory.getAdvertisedContracts();
        Assert.assertEquals(1, serviceContracts.size());
        Assert.assertTrue(serviceContracts.contains(CONTRACT_NAME));
    }
    Assert.assertTrue(fds.toString().contains("descriptorType=PROVIDE_METHOD"));
    Assert.assertTrue(fds.toString().contains("descriptorType=CLASS"));
}
Also used : Named(jakarta.inject.Named) Set(java.util.Set) DescriptorImpl(org.glassfish.hk2.utilities.DescriptorImpl) Factory(org.glassfish.hk2.api.Factory) FactoryDescriptors(org.glassfish.hk2.api.FactoryDescriptors) Singleton(jakarta.inject.Singleton) Descriptor(org.glassfish.hk2.api.Descriptor) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) Test(org.junit.Test)

Example 4 with FactoryDescriptors

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

the class BasicTopicTest method testDifferentClassFactory.

/**
 * Tests that a factory that returns different classes on subsequent
 * calls gets events on all created objects
 */
@Test
public void testDifferentClassFactory() {
    ServiceLocator locator = Utilities.getLocatorWithTopics();
    ServiceLocatorUtilities.addClasses(locator, FooPublisher.class);
    FactoryDescriptors factoryDescriptors = BuilderHelper.link(GreekFactory.class).to(Greek.class.getName()).in(PerLookup.class.getName()).qualifiedBy(MessageReceiver.class.getName()).buildFactory(Singleton.class.getName());
    ServiceLocatorUtilities.addFactoryDescriptors(locator, false, factoryDescriptors);
    FooPublisher fooPublisher = locator.getService(FooPublisher.class);
    Greek greek1 = locator.getService(Greek.class);
    Greek greek2 = locator.getService(Greek.class);
    Greek greek3 = locator.getService(Greek.class);
    Assert.assertNotSame(greek1.getClass(), greek2.getClass());
    Assert.assertNotSame(greek1.getClass(), greek3.getClass());
    Assert.assertNotSame(greek2.getClass(), greek3.getClass());
    fooPublisher.publishFoo(3);
    Assert.assertEquals(3, greek1.getFooValue());
    Assert.assertEquals(3, greek2.getFooValue());
    Assert.assertEquals(3, greek3.getFooValue());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) FactoryDescriptors(org.glassfish.hk2.api.FactoryDescriptors) Singleton(jakarta.inject.Singleton) PerLookup(org.glassfish.hk2.api.PerLookup) Test(org.junit.Test)

Example 5 with FactoryDescriptors

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

the class DynamicConfigurationImpl method bind.

@Override
public FactoryDescriptors bind(FactoryDescriptors factoryDescriptors, boolean requiresDeepCopy) {
    if (factoryDescriptors == null)
        throw new IllegalArgumentException("factoryDescriptors is null");
    // Now a bunch of validations
    Descriptor asService = factoryDescriptors.getFactoryAsAService();
    Descriptor asFactory = factoryDescriptors.getFactoryAsAFactory();
    checkDescriptor(asService);
    checkDescriptor(asFactory);
    String implClassService = asService.getImplementation();
    String implClassFactory = asFactory.getImplementation();
    if (!implClassService.equals(implClassFactory)) {
        throw new IllegalArgumentException("The implementation classes must match (" + implClassService + "/" + implClassFactory + ")");
    }
    if (!asService.getDescriptorType().equals(DescriptorType.CLASS)) {
        throw new IllegalArgumentException("The getFactoryAsService descriptor must be of type CLASS");
    }
    if (!asFactory.getDescriptorType().equals(DescriptorType.PROVIDE_METHOD)) {
        throw new IllegalArgumentException("The getFactoryAsFactory descriptor must be of type PROVIDE_METHOD");
    }
    final SystemDescriptor<?> boundAsService = new SystemDescriptor<Object>(asService, requiresDeepCopy, locator, locator.getNextServiceId());
    // Link the factory descriptor to the service descriptor for the factory
    final SystemDescriptor<?> boundAsFactory = new SystemDescriptor<Object>(asFactory, requiresDeepCopy, locator, locator.getNextServiceId());
    if (asService instanceof ActiveDescriptor) {
        boundAsFactory.setFactoryIds(boundAsService.getLocatorId(), boundAsService.getServiceId());
    }
    // Bind the factory first, so normally people get the factory, not the service
    allDescriptors.add(boundAsFactory);
    allDescriptors.add(boundAsService);
    return new FactoryDescriptorsImpl(boundAsService, boundAsFactory);
}
Also used : ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Descriptor(org.glassfish.hk2.api.Descriptor) FactoryDescriptorsImpl(org.glassfish.hk2.utilities.FactoryDescriptorsImpl)

Aggregations

FactoryDescriptors (org.glassfish.hk2.api.FactoryDescriptors)15 Test (org.junit.Test)13 Descriptor (org.glassfish.hk2.api.Descriptor)9 Singleton (jakarta.inject.Singleton)8 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)7 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)7 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)6 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)6 Factory (org.glassfish.hk2.api.Factory)6 DescriptorImpl (org.glassfish.hk2.utilities.DescriptorImpl)5 LinkedList (java.util.LinkedList)4 FactoryDescriptorsImpl (org.glassfish.hk2.utilities.FactoryDescriptorsImpl)4 AbstractActiveDescriptor (org.glassfish.hk2.utilities.AbstractActiveDescriptor)3 Type (java.lang.reflect.Type)2 List (java.util.List)2 ErrorInformation (org.glassfish.hk2.api.ErrorInformation)2 PerLookup (org.glassfish.hk2.api.PerLookup)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 Named (jakarta.inject.Named)1 ParameterizedType (java.lang.reflect.ParameterizedType)1