Search in sources :

Example 1 with CollectionMetadata

use of org.osgi.service.blueprint.reflect.CollectionMetadata in project aries by apache.

the class BlueprintContainerImpl method getMetadata.

private <T extends ComponentMetadata> void getMetadata(Class<T> clazz, Metadata component, Collection<T> metadatas) {
    if (component == null) {
        return;
    }
    if (clazz.isInstance(component)) {
        metadatas.add(clazz.cast(component));
    }
    if (component instanceof BeanMetadata) {
        getMetadata(clazz, ((BeanMetadata) component).getFactoryComponent(), metadatas);
        for (BeanArgument arg : ((BeanMetadata) component).getArguments()) {
            getMetadata(clazz, arg.getValue(), metadatas);
        }
        for (BeanProperty prop : ((BeanMetadata) component).getProperties()) {
            getMetadata(clazz, prop.getValue(), metadatas);
        }
    }
    if (component instanceof CollectionMetadata) {
        for (Metadata m : ((CollectionMetadata) component).getValues()) {
            getMetadata(clazz, m, metadatas);
        }
    }
    if (component instanceof MapMetadata) {
        for (MapEntry m : ((MapMetadata) component).getEntries()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
    }
    if (component instanceof PropsMetadata) {
        for (MapEntry m : ((PropsMetadata) component).getEntries()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
    }
    if (component instanceof ServiceReferenceMetadata) {
        for (ReferenceListener l : ((ServiceReferenceMetadata) component).getReferenceListeners()) {
            getMetadata(clazz, l.getListenerComponent(), metadatas);
        }
    }
    if (component instanceof ServiceMetadata) {
        getMetadata(clazz, ((ServiceMetadata) component).getServiceComponent(), metadatas);
        for (MapEntry m : ((ServiceMetadata) component).getServiceProperties()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
        for (RegistrationListener l : ((ServiceMetadata) component).getRegistrationListeners()) {
            getMetadata(clazz, l.getListenerComponent(), metadatas);
        }
    }
}
Also used : BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListener(org.osgi.service.blueprint.reflect.ReferenceListener) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty)

Example 2 with CollectionMetadata

use of org.osgi.service.blueprint.reflect.CollectionMetadata in project aries by apache.

the class AbstractParserProxy method processMultiValueProperty.

private void processMultiValueProperty(Map<String, Object> serviceProps, String key, Metadata value) {
    List<Metadata> values = ((CollectionMetadata) value).getValues();
    Class<?> collectionClass = ((CollectionMetadata) value).getCollectionClass();
    Object collectionValue;
    if (Collection.class.isAssignableFrom(collectionClass)) {
        Collection<String> theseValues = getCollectionFromClass(collectionClass);
        for (Metadata m : values) {
            theseValues.add(((ValueMetadata) m).getStringValue());
        }
        collectionValue = theseValues;
    } else {
        String[] theseValues = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            Metadata m = values.get(i);
            theseValues[i] = ((ValueMetadata) m).getStringValue();
        }
        collectionValue = theseValues;
    }
    serviceProps.put(key, collectionValue);
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata)

Example 3 with CollectionMetadata

use of org.osgi.service.blueprint.reflect.CollectionMetadata in project aries by apache.

the class AbstractParserProxy method parseCDRForServices.

/**
 * Extract Service metadata from a ComponentDefinitionRegistry. When doing SCA modelling, we
 * need to suppress anonymous services. We don't want to do that when we're modelling for
 * provisioning dependencies.
 * @param cdr                       ComponentDefinitionRegistry
 * @param suppressAnonymousServices Unnamed services will not be returned if this is true
 * @return List<WrappedServiceMetadata>
 */
private List<ExportedService> parseCDRForServices(ComponentDefinitionRegistry cdr, boolean suppressAnonymousServices) {
    _logger.debug(LOG_ENTRY, "parseCDRForServices", new Object[] { cdr, suppressAnonymousServices });
    List<ExportedService> result = new ArrayList<ExportedService>();
    for (ComponentMetadata compMetadata : findAllComponents(cdr)) {
        if (compMetadata instanceof ServiceMetadata) {
            ServiceMetadata serviceMetadata = (ServiceMetadata) compMetadata;
            String serviceName;
            int ranking;
            Collection<String> interfaces = new ArrayList<String>();
            Map<String, Object> serviceProps = new HashMap<String, Object>();
            ranking = serviceMetadata.getRanking();
            for (Object i : serviceMetadata.getInterfaces()) {
                interfaces.add((String) i);
            }
            // get the service properties
            List<MapEntry> props = serviceMetadata.getServiceProperties();
            for (MapEntry entry : props) {
                String key = ((ValueMetadata) entry.getKey()).getStringValue();
                Metadata value = entry.getValue();
                if (value instanceof CollectionMetadata) {
                    processMultiValueProperty(serviceProps, key, value);
                } else {
                    serviceProps.put(key, ((ValueMetadata) entry.getValue()).getStringValue());
                }
            }
            // serviceName: use the service id unless that's not set,
            // in which case we use the bean id.
            serviceName = serviceMetadata.getId();
            // If the Service references a Bean, export the bean id as a service property
            // as per 121.6.5 p669 of the blueprint 1.0 specification
            Target t = serviceMetadata.getServiceComponent();
            String targetId = null;
            if (t instanceof RefMetadata) {
                targetId = ((RefMetadata) t).getComponentId();
            } else if (t instanceof BeanMetadata) {
                targetId = ((BeanMetadata) t).getId();
            }
            // or auto-generated for an anonymous service. This must ALWAYS be set.
            if (targetId != null && !targetId.startsWith(".")) {
                // Don't set this for anonymous inner components
                serviceProps.put("osgi.service.blueprint.compname", targetId);
                if (serviceName == null || serviceName.equals("") || serviceName.startsWith(".")) {
                    serviceName = targetId;
                }
            }
            if (serviceName != null && serviceName.startsWith("."))
                serviceName = null;
            // If suppressAnonymous services, do not expose services that have no name
            if (!suppressAnonymousServices || (serviceName != null)) {
                ExportedService wsm = _modellingManager.getExportedService(serviceName, ranking, interfaces, serviceProps);
                result.add(wsm);
            }
        }
    }
    _logger.debug(LOG_EXIT, "parseAllServiceElements", new Object[] { result });
    return result;
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) HashMap(java.util.HashMap) ExportedService(org.apache.aries.application.modelling.ExportedService) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 4 with CollectionMetadata

use of org.osgi.service.blueprint.reflect.CollectionMetadata 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 5 with CollectionMetadata

use of org.osgi.service.blueprint.reflect.CollectionMetadata 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)

Aggregations

CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)14 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)11 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)10 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)9 Metadata (org.osgi.service.blueprint.reflect.Metadata)9 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)7 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)7 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)6 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)5 PropsMetadata (org.osgi.service.blueprint.reflect.PropsMetadata)5 ReferenceListMetadata (org.osgi.service.blueprint.reflect.ReferenceListMetadata)5 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)5 ArrayList (java.util.ArrayList)4 BeanMetadataImpl (org.apache.aries.blueprint.reflect.BeanMetadataImpl)4 BeanProperty (org.osgi.service.blueprint.reflect.BeanProperty)4 WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)3 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)3 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)3 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)3 NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)3