use of org.osgi.service.blueprint.reflect.NullMetadata 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.osgi.service.blueprint.reflect.NullMetadata in project aries by apache.
the class ParserTest method testParseComponent.
public void testParseComponent() throws Exception {
ComponentDefinitionRegistry registry = parse("/test-simple-component.xml");
assertNotNull(registry);
ComponentMetadata component = registry.getComponentDefinition("pojoA");
assertNotNull(component);
assertEquals("pojoA", component.getId());
assertTrue(component instanceof BeanMetadata);
BeanMetadata local = (BeanMetadata) component;
List<String> deps = local.getDependsOn();
assertNotNull(deps);
assertEquals(2, deps.size());
assertTrue(deps.contains("pojoB"));
assertTrue(deps.contains("pojoC"));
assertEquals("org.apache.aries.blueprint.pojos.PojoA", local.getClassName());
List<BeanArgument> params = local.getArguments();
assertNotNull(params);
assertEquals(6, params.size());
BeanArgument param = params.get(0);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertNull(param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof ValueMetadata);
assertEquals("val0", ((ValueMetadata) param.getValue()).getStringValue());
assertNull(((ValueMetadata) param.getValue()).getType());
param = params.get(1);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertNull(param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof RefMetadata);
assertEquals("val1", ((RefMetadata) param.getValue()).getComponentId());
param = params.get(2);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertNull(param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof NullMetadata);
param = params.get(3);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertEquals("java.lang.String", param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof ValueMetadata);
assertEquals("val3", ((ValueMetadata) param.getValue()).getStringValue());
assertNull(((ValueMetadata) param.getValue()).getType());
param = params.get(4);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertNull(param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof CollectionMetadata);
CollectionMetadata array = (CollectionMetadata) param.getValue();
assertNull(array.getValueType());
assertNotNull(array.getValues());
assertEquals(3, array.getValues().size());
assertTrue(array.getValues().get(0) instanceof ValueMetadata);
assertTrue(array.getValues().get(1) instanceof ComponentMetadata);
assertTrue(array.getValues().get(2) instanceof NullMetadata);
param = params.get(5);
assertNotNull(param);
assertEquals(-1, param.getIndex());
assertNull(param.getValueType());
assertNotNull(param.getValue());
assertTrue(param.getValue() instanceof RefMetadata);
assertEquals("pojoB", ((RefMetadata) param.getValue()).getComponentId());
assertEquals(null, local.getInitMethod());
assertEquals(null, local.getDestroyMethod());
// test pojoB
ComponentMetadata pojoB = registry.getComponentDefinition("pojoB");
assertNotNull(pojoB);
assertEquals("pojoB", pojoB.getId());
assertTrue(pojoB instanceof BeanMetadata);
BeanMetadata pojoBLocal = (BeanMetadata) pojoB;
assertEquals("initPojo", pojoBLocal.getInitMethod());
// assertEquals("", pojoBLocal.getDestroyMethod());
params = pojoBLocal.getArguments();
assertNotNull(params);
assertEquals(2, params.size());
param = params.get(0);
assertNotNull(param);
assertEquals(1, param.getIndex());
param = params.get(1);
assertNotNull(param);
assertEquals(0, param.getIndex());
}
use of org.osgi.service.blueprint.reflect.NullMetadata in project aries by apache.
the class Util method binary2BPMetadata.
public static BPMetadata binary2BPMetadata(byte[] buf) {
if (null == buf)
return null;
ByteArrayInputStream inBytes = new ByteArrayInputStream(buf);
ObjectInputStream inObject;
CompositeData metadata;
try {
inObject = new ObjectInputStream(inBytes);
metadata = (CompositeData) inObject.readObject();
inObject.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
String typename = metadata.getCompositeType().getTypeName();
// target first
if (typename.equals(BlueprintMetadataMBean.BEAN_METADATA))
return new BPBeanMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.REFERENCE_METADATA))
return new BPReferenceMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.REF_METADATA))
return new BPRefMetadata(metadata);
// others
if (typename.equals(BlueprintMetadataMBean.COLLECTION_METADATA))
return new BPCollectionMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.SERVICE_METADATA))
return new BPServiceMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.REFERENCE_LIST_METADATA))
return new BPReferenceListMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.ID_REF_METADATA))
return new BPIdRefMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.MAP_METADATA))
return new BPMapMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.PROPS_METADATA))
return new BPPropsMetadata(metadata);
if (typename.equals(BlueprintMetadataMBean.VALUE_METADATA))
return new BPValueMetadata(metadata);
// null last
if (metadata instanceof NullMetadata)
return new BPNullMetadata(metadata);
throw new RuntimeException("Unknown metadata type");
}
Aggregations