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;
}
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;
}
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"));
}
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());
}
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);
}
Aggregations