Search in sources :

Example 46 with ComponentMetadata

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

the class Parser method parseService.

private ComponentMetadata parseService(Element element, boolean topElement) {
    ServiceMetadataImpl service = new ServiceMetadataImpl();
    boolean hasInterfaceNameAttribute = false;
    if (topElement) {
        service.setId(getId(element));
        service.setActivation(parseActivation(element));
    } else {
        service.setActivation(ComponentMetadata.ACTIVATION_LAZY);
    }
    if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
        service.setInterfaceNames(Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE)));
        hasInterfaceNameAttribute = true;
    }
    if (element.hasAttribute(REF_ATTRIBUTE)) {
        service.setServiceComponent(new RefMetadataImpl(element.getAttribute(REF_ATTRIBUTE)));
    }
    if (element.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
        service.setDependsOn(parseList(element.getAttribute(DEPENDS_ON_ATTRIBUTE)));
    }
    String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
    if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
        service.setAutoExport(ServiceMetadata.AUTO_EXPORT_DISABLED);
    } else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
        service.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
        service.setAutoExport(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL.equals(autoExport)) {
        service.setAutoExport(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
    }
    String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
    try {
        service.setRanking(Integer.parseInt(ranking));
    } catch (NumberFormatException e) {
        throw new ComponentDefinitionException("Attribute " + RANKING_ATTRIBUTE + " must be a valid integer (was: " + ranking + ")");
    }
    // Parse elements
    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 (hasInterfaceNameAttribute) {
                        throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
                    }
                    service.setInterfaceNames(parseInterfaceNames(e));
                } else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
                    List<MapEntry> entries = parseServiceProperties(e, service).getEntries();
                    service.setServiceProperties(entries);
                } else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
                    service.addRegistrationListener(parseRegistrationListener(e, service));
                } else if (nodeNameEquals(e, BEAN_ELEMENT)) {
                    if (service.getServiceComponent() != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
                    }
                    service.setServiceComponent((Target) parseBeanMetadata(e, false));
                } else if (nodeNameEquals(e, REF_ELEMENT)) {
                    if (service.getServiceComponent() != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
                    }
                    String component = e.getAttribute(COMPONENT_ID_ATTRIBUTE);
                    if (component == null || component.length() == 0) {
                        throw new ComponentDefinitionException("Element " + REF_ELEMENT + " must have a valid " + COMPONENT_ID_ATTRIBUTE + " attribute");
                    }
                    service.setServiceComponent(new RefMetadataImpl(component));
                } else if (nodeNameEquals(e, REFERENCE_ELEMENT)) {
                    if (service.getServiceComponent() != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element can be set");
                    }
                    service.setServiceComponent((Target) parseReference(e, false));
                }
            }
        }
    }
    // Check service
    if (service.getServiceComponent() == null) {
        throw new ComponentDefinitionException("One of " + REF_ATTRIBUTE + " attribute, " + BEAN_ELEMENT + " element, " + REFERENCE_ELEMENT + " element or " + REF_ELEMENT + " element must be set");
    }
    // Check interface
    if (service.getAutoExport() == ServiceMetadata.AUTO_EXPORT_DISABLED && service.getInterfaces().isEmpty()) {
        throw new ComponentDefinitionException(INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be set when " + AUTO_EXPORT_ATTRIBUTE + " is set to " + AUTO_EXPORT_DISABLED);
    }
    // Check for non-disabled auto-exports and interfaces
    if (service.getAutoExport() != ServiceMetadata.AUTO_EXPORT_DISABLED && !service.getInterfaces().isEmpty()) {
        throw new ComponentDefinitionException(INTERFACE_ATTRIBUTE + " attribute or  " + INTERFACES_ELEMENT + " element must not be set when " + AUTO_EXPORT_ATTRIBUTE + " is set to anything else than " + AUTO_EXPORT_DISABLED);
    }
    ComponentMetadata s = service;
    // Parse custom attributes
    s = handleCustomAttributes(element.getAttributes(), s);
    // Parse custom elements;
    s = handleCustomElements(element, s);
    return s;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) Target(org.osgi.service.blueprint.reflect.Target) ServiceMetadataImpl(org.apache.aries.blueprint.reflect.ServiceMetadataImpl) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl)

Example 47 with ComponentMetadata

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

the class Parser method parseBeanProperty.

public BeanProperty parseBeanProperty(ComponentMetadata enclosingComponent, Element element) {
    String name = element.hasAttribute(NAME_ATTRIBUTE) ? element.getAttribute(NAME_ATTRIBUTE) : null;
    Metadata value = parseArgumentOrPropertyValue(element, enclosingComponent);
    return new BeanPropertyImpl(name, value);
}
Also used : BeanPropertyImpl(org.apache.aries.blueprint.reflect.BeanPropertyImpl) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata)

Example 48 with ComponentMetadata

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

the class BlueprintMetadata method getComponentMetadata.

public CompositeData getComponentMetadata(long containerServiceId, String componentId) throws IOException {
    BlueprintContainer container = getBlueprintContainer(containerServiceId);
    ComponentMetadata componentMetadata = container.getComponentMetadata(componentId);
    BPMetadata metadata = Util.metadata2BPMetadata(componentMetadata);
    return metadata.asCompositeData();
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) BPMetadata(org.apache.aries.jmx.blueprint.codec.BPMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 49 with ComponentMetadata

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

the class CmNamespaceHandler method parsePropertyPlaceholder.

private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    metadata.setRuntimeClass(CmPropertyPlaceholder.class);
    metadata.setInitMethod("init");
    metadata.setDestroyMethod("destroy");
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
    String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
    metadata.addProperty("placeholderPrefix", createValue(context, prefix));
    String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) : "}";
    metadata.addProperty("placeholderSuffix", createValue(context, suffix));
    String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
    if (defaultsRef != null) {
        metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
    }
    String ignoreMissingLocations = extractIgnoreMissingLocations(element);
    if (ignoreMissingLocations != null) {
        metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
    }
    String systemProperties = extractSystemPropertiesAttribute(element);
    if (systemProperties == null) {
        systemProperties = SYSTEM_PROPERTIES_NEVER;
    }
    metadata.addProperty("systemProperties", createValue(context, systemProperties));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    // Parse elements
    List<String> locations = new ArrayList<String>();
    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 (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
                    if (defaultsRef != null) {
                        throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
                    }
                    Metadata props = parseDefaultProperties(context, metadata, e);
                    metadata.addProperty("defaultProperties", props);
                }
            } else if (isExtNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, LOCATION_ELEMENT)) {
                    locations.add(getTextValue(e));
                }
            }
        }
    }
    if (!locations.isEmpty()) {
        metadata.addProperty("locations", createList(context, locations));
    }
    PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return metadata;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) 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) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) MutableIdRefMetadata(org.apache.aries.blueprint.mutable.MutableIdRefMetadata) MutableComponentMetadata(org.apache.aries.blueprint.mutable.MutableComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 50 with ComponentMetadata

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

the class BlueprintContainerImpl method processTypeConverters.

private void processTypeConverters() throws Exception {
    List<String> typeConverters = new ArrayList<String>();
    for (Target target : componentDefinitionRegistry.getTypeConverters()) {
        if (target instanceof ComponentMetadata) {
            typeConverters.add(((ComponentMetadata) target).getId());
        } else if (target instanceof RefMetadata) {
            typeConverters.add(((RefMetadata) target).getComponentId());
        } else {
            throw new ComponentDefinitionException("Unexpected metadata for type converter: " + target);
        }
    }
    Map<String, Object> objects = repository.createAll(typeConverters, Arrays.<Class<?>>asList(Converter.class));
    for (String name : typeConverters) {
        Object obj = objects.get(name);
        if (obj instanceof Converter) {
            converter.registerConverter((Converter) obj);
        } else {
            throw new ComponentDefinitionException("Type converter " + obj + " does not implement the " + Converter.class.getName() + " interface");
        }
    }
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ArrayList(java.util.ArrayList) Converter(org.osgi.service.blueprint.container.Converter) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Aggregations

ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)54 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)22 Metadata (org.osgi.service.blueprint.reflect.Metadata)19 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)18 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)17 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)17 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)15 ArrayList (java.util.ArrayList)14 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)14 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)14 Element (org.w3c.dom.Element)14 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)13 NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)12 ReferenceListMetadata (org.osgi.service.blueprint.reflect.ReferenceListMetadata)12 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)12 Node (org.w3c.dom.Node)12 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)11 PropsMetadata (org.osgi.service.blueprint.reflect.PropsMetadata)10 NodeList (org.w3c.dom.NodeList)10 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)9