Search in sources :

Example 1 with Target

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

the class SpringOsgiNamespaceHandler method parseInlinedTarget.

private Target parseInlinedTarget(ParserContext context, ComponentMetadata metadata, Element element) {
    Target listenerComponent;
    if (BLUEPRINT_NAMESPACE.equals(element.getNamespaceURI()) && BEAN_ELEMENT.equals(element.getLocalName())) {
        listenerComponent = context.parseElement(BeanMetadata.class, metadata, element);
    } else {
        NamespaceHandler handler = context.getNamespaceHandler(URI.create(element.getNamespaceURI()));
        if (handler == null) {
            throw new IllegalStateException("No NamespaceHandler found for " + element.getNamespaceURI());
        }
        Metadata md = handler.parse(element, context);
        if (!(md instanceof Target)) {
            throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
        }
        listenerComponent = (Target) md;
    }
    return listenerComponent;
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 2 with Target

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

the class SpringOsgiNamespaceHandler method parseReference.

private Metadata parseReference(Element element, ParserContext context) {
    MutableReferenceMetadata metadata = context.createMetadata(MutableReferenceMetadata.class);
    // Parse attributes
    if (element.hasAttribute(ID_ATTRIBUTE)) {
        metadata.setId(element.getAttribute(ID_ATTRIBUTE));
    } else {
        metadata.setId(generateId(context));
    }
    metadata.setAvailability(CARDINALITY_0_1.equals(element.getAttribute(CARDINALITY_ATTRIBUTE)) ? ReferenceMetadata.AVAILABILITY_OPTIONAL : ReferenceMetadata.AVAILABILITY_MANDATORY);
    metadata.setTimeout(getLong(element.getAttribute(TIMEOUT_ATTRIBUTE), DEFAULT_TIMEOUT));
    metadata.setInterface(element.getAttribute(INTERFACE_ATTRIBUTE));
    metadata.setFilter(element.getAttribute(FILTER_ATTRIBUTE));
    String[] dependsOn = StringUtils.tokenizeToStringArray(element.getAttribute(DEPENDS_ON_ATTRIBUTE), ",; ");
    metadata.setDependsOn(dependsOn != null ? Arrays.asList(dependsOn) : null);
    metadata.setComponentName(element.getAttribute(BEAN_NAME_ELEMENT));
    // Parse child elements
    for (Element child : getChildren(element)) {
        if (element.getNamespaceURI().equals(child.getNamespaceURI())) {
            if (INTERFACES_ELEMENT.equals(child.getLocalName())) {
                List<String> itfs = parseInterfaces(child);
                metadata.setExtraInterfaces(itfs);
            } else if (LISTENER_ELEMENT.equals(child.getLocalName())) {
                String bindMethod = nonEmpty(child.getAttribute(BIND_METHOD_ATTRIBUTE));
                String unbindMethod = nonEmpty(child.getAttribute(UNBIND_METHOD_ATTRIBUTE));
                String refStr = nonEmpty(child.getAttribute(REF_ATTRIBUTE));
                Target listenerComponent = null;
                if (refStr != null) {
                    MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
                    ref.setComponentId(refStr);
                    listenerComponent = ref;
                }
                for (Element cchild : getChildren(child)) {
                    if (listenerComponent != null) {
                        throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
                    }
                    listenerComponent = parseInlinedTarget(context, metadata, cchild);
                }
                if (listenerComponent == null) {
                    throw new IllegalArgumentException("Missing @ref attribute or inlined bean definition element");
                }
                metadata.addServiceListener(listenerComponent, bindMethod, unbindMethod);
            }
        } else {
            throw new UnsupportedOperationException("Custom namespaces not supported");
        }
    }
    return metadata;
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) Element(org.w3c.dom.Element) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

Example 3 with Target

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

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

the class AbstractPropertyPlaceholder method processBeanMetadata.

protected Metadata processBeanMetadata(BeanMetadata component) {
    for (BeanArgument arg : component.getArguments()) {
        try {
            processingStack.add("Argument index " + arg.getIndex() + " and value type " + arg.getValueType() + "->");
            if (arg instanceof MutableBeanArgument) {
                ((MutableBeanArgument) arg).setValue(processMetadata(arg.getValue()));
            } else {
                // Say that we can't change this argument, but continue processing
                // If the value is mutable then we may be ok!
                printWarning(arg, "Constructor Argument");
                processMetadata(arg.getValue());
            }
        } finally {
            processingStack.removeLast();
        }
    }
    for (BeanProperty prop : component.getProperties()) {
        try {
            processingStack.add("Property named " + prop.getName() + "->");
            if (prop instanceof MutableBeanProperty) {
                ((MutableBeanProperty) prop).setValue(processMetadata(prop.getValue()));
            } else {
                // Say that we can't change this property, but continue processing
                // If the value is mutable then we may be ok!
                printWarning(prop, "Injection Property");
                processMetadata(prop.getValue());
            }
        } finally {
            processingStack.removeLast();
        }
    }
    Target factoryComponent = component.getFactoryComponent();
    if (factoryComponent != null) {
        try {
            if (component instanceof MutableBeanMetadata) {
                processingStack.add("Factory Component->");
                ((MutableBeanMetadata) component).setFactoryComponent((Target) processMetadata(factoryComponent));
            } else {
                printWarning(component, "Factory Component");
                processingStack.add("Factory Component->");
                processMetadata(factoryComponent);
            }
        } finally {
            processingStack.removeLast();
        }
    }
    return component;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableBeanArgument(org.apache.aries.blueprint.mutable.MutableBeanArgument) BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) Target(org.osgi.service.blueprint.reflect.Target) MutableBeanArgument(org.apache.aries.blueprint.mutable.MutableBeanArgument) MutableBeanProperty(org.apache.aries.blueprint.mutable.MutableBeanProperty) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty) MutableBeanProperty(org.apache.aries.blueprint.mutable.MutableBeanProperty)

Example 5 with Target

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

the class AbstractPropertyPlaceholder method processServiceMetadata.

protected Metadata processServiceMetadata(ServiceMetadata component) {
    try {
        if (component instanceof MutableServiceMetadata) {
            processingStack.add("Service Component->");
            ((MutableServiceMetadata) component).setServiceComponent((Target) processMetadata(component.getServiceComponent()));
        } else {
            printWarning(component, "Service Component");
            processingStack.add("Service Component->");
            processMetadata(component.getServiceComponent());
        }
    } finally {
        processingStack.removeLast();
    }
    List<MapEntry> entries = new ArrayList<MapEntry>(component.getServiceProperties());
    if (!!!entries.isEmpty()) {
        try {
            if (component instanceof MutableServiceMetadata) {
                processingStack.add("Service Properties->");
                MutableServiceMetadata msm = (MutableServiceMetadata) component;
                for (MapEntry entry : entries) {
                    msm.removeServiceProperty(entry);
                }
                for (MapEntry entry : processMapEntries(entries)) {
                    msm.addServiceProperty(entry);
                }
            } else {
                printWarning(component, "Service Properties");
                processingStack.add("Service Properties->");
                processMapEntries(entries);
            }
        } finally {
            processingStack.removeLast();
        }
    }
    for (RegistrationListener listener : component.getRegistrationListeners()) {
        Target listenerComponent = listener.getListenerComponent();
        try {
            processingStack.add("Registration Listener " + listenerComponent + "->");
            if (listener instanceof MutableRegistrationListener) {
                ((MutableRegistrationListener) listener).setListenerComponent((Target) processMetadata(listenerComponent));
            } else {
                //Say that we can't change this listener, but continue processing
                //If the value is mutable then we may be ok!
                printWarning(listener, "Service Registration Listener");
                processMetadata(listenerComponent);
            }
        } finally {
            processingStack.removeLast();
        }
    }
    return component;
}
Also used : MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) MutableRegistrationListener(org.apache.aries.blueprint.mutable.MutableRegistrationListener) Target(org.osgi.service.blueprint.reflect.Target) MutableMapEntry(org.apache.aries.blueprint.mutable.MutableMapEntry) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) ArrayList(java.util.ArrayList) MutableRegistrationListener(org.apache.aries.blueprint.mutable.MutableRegistrationListener)

Aggregations

Target (org.osgi.service.blueprint.reflect.Target)10 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)5 ArrayList (java.util.ArrayList)4 Element (org.w3c.dom.Element)4 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)3 MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)3 MutableServiceMetadata (org.apache.aries.blueprint.mutable.MutableServiceMetadata)3 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)3 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)3 Metadata (org.osgi.service.blueprint.reflect.Metadata)3 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)3 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)2 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)2 IdRefMetadataImpl (org.apache.aries.blueprint.reflect.IdRefMetadataImpl)2 RefMetadataImpl (org.apache.aries.blueprint.reflect.RefMetadataImpl)2 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)2 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)2 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)2 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)2 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)2