Search in sources :

Example 1 with MapMetadata

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

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

the class Parser method parseServiceProperties.

public MapMetadata parseServiceProperties(Element element, ComponentMetadata enclosingComponent) {
    // TODO: need to handle this better
    MapMetadata map = parseMap(element, enclosingComponent);
    handleCustomElements(element, enclosingComponent);
    return map;
}
Also used : MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata)

Example 3 with MapMetadata

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

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

the class CmNamespaceHandler method parseManagedServiceFactory.

private ComponentMetadata parseManagedServiceFactory(ParserContext context, Element element) {
    String id = getId(context, element);
    MutableBeanMetadata factoryMetadata = context.createMetadata(MutableBeanMetadata.class);
    generateIdIfNeeded(context, factoryMetadata);
    factoryMetadata.addProperty("id", createValue(context, factoryMetadata.getId()));
    factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    factoryMetadata.setRuntimeClass(CmManagedServiceFactory.class);
    factoryMetadata.setInitMethod("init");
    factoryMetadata.setDestroyMethod("destroy");
    factoryMetadata.addArgument(createRef(context, "blueprintContainer"), null, 0);
    factoryMetadata.addProperty("factoryPid", createValue(context, element.getAttribute(FACTORY_PID_ATTRIBUTE)));
    String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
    if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_DISABLED);
    } else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
    }
    factoryMetadata.addProperty("autoExport", createValue(context, autoExport));
    String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
    factoryMetadata.addProperty("ranking", createValue(context, ranking));
    List<String> interfaces = null;
    if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
        interfaces = Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE));
        factoryMetadata.addProperty("interfaces", createList(context, interfaces));
    }
    // Parse elements
    List<RegistrationListener> listeners = new ArrayList<RegistrationListener>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (isBlueprintNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, INTERFACES_ELEMENT)) {
                    if (interfaces != null) {
                        throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
                    }
                    interfaces = parseInterfaceNames(e);
                    factoryMetadata.addProperty("interfaces", createList(context, interfaces));
                } else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
                    MapMetadata map = context.parseElement(MapMetadata.class, factoryMetadata, e);
                    factoryMetadata.addProperty("serviceProperties", map);
                    NodeList enl = e.getChildNodes();
                    for (int j = 0; j < enl.getLength(); j++) {
                        Node enode = enl.item(j);
                        if (enode instanceof Element) {
                            if (isCmNamespace(enode.getNamespaceURI()) && nodeNameEquals(enode, CM_PROPERTIES_ELEMENT)) {
                                decorateCmProperties(context, (Element) enode, factoryMetadata);
                            }
                        }
                    }
                } else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
                    listeners.add(context.parseElement(RegistrationListener.class, factoryMetadata, e));
                }
            } else if (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, MANAGED_COMPONENT_ELEMENT)) {
                    MutableBeanMetadata managedComponent = context.parseElement(MutableBeanMetadata.class, null, e);
                    generateIdIfNeeded(context, managedComponent);
                    managedComponent.setScope(BeanMetadata.SCOPE_PROTOTYPE);
                    // destroy-method on managed-component has different signature than on regular beans
                    // so we'll handle it differently
                    String destroyMethod = managedComponent.getDestroyMethod();
                    if (destroyMethod != null) {
                        factoryMetadata.addProperty("componentDestroyMethod", createValue(context, destroyMethod));
                        managedComponent.setDestroyMethod(null);
                    }
                    context.getComponentDefinitionRegistry().registerComponentDefinition(managedComponent);
                    factoryMetadata.addProperty("managedComponentName", createIdRef(context, managedComponent.getId()));
                }
            }
        }
    }
    MutableCollectionMetadata listenerCollection = context.createMetadata(MutableCollectionMetadata.class);
    listenerCollection.setCollectionClass(List.class);
    for (RegistrationListener listener : listeners) {
        MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
        bean.setRuntimeClass(ServiceListener.class);
        bean.addProperty("listener", listener.getListenerComponent());
        bean.addProperty("registerMethod", createValue(context, listener.getRegistrationMethod()));
        bean.addProperty("unregisterMethod", createValue(context, listener.getUnregistrationMethod()));
        listenerCollection.addValue(bean);
    }
    factoryMetadata.addProperty("listeners", listenerCollection);
    context.getComponentDefinitionRegistry().registerComponentDefinition(factoryMetadata);
    MutableBeanMetadata mapMetadata = context.createMetadata(MutableBeanMetadata.class);
    mapMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    mapMetadata.setId(id);
    mapMetadata.setFactoryComponent(createRef(context, factoryMetadata.getId()));
    mapMetadata.setFactoryMethod("getServiceMap");
    return mapMetadata;
}
Also used : RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 5 with MapMetadata

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

the class AbstractParserProxy method traverse.

/**
	   * Traverse to find all nested {@link ComponentMetadata} instances
	   * @param metadata
	   * @param output
	   */
private void traverse(Metadata metadata, Set<ComponentMetadata> output) {
    if (metadata instanceof ComponentMetadata) {
        traverseComponent((ComponentMetadata) metadata, output);
    } else if (metadata instanceof CollectionMetadata) {
        CollectionMetadata collection = (CollectionMetadata) metadata;
        for (Metadata v : collection.getValues()) traverse(v, output);
    } else if (metadata instanceof MapMetadata) {
        MapMetadata map = (MapMetadata) metadata;
        for (MapEntry e : map.getEntries()) {
            traverse(e.getKey(), output);
            traverse(e.getValue(), output);
        }
    }
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) 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) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata)

Aggregations

MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)6 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)4 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)4 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)3 Metadata (org.osgi.service.blueprint.reflect.Metadata)3 PropsMetadata (org.osgi.service.blueprint.reflect.PropsMetadata)3 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)3 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)3 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)3 ArrayList (java.util.ArrayList)2 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)2 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)2 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)2 ReferenceListMetadata (org.osgi.service.blueprint.reflect.ReferenceListMetadata)2 RegistrationListener (org.osgi.service.blueprint.reflect.RegistrationListener)2 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)2 WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)1 ExtendedReferenceMetadata (org.apache.aries.blueprint.ExtendedReferenceMetadata)1