Search in sources :

Example 6 with ComponentMetadata

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

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

the class NamespaceHandlerRegistryImpl method wrapIfNeeded.

/**
     * Wrap the handler if needed to fix its behavior.
     * When asked for a schema location, some simple handlers always return
     * the same url, whatever the asked location is.  This can lead to lots
     * of problems, so we need to verify and fix those behaviors.
     */
private static NamespaceHandler wrapIfNeeded(final NamespaceHandler handler) {
    URL result = null;
    try {
        result = handler.getSchemaLocation("");
    } catch (Throwable t) {
    // Ignore
    }
    if (result != null) {
        LOGGER.warn("NamespaceHandler " + handler.getClass().getName() + " is behaving badly and should be fixed");
        final URL res = result;
        return new NamespaceHandler() {

            final ConcurrentMap<String, Boolean> cache = new ConcurrentHashMap<String, Boolean>();

            @Override
            public URL getSchemaLocation(String s) {
                URL url = handler.getSchemaLocation(s);
                if (url != null && url.equals(res)) {
                    Boolean v, newValue;
                    Boolean valid = ((v = cache.get(s)) == null && (newValue = isValidSchema(s, url)) != null && (v = cache.putIfAbsent(s, newValue)) == null) ? newValue : v;
                    return valid ? url : null;
                }
                return url;
            }

            @Override
            public Set<Class> getManagedClasses() {
                return handler.getManagedClasses();
            }

            @Override
            public Metadata parse(Element element, ParserContext parserContext) {
                return handler.parse(element, parserContext);
            }

            @Override
            public ComponentMetadata decorate(Node node, ComponentMetadata componentMetadata, ParserContext parserContext) {
                return handler.decorate(node, componentMetadata, parserContext);
            }

            private boolean isValidSchema(String ns, URL url) {
                try {
                    InputStream is = url.openStream();
                    try {
                        XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
                        try {
                            reader.nextTag();
                            String nsuri = reader.getNamespaceURI();
                            String name = reader.getLocalName();
                            if ("http://www.w3.org/2001/XMLSchema".equals(nsuri) && "schema".equals(name)) {
                                String target = reader.getAttributeValue(null, "targetNamespace");
                                if (ns.equals(target)) {
                                    return true;
                                }
                            }
                        } finally {
                            reader.close();
                        }
                    } finally {
                        is.close();
                    }
                } catch (Throwable t) {
                // Ignore
                }
                return false;
            }
        };
    } else {
        return handler;
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) URL(java.net.URL) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ParserContext(org.apache.aries.blueprint.ParserContext)

Example 8 with ComponentMetadata

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

the class PlaceholdersUtils method validatePlaceholder.

public static void validatePlaceholder(MutableBeanMetadata metadata, ComponentDefinitionRegistry registry) {
    String prefix = getPlaceholderProperty(metadata, "placeholderPrefix");
    String suffix = getPlaceholderProperty(metadata, "placeholderSuffix");
    for (String id : registry.getComponentDefinitionNames()) {
        ComponentMetadata component = registry.getComponentDefinition(id);
        if (component instanceof ExtendedBeanMetadata) {
            ExtendedBeanMetadata bean = (ExtendedBeanMetadata) component;
            if (bean.getRuntimeClass() != null && AbstractPropertyPlaceholder.class.isAssignableFrom(bean.getRuntimeClass())) {
                String otherPrefix = getPlaceholderProperty(bean, "placeholderPrefix");
                String otherSuffix = getPlaceholderProperty(bean, "placeholderSuffix");
                if (prefix.equals(otherPrefix) && suffix.equals(otherSuffix)) {
                    throw new ComponentDefinitionException("Multiple placeholders with the same prefix and suffix are not allowed");
                }
            }
        }
    }
}
Also used : ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) AbstractPropertyPlaceholder(org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 9 with ComponentMetadata

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

the class BlueprintMetadata method getComponentIdsByType.

/*
     * 
     * type could be Bean, Service, serviceReference (non-Javadoc)
     * 
     * @see org.apache.aries.jmx.blueprint.BlueprintMetadataMBean#getComponentIdsByType(long, java.lang.String)
     */
public String[] getComponentIdsByType(long containerServiceId, String type) throws IOException {
    BlueprintContainer container = getBlueprintContainer(containerServiceId);
    Collection<? extends ComponentMetadata> components;
    if (type.equals(BlueprintMetadataMBean.SERVICE_METADATA)) {
        components = container.getMetadata(ServiceMetadata.class);
    } else if (type.equals(BlueprintMetadataMBean.BEAN_METADATA)) {
        components = container.getMetadata(BeanMetadata.class);
    } else if (type.equals(BlueprintMetadataMBean.SERVICE_REFERENCE_METADATA)) {
        components = container.getMetadata(ServiceReferenceMetadata.class);
    } else {
        throw new IllegalArgumentException("Unrecognized component type: " + type);
    }
    String[] ids = new String[components.size()];
    int i = 0;
    for (ComponentMetadata component : components) {
        // Here we ignore it.
        if (null == component)
            continue;
        ids[i++] = component.getId();
    }
    return ids;
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 10 with ComponentMetadata

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

Aggregations

ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)45 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)22 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)17 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)17 Metadata (org.osgi.service.blueprint.reflect.Metadata)16 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)15 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)15 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)14 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)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 ArrayList (java.util.ArrayList)11 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)11 Element (org.w3c.dom.Element)11 PropsMetadata (org.osgi.service.blueprint.reflect.PropsMetadata)10 NodeList (org.w3c.dom.NodeList)10 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)9