use of org.apache.aries.blueprint.di.MapRecipe in project aries by apache.
the class RecipeBuilder method createMapRecipe.
private MapRecipe createMapRecipe(MapMetadata mapValue) {
String keyType = mapValue.getKeyType();
String valueType = mapValue.getValueType();
MapRecipe mr = new MapRecipe(getName(null), HashMap.class, keyType, valueType);
for (MapEntry entry : mapValue.getEntries()) {
Recipe key = getValue(entry.getKey(), keyType);
Recipe val = getValue(entry.getValue(), valueType);
mr.put(key, val);
}
return mr;
}
use of org.apache.aries.blueprint.di.MapRecipe in project aries by apache.
the class RecipeBuilder method getValue.
private Recipe getValue(Metadata v, Object groupingType) {
if (v instanceof NullMetadata) {
return null;
} else if (v instanceof ComponentMetadata) {
return createRecipe((ComponentMetadata) v);
} else if (v instanceof ValueMetadata) {
ValueMetadata stringValue = (ValueMetadata) v;
Object type = stringValue.getType();
type = (type == null) ? groupingType : type;
ValueRecipe vr = new ValueRecipe(getName(null), stringValue, type);
return vr;
} else if (v instanceof RefMetadata) {
// TODO: make it work with property-placeholders?
String componentName = ((RefMetadata) v).getComponentId();
RefRecipe rr = new RefRecipe(getName(null), componentName);
return rr;
} else if (v instanceof CollectionMetadata) {
CollectionMetadata collectionMetadata = (CollectionMetadata) v;
Class<?> cl = collectionMetadata.getCollectionClass();
String type = collectionMetadata.getValueType();
if (cl == Object[].class) {
ArrayRecipe ar = new ArrayRecipe(getName(null), type);
for (Metadata lv : collectionMetadata.getValues()) {
ar.add(getValue(lv, type));
}
return ar;
} else {
CollectionRecipe cr = new CollectionRecipe(getName(null), cl != null ? cl : ArrayList.class, type);
for (Metadata lv : collectionMetadata.getValues()) {
cr.add(getValue(lv, type));
}
return cr;
}
} else if (v instanceof MapMetadata) {
return createMapRecipe((MapMetadata) v);
} else if (v instanceof PropsMetadata) {
PropsMetadata mapValue = (PropsMetadata) v;
MapRecipe mr = new MapRecipe(getName(null), Properties.class, String.class, String.class);
for (MapEntry entry : mapValue.getEntries()) {
Recipe key = getValue(entry.getKey(), String.class);
Recipe val = getValue(entry.getValue(), String.class);
mr.put(key, val);
}
return mr;
} else if (v instanceof IdRefMetadata) {
// TODO: make it work with property-placeholders?
String componentName = ((IdRefMetadata) v).getComponentId();
IdRefRecipe rnr = new IdRefRecipe(getName(null), componentName);
return rnr;
} else {
throw new IllegalStateException("Unsupported value: " + (v != null ? v.getClass().getName() : "null"));
}
}
use of org.apache.aries.blueprint.di.MapRecipe in project aries by apache.
the class WiringTest method testWiring.
public void testWiring() throws Exception {
ComponentDefinitionRegistryImpl registry = parse("/test-wiring.xml");
Repository repository = new TestBlueprintContainer(registry).getRepository();
Object obj1 = repository.create("pojoA");
assertNotNull(obj1);
assertTrue(obj1 instanceof PojoA);
PojoA pojoa = (PojoA) obj1;
// test singleton scope
assertTrue(obj1 == repository.create("pojoA"));
Object obj2 = repository.create("pojoB");
assertNotNull(obj2);
assertTrue(obj2 instanceof PojoB);
PojoB pojob = (PojoB) obj2;
assertNotNull(pojoa.getPojob());
assertNotNull(pojoa.getPojob().getUri());
assertNotNull(pojoa.getList());
assertEquals("list value", pojoa.getList().get(0));
assertEquals(new Integer(55), pojoa.getList().get(2));
assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getList().get(3));
Object c0 = pojoa.getList().get(1);
Object c1 = pojoa.getList().get(4);
assertNotNull(c0);
assertNotNull(c1);
assertEquals(PojoB.class, c0.getClass());
assertEquals(PojoB.class, c1.getClass());
assertNotSame(c0, c1);
assertNotNull(pojoa.getArray());
assertEquals("list value", pojoa.getArray()[0]);
assertEquals(pojob, pojoa.getArray()[1]);
assertEquals(new Integer(55), pojoa.getArray()[2]);
assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getArray()[3]);
assertNotNull(pojoa.getSet());
assertTrue(pojoa.getSet().contains("set value"));
assertTrue(pojoa.getSet().contains(pojob.getUri()));
assertTrue(pojoa.getSet().contains(URI.create("http://geronimo.apache.org")));
assertNotNull(pojoa.getMap());
assertEquals("val", pojoa.getMap().get("key"));
assertEquals(pojob, pojoa.getMap().get(pojob));
assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getMap().get(new Integer(5)));
assertNotNull(pojoa.getProps());
assertEquals("value1", pojoa.getProps().get("key1"));
assertEquals("value2", pojoa.getProps().get("2"));
assertEquals("bar", pojoa.getProps().get("foo"));
assertNotNull(pojoa.getNumber());
assertEquals(new BigInteger("10"), pojoa.getNumber());
assertNotNull(pojoa.getIntArray());
assertEquals(3, pojoa.getIntArray().length);
assertEquals(1, pojoa.getIntArray()[0]);
assertEquals(50, pojoa.getIntArray()[1]);
assertEquals(100, pojoa.getIntArray()[2]);
assertNotNull(pojoa.getNumberArray());
assertEquals(4, pojoa.getNumberArray().length);
assertEquals(new Integer(1), pojoa.getNumberArray()[0]);
assertEquals(new BigInteger("50"), pojoa.getNumberArray()[1]);
assertEquals(new Long(100), pojoa.getNumberArray()[2]);
assertEquals(new Integer(200), pojoa.getNumberArray()[3]);
// test init-method
assertEquals(true, pojob.getInitCalled());
// test service
Object obj3 = repository.create("service1");
assertNotNull(obj3);
assertTrue(obj3 instanceof ServiceRegistration);
ExecutionContext.Holder.setContext((ExecutionContext) repository);
for (Recipe r : ((ServiceRecipe) repository.getRecipe("service1")).getDependencies()) {
if (r instanceof MapRecipe) {
Map m = (Map) r.create();
assertEquals("value1", m.get("key1"));
assertEquals("value2", m.get("key2"));
assertTrue(m.get("key3") instanceof List);
}
}
ExecutionContext.Holder.setContext(null);
// tests 'prototype' scope
Object obj4 = repository.create("pojoC");
assertNotNull(obj4);
assertTrue(obj4 != repository.create("pojoC"));
repository.destroy();
// test destroy-method
assertEquals(true, pojob.getDestroyCalled());
}
Aggregations