Search in sources :

Example 1 with NullMetadata

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"));
    }
}
Also used : PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) DependentComponentFactoryRecipe(org.apache.aries.blueprint.di.DependentComponentFactoryRecipe) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) ComponentFactoryRecipe(org.apache.aries.blueprint.di.ComponentFactoryRecipe) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) PassThroughRecipe(org.apache.aries.blueprint.di.PassThroughRecipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) ArrayRecipe(org.apache.aries.blueprint.di.ArrayRecipe) RefRecipe(org.apache.aries.blueprint.di.RefRecipe) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) RefRecipe(org.apache.aries.blueprint.di.RefRecipe) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) ComponentFactoryMetadata(org.apache.aries.blueprint.ext.ComponentFactoryMetadata) DependentComponentFactoryMetadata(org.apache.aries.blueprint.ext.DependentComponentFactoryMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata) ExtendedServiceReferenceMetadata(org.apache.aries.blueprint.ExtendedServiceReferenceMetadata) ArrayList(java.util.ArrayList) ArrayRecipe(org.apache.aries.blueprint.di.ArrayRecipe) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata)

Example 2 with NullMetadata

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());
}
Also used : BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata)

Example 3 with NullMetadata

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");
}
Also used : CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)3 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)2 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)2 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)2 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)2 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1 CompositeData (javax.management.openmbean.CompositeData)1 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)1 ExtendedReferenceMetadata (org.apache.aries.blueprint.ExtendedReferenceMetadata)1 ExtendedServiceReferenceMetadata (org.apache.aries.blueprint.ExtendedServiceReferenceMetadata)1 PassThroughMetadata (org.apache.aries.blueprint.PassThroughMetadata)1 ArrayRecipe (org.apache.aries.blueprint.di.ArrayRecipe)1 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)1 ComponentFactoryRecipe (org.apache.aries.blueprint.di.ComponentFactoryRecipe)1 DependentComponentFactoryRecipe (org.apache.aries.blueprint.di.DependentComponentFactoryRecipe)1 IdRefRecipe (org.apache.aries.blueprint.di.IdRefRecipe)1