Search in sources :

Example 1 with MutableReferenceMetadata

use of org.apache.aries.blueprint.mutable.MutableReferenceMetadata 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 2 with MutableReferenceMetadata

use of org.apache.aries.blueprint.mutable.MutableReferenceMetadata 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)

Example 3 with MutableReferenceMetadata

use of org.apache.aries.blueprint.mutable.MutableReferenceMetadata in project aries by apache.

the class ExtNamespaceHandler method decorateAdditionalInterfaces.

private ComponentMetadata decorateAdditionalInterfaces(Node node, ComponentMetadata component, ParserContext context) {
    if (!(component instanceof MutableReferenceMetadata)) {
        throw new ComponentDefinitionException("Expected an instanceof MutableReferenceMetadata");
    }
    MutableReferenceMetadata mrm = (MutableReferenceMetadata) component;
    List<String> list = new ArrayList<String>();
    Node nd = node.getFirstChild();
    while (nd != null) {
        if (nd instanceof Element && nodeNameEquals(nd, INTERFACE_VALUE)) {
            list.add(((Element) nd).getTextContent());
        }
        nd = nd.getNextSibling();
    }
    mrm.setExtraInterfaces(list);
    return component;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

Example 4 with MutableReferenceMetadata

use of org.apache.aries.blueprint.mutable.MutableReferenceMetadata in project aries by apache.

the class ExtNamespaceHandler method createReference.

private MutableReferenceMetadata createReference(ParserContext context, String value) {
    MutableReferenceMetadata m = context.createMetadata(MutableReferenceMetadata.class);
    // use the class instance directly rather than loading the class from the specified the interface name.
    m.setRuntimeInterface(PropertyEvaluator.class);
    m.setFilter("(org.apache.aries.blueprint.ext.evaluator.name=" + value + ")");
    m.setBundleContext(ctx);
    return m;
}
Also used : MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

Example 5 with MutableReferenceMetadata

use of org.apache.aries.blueprint.mutable.MutableReferenceMetadata 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)

Aggregations

MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)7 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)3 PassThroughMetadata (org.apache.aries.blueprint.PassThroughMetadata)3 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)3 Bundle (org.osgi.framework.Bundle)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 Element (org.w3c.dom.Element)2 ArrayList (java.util.ArrayList)1 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)1 ComponentResolver (org.apache.camel.spi.ComponentResolver)1 DataFormatResolver (org.apache.camel.spi.DataFormatResolver)1 LanguageResolver (org.apache.camel.spi.LanguageResolver)1 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)1 Target (org.osgi.service.blueprint.reflect.Target)1 Node (org.w3c.dom.Node)1