Search in sources :

Example 51 with ComponentMetadata

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

the class BlueprintBeanLocator method getBeanNamesOfType.

/**
 * {@inheritDoc}
 */
public List<String> getBeanNamesOfType(Class<?> type) {
    Set<String> names = new LinkedHashSet<String>();
    for (String s : container.getComponentIds()) {
        ComponentMetadata cmd = container.getComponentMetadata(s);
        Class<?> cls = getClassForMetaData(cmd);
        if (cls != null && type.isAssignableFrom(cls)) {
            names.add(s);
        }
    }
    names.addAll(orig.getBeanNamesOfType(type));
    return new ArrayList<>(names);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 52 with ComponentMetadata

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

the class BlueprintBeanLocator method loadBeansOfType.

/**
 * {@inheritDoc}
 */
public <T> boolean loadBeansOfType(Class<T> type, BeanLoaderListener<T> listener) {
    List<String> names = new ArrayList<>();
    boolean loaded = false;
    for (String s : container.getComponentIds()) {
        ComponentMetadata cmd = container.getComponentMetadata(s);
        Class<?> cls = getClassForMetaData(cmd);
        if (cls != null && type.isAssignableFrom(cls)) {
            names.add(s);
        }
    }
    Collections.reverse(names);
    for (String s : names) {
        ComponentMetadata cmd = container.getComponentMetadata(s);
        Class<?> beanType = getClassForMetaData(cmd);
        Class<? extends T> t = beanType.asSubclass(type);
        if (listener.loadBean(s, t)) {
            Object o = container.getComponentInstance(s);
            if (listener.beanLoaded(s, type.cast(o))) {
                return true;
            }
            loaded = true;
        }
    }
    try {
        if (context != null) {
            ServiceReference<?>[] refs = context.getServiceReferences(type.getName(), null);
            if (refs != null) {
                for (ServiceReference<?> r : refs) {
                    Object o2 = context.getService(r);
                    Class<? extends T> t = o2.getClass().asSubclass(type);
                    if (listener.loadBean(t.getName(), t)) {
                        if (listener.beanLoaded(t.getName(), type.cast(o2))) {
                            return true;
                        }
                        loaded = true;
                    }
                }
            }
        }
    } catch (Exception ex) {
        // ignore, just don't support the OSGi services
        LOG.info("Try to find the Bean with type:" + type + " from OSGi services and get error: " + ex);
    }
    return orig.loadBeansOfType(type, listener) || loaded;
}
Also used : ArrayList(java.util.ArrayList) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) NoSuchComponentException(org.osgi.service.blueprint.container.NoSuchComponentException) ServiceReference(org.osgi.framework.ServiceReference)

Example 53 with ComponentMetadata

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

the class BlueprintBeanLocator method getBeansOfType.

/**
 * {@inheritDoc}
 */
public <T> Collection<? extends T> getBeansOfType(Class<T> type) {
    List<T> list = new ArrayList<>();
    for (String s : container.getComponentIds()) {
        ComponentMetadata cmd = container.getComponentMetadata(s);
        Class<?> cls = getClassForMetaData(cmd);
        if (cls != null && type.isAssignableFrom(cls)) {
            list.add(type.cast(container.getComponentInstance(s)));
        }
    }
    if (list.isEmpty() && context != null) {
        try {
            ServiceReference<?>[] refs = context.getServiceReferences(type.getName(), null);
            if (refs != null) {
                for (ServiceReference<?> r : refs) {
                    list.add(type.cast(context.getService(r)));
                }
            }
        } catch (Exception ex) {
            // ignore, just don't support the OSGi services
            LOG.info("Try to find the Bean with type:" + type + " from OSGi services and get error: " + ex);
        }
    }
    list.addAll(orig.getBeansOfType(type));
    return list;
}
Also used : ArrayList(java.util.ArrayList) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) NoSuchComponentException(org.osgi.service.blueprint.container.NoSuchComponentException) ServiceReference(org.osgi.framework.ServiceReference)

Example 54 with ComponentMetadata

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

the class AbstractBPBeanDefinitionParser method getBus.

protected MutableBeanMetadata getBus(ParserContext context, String name) {
    ComponentDefinitionRegistry cdr = context.getComponentDefinitionRegistry();
    ComponentMetadata meta = cdr.getComponentDefinition("blueprintBundle");
    if (!cdr.containsComponentDefinition(InterceptorTypeConverter.class.getName())) {
        MutablePassThroughMetadata md = context.createMetadata(MutablePassThroughMetadata.class);
        md.setObject(new InterceptorTypeConverter());
        md.setId(InterceptorTypeConverter.class.getName());
        context.getComponentDefinitionRegistry().registerTypeConverter(md);
    }
    if (!cdr.containsComponentDefinition(name)) {
        // Create a bus
        MutableBeanMetadata bus = context.createMetadata(MutableBeanMetadata.class);
        bus.setId(name);
        bus.setRuntimeClass(BlueprintBus.class);
        if (meta != null) {
            // blueprint-no-osgi does not provide a bundleContext
            bus.addProperty("bundleContext", createRef(context, "blueprintBundleContext"));
        }
        bus.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
        bus.setDestroyMethod("shutdown");
        bus.setInitMethod("initialize");
        context.getComponentDefinitionRegistry().registerComponentDefinition(bus);
        return bus;
    }
    return (MutableBeanMetadata) cdr.getComponentDefinition(name);
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) 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