Search in sources :

Example 1 with ComponentMetadata

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

the class BlueprintContainerRegistry method lookupByType.

public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type, boolean includeNonSingletons) {
    Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
    Map<String, T> objects = new LinkedHashMap<String, T>();
    Set<String> ids = blueprintContainer.getComponentIds();
    for (String id : ids) {
        try {
            ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
            Class<?> cl = null;
            if (metadata instanceof BeanMetadata) {
                BeanMetadata beanMetadata = (BeanMetadata) metadata;
                // should we skip the bean if its prototype and we are only looking for singletons?
                if (!includeNonSingletons) {
                    String scope = beanMetadata.getScope();
                    if (BeanMetadata.SCOPE_PROTOTYPE.equals(scope)) {
                        continue;
                    }
                }
                cl = bundle.loadClass(beanMetadata.getClassName());
            } else if (metadata instanceof ReferenceMetadata) {
                ReferenceMetadata referenceMetadata = (ReferenceMetadata) metadata;
                cl = bundle.loadClass(referenceMetadata.getInterface());
            }
            if (cl != null && type.isAssignableFrom(cl)) {
                Object o = blueprintContainer.getComponentInstance(metadata.getId());
                objects.put(metadata.getId(), type.cast(o));
            }
        } catch (Throwable t) {
        // ignore
        }
    }
    return objects;
}
Also used : BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Bundle(org.osgi.framework.Bundle) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ComponentMetadata

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

the class BlueprintPropertiesParser method lookupPropertyPlaceholderIds.

/**
     * Lookup the ids of the Blueprint property placeholder services in the
     * Blueprint container.
     *
     * @return the ids, will be an empty array if none found.
     */
public String[] lookupPropertyPlaceholderIds() {
    List<String> ids = new ArrayList<String>();
    for (Object componentId : container.getComponentIds()) {
        String id = (String) componentId;
        ComponentMetadata meta = container.getComponentMetadata(id);
        if (meta instanceof ExtendedBeanMetadata) {
            Class<?> clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass();
            if (clazz != null && AbstractPropertyPlaceholder.class.isAssignableFrom(clazz)) {
                ids.add(id);
            }
        }
    }
    return ids.toArray(new String[ids.size()]);
}
Also used : ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ArrayList(java.util.ArrayList) AbstractPropertyPlaceholder(org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 3 with ComponentMetadata

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

the class CamelNamespaceHandler method getLanguageResolverReference.

private static ComponentMetadata getLanguageResolverReference(ParserContext context, String language) {
    // we cannot resolve language names using property placeholders at this point in time
    if (language.startsWith(PropertiesComponent.DEFAULT_PREFIX_TOKEN)) {
        return null;
    }
    ComponentDefinitionRegistry componentDefinitionRegistry = context.getComponentDefinitionRegistry();
    ComponentMetadata cm = componentDefinitionRegistry.getComponentDefinition(".camelBlueprint.languageResolver." + language);
    if (cm == null) {
        MutableReferenceMetadata svc = context.createMetadata(MutableReferenceMetadata.class);
        svc.setId(".camelBlueprint.languageResolver." + language);
        svc.setFilter("(language=" + language + ")");
        svc.setAvailability(componentDefinitionRegistry.containsComponentDefinition(language) ? AVAILABILITY_OPTIONAL : AVAILABILITY_MANDATORY);
        try {
            // Try to set the runtime interface (only with aries blueprint > 0.1
            svc.getClass().getMethod("setRuntimeInterface", Class.class).invoke(svc, LanguageResolver.class);
        } catch (Throwable t) {
            // Check if the bundle can see the class
            try {
                PassThroughMetadata ptm = (PassThroughMetadata) componentDefinitionRegistry.getComponentDefinition("blueprintBundle");
                Bundle b = (Bundle) ptm.getObject();
                if (b.loadClass(LanguageResolver.class.getName()) != LanguageResolver.class) {
                    throw new UnsupportedOperationException();
                }
                svc.setInterface(LanguageResolver.class.getName());
            } catch (Throwable t2) {
                throw new UnsupportedOperationException();
            }
        }
        componentDefinitionRegistry.registerComponentDefinition(svc);
        cm = svc;
    }
    return cm;
}
Also used : ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) LanguageResolver(org.apache.camel.spi.LanguageResolver) Bundle(org.osgi.framework.Bundle) MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

Example 4 with ComponentMetadata

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

the class CamelNamespaceHandler method parseCamelContextNode.

private Metadata parseCamelContextNode(Element element, ParserContext context) {
    LOG.trace("Parsing CamelContext {}", element);
    // Find the id, generate one if needed
    String contextId = element.getAttribute("id");
    boolean implicitId = false;
    // let's avoid folks having to explicitly give an ID to a camel context
    if (ObjectHelper.isEmpty(contextId)) {
        // if no explicit id was set then use a default auto generated name
        CamelContextNameStrategy strategy = new DefaultCamelContextNameStrategy();
        contextId = strategy.getName();
        element.setAttributeNS(null, "id", contextId);
        implicitId = true;
    }
    // now let's parse the routes with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof CamelContextFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + CamelContextFactoryBean.class);
    }
    CamelContextFactoryBean ccfb = (CamelContextFactoryBean) value;
    ccfb.setImplicitId(implicitId);
    // The properties component is always used / created by the CamelContextFactoryBean
    // so we need to ensure that the resolver is ready to use
    ComponentMetadata propertiesComponentResolver = getComponentResolverReference(context, "properties");
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + contextId);
    factory.setObject(new PassThroughCallable<Object>(value));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + contextId);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    factory2.addProperty("bundleContext", createRef(context, "blueprintBundleContext"));
    factory2.addDependsOn(propertiesComponentResolver.getId());
    // We need to add other components which the camel context dependsOn
    if (ObjectHelper.isNotEmpty(ccfb.getDependsOn())) {
        factory2.setDependsOn(Arrays.asList(ccfb.getDependsOn().split(" |,")));
    }
    context.getComponentDefinitionRegistry().registerComponentDefinition(factory2);
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(contextId);
    ctx.setRuntimeClass(BlueprintCamelContext.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getContext");
    ctx.setInitMethod("init");
    ctx.setDestroyMethod("destroy");
    // Register factory beans
    registerBeans(context, contextId, ccfb.getThreadPools());
    registerBeans(context, contextId, ccfb.getEndpoints());
    registerBeans(context, contextId, ccfb.getRedeliveryPolicies());
    registerBeans(context, contextId, ccfb.getBeansFactory());
    // Register processors
    MutablePassThroughMetadata beanProcessorFactory = context.createMetadata(MutablePassThroughMetadata.class);
    beanProcessorFactory.setId(".camelBlueprint.processor.bean.passThrough." + contextId);
    beanProcessorFactory.setObject(new PassThroughCallable<Object>(new CamelInjector(contextId)));
    MutableBeanMetadata beanProcessor = context.createMetadata(MutableBeanMetadata.class);
    beanProcessor.setId(".camelBlueprint.processor.bean." + contextId);
    beanProcessor.setRuntimeClass(CamelInjector.class);
    beanProcessor.setFactoryComponent(beanProcessorFactory);
    beanProcessor.setFactoryMethod("call");
    beanProcessor.setProcessor(true);
    beanProcessor.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    context.getComponentDefinitionRegistry().registerComponentDefinition(beanProcessor);
    MutablePassThroughMetadata regProcessorFactory = context.createMetadata(MutablePassThroughMetadata.class);
    regProcessorFactory.setId(".camelBlueprint.processor.registry.passThrough." + contextId);
    regProcessorFactory.setObject(new PassThroughCallable<Object>(new CamelDependenciesFinder(contextId, context)));
    MutableBeanMetadata regProcessor = context.createMetadata(MutableBeanMetadata.class);
    regProcessor.setId(".camelBlueprint.processor.registry." + contextId);
    regProcessor.setRuntimeClass(CamelDependenciesFinder.class);
    regProcessor.setFactoryComponent(regProcessorFactory);
    regProcessor.setFactoryMethod("call");
    regProcessor.setProcessor(true);
    regProcessor.addDependsOn(".camelBlueprint.processor.bean." + contextId);
    regProcessor.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    context.getComponentDefinitionRegistry().registerComponentDefinition(regProcessor);
    // lets inject the namespaces into any namespace aware POJOs
    injectNamespaces(element, binder);
    LOG.trace("Parsing CamelContext done, returning {}", ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) CamelContextNameStrategy(org.apache.camel.spi.CamelContextNameStrategy) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) CamelContextFactoryBean(org.apache.camel.blueprint.CamelContextFactoryBean)

Example 5 with ComponentMetadata

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

the class CamelNamespaceHandler method getComponentResolverReference.

private static ComponentMetadata getComponentResolverReference(ParserContext context, String component) {
    // we cannot resolve component names using property placeholders at this point in time
    if (component.startsWith(PropertiesComponent.DEFAULT_PREFIX_TOKEN)) {
        return null;
    }
    ComponentDefinitionRegistry componentDefinitionRegistry = context.getComponentDefinitionRegistry();
    ComponentMetadata cm = componentDefinitionRegistry.getComponentDefinition(".camelBlueprint.componentResolver." + component);
    if (cm == null) {
        MutableReferenceMetadata svc = context.createMetadata(MutableReferenceMetadata.class);
        svc.setId(".camelBlueprint.componentResolver." + component);
        svc.setFilter("(component=" + component + ")");
        svc.setAvailability(componentDefinitionRegistry.containsComponentDefinition(component) ? AVAILABILITY_OPTIONAL : AVAILABILITY_MANDATORY);
        try {
            // Try to set the runtime interface (only with aries blueprint > 0.1
            svc.getClass().getMethod("setRuntimeInterface", Class.class).invoke(svc, ComponentResolver.class);
        } catch (Throwable t) {
            // Check if the bundle can see the class
            try {
                PassThroughMetadata ptm = (PassThroughMetadata) componentDefinitionRegistry.getComponentDefinition("blueprintBundle");
                Bundle b = (Bundle) ptm.getObject();
                if (b.loadClass(ComponentResolver.class.getName()) != ComponentResolver.class) {
                    throw new UnsupportedOperationException();
                }
                svc.setInterface(ComponentResolver.class.getName());
            } catch (Throwable t2) {
                throw new UnsupportedOperationException();
            }
        }
        componentDefinitionRegistry.registerComponentDefinition(svc);
        cm = svc;
    }
    return cm;
}
Also used : ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) Bundle(org.osgi.framework.Bundle) ComponentResolver(org.apache.camel.spi.ComponentResolver) MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

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