Search in sources :

Example 71 with MutableBeanMetadata

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

the class BlueprintNamespaceHandler method getOrCreateParserContext.

private org.springframework.beans.factory.xml.ParserContext getOrCreateParserContext(ParserContext parserContext) {
    ComponentDefinitionRegistry registry = parserContext.getComponentDefinitionRegistry();
    ExtendedBlueprintContainer container = getBlueprintContainer(parserContext);
    // Create spring application context
    SpringApplicationContext applicationContext = getPassThrough(parserContext, SPRING_APPLICATION_CONTEXT_ID, SpringApplicationContext.class);
    if (applicationContext == null) {
        applicationContext = new SpringApplicationContext(container);
        registry.registerComponentDefinition(createPassThrough(parserContext, SPRING_APPLICATION_CONTEXT_ID, applicationContext, "destroy"));
    }
    // Create registry
    DefaultListableBeanFactory beanFactory = getPassThrough(parserContext, SPRING_BEAN_FACTORY_ID, DefaultListableBeanFactory.class);
    if (beanFactory == null) {
        beanFactory = applicationContext.getBeanFactory();
        registry.registerComponentDefinition(createPassThrough(parserContext, SPRING_BEAN_FACTORY_ID, beanFactory));
    }
    // Create spring parser context
    org.springframework.beans.factory.xml.ParserContext springParserContext = getPassThrough(parserContext, SPRING_CONTEXT_ID, org.springframework.beans.factory.xml.ParserContext.class);
    if (springParserContext == null) {
        // Create spring context
        springParserContext = createSpringParserContext(parserContext, beanFactory);
        registry.registerComponentDefinition(createPassThrough(parserContext, SPRING_CONTEXT_ID, springParserContext));
    }
    // Create processor
    if (!parserContext.getComponentDefinitionRegistry().containsComponentDefinition(SPRING_BEAN_PROCESSOR_ID)) {
        MutableBeanMetadata bm = parserContext.createMetadata(MutableBeanMetadata.class);
        bm.setId(SPRING_BEAN_PROCESSOR_ID);
        bm.setProcessor(true);
        bm.setScope(BeanMetadata.SCOPE_SINGLETON);
        bm.setRuntimeClass(SpringBeanProcessor.class);
        bm.setActivation(BeanMetadata.ACTIVATION_EAGER);
        bm.addArgument(createRef(parserContext, "blueprintBundleContext"), null, 0);
        bm.addArgument(createRef(parserContext, "blueprintContainer"), null, 0);
        bm.addArgument(createRef(parserContext, SPRING_APPLICATION_CONTEXT_ID), null, 0);
        registry.registerComponentDefinition(bm);
    }
    // Add the namespace handlers' bundle to the application context classloader
    for (URI uri : parserContext.getNamespaces()) {
        NamespaceHandler ns = parserContext.getNamespaceHandler(uri);
        if (ns instanceof BlueprintNamespaceHandler) {
            applicationContext.addSourceBundle(((BlueprintNamespaceHandler) ns).bundle);
        }
    }
    return springParserContext;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ExtendedBlueprintContainer(org.apache.aries.blueprint.services.ExtendedBlueprintContainer) URI(java.net.URI)

Example 72 with MutableBeanMetadata

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

the class CmNamespaceHandler method parseManagedServiceFactory.

private ComponentMetadata parseManagedServiceFactory(ParserContext context, Element element) {
    String id = getId(context, element);
    MutableBeanMetadata factoryMetadata = context.createMetadata(MutableBeanMetadata.class);
    generateIdIfNeeded(context, factoryMetadata);
    factoryMetadata.addProperty("id", createValue(context, factoryMetadata.getId()));
    factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    factoryMetadata.setRuntimeClass(CmManagedServiceFactory.class);
    factoryMetadata.setInitMethod("init");
    factoryMetadata.setDestroyMethod("destroy");
    factoryMetadata.addArgument(createRef(context, "blueprintContainer"), null, 0);
    factoryMetadata.addProperty("factoryPid", createValue(context, element.getAttribute(FACTORY_PID_ATTRIBUTE)));
    String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
    if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_DISABLED);
    } else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
    }
    factoryMetadata.addProperty("autoExport", createValue(context, autoExport));
    String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
    factoryMetadata.addProperty("ranking", createValue(context, ranking));
    List<String> interfaces = null;
    if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
        interfaces = Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE));
        factoryMetadata.addProperty("interfaces", createList(context, interfaces));
    }
    // Parse elements
    List<RegistrationListener> listeners = new ArrayList<RegistrationListener>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (isBlueprintNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, INTERFACES_ELEMENT)) {
                    if (interfaces != null) {
                        throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
                    }
                    interfaces = parseInterfaceNames(e);
                    factoryMetadata.addProperty("interfaces", createList(context, interfaces));
                } else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
                    MapMetadata map = context.parseElement(MapMetadata.class, factoryMetadata, e);
                    factoryMetadata.addProperty("serviceProperties", map);
                    NodeList enl = e.getChildNodes();
                    for (int j = 0; j < enl.getLength(); j++) {
                        Node enode = enl.item(j);
                        if (enode instanceof Element) {
                            if (isCmNamespace(enode.getNamespaceURI()) && nodeNameEquals(enode, CM_PROPERTIES_ELEMENT)) {
                                decorateCmProperties(context, (Element) enode, factoryMetadata);
                            }
                        }
                    }
                } else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
                    listeners.add(context.parseElement(RegistrationListener.class, factoryMetadata, e));
                }
            } else if (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, MANAGED_COMPONENT_ELEMENT)) {
                    MutableBeanMetadata managedComponent = context.parseElement(MutableBeanMetadata.class, null, e);
                    generateIdIfNeeded(context, managedComponent);
                    managedComponent.setScope(BeanMetadata.SCOPE_PROTOTYPE);
                    // destroy-method on managed-component has different signature than on regular beans
                    // so we'll handle it differently
                    String destroyMethod = managedComponent.getDestroyMethod();
                    if (destroyMethod != null) {
                        factoryMetadata.addProperty("componentDestroyMethod", createValue(context, destroyMethod));
                        managedComponent.setDestroyMethod(null);
                    }
                    context.getComponentDefinitionRegistry().registerComponentDefinition(managedComponent);
                    factoryMetadata.addProperty("managedComponentName", createIdRef(context, managedComponent.getId()));
                }
            }
        }
    }
    MutableCollectionMetadata listenerCollection = context.createMetadata(MutableCollectionMetadata.class);
    listenerCollection.setCollectionClass(List.class);
    for (RegistrationListener listener : listeners) {
        MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
        bean.setRuntimeClass(ServiceListener.class);
        bean.addProperty("listener", listener.getListenerComponent());
        bean.addProperty("registerMethod", createValue(context, listener.getRegistrationMethod()));
        bean.addProperty("unregisterMethod", createValue(context, listener.getUnregistrationMethod()));
        listenerCollection.addValue(bean);
    }
    factoryMetadata.addProperty("listeners", listenerCollection);
    context.getComponentDefinitionRegistry().registerComponentDefinition(factoryMetadata);
    MutableBeanMetadata mapMetadata = context.createMetadata(MutableBeanMetadata.class);
    mapMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    mapMetadata.setId(id);
    mapMetadata.setFactoryComponent(createRef(context, factoryMetadata.getId()));
    mapMetadata.setFactoryMethod("getServiceMap");
    return mapMetadata;
}
Also used : RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 73 with MutableBeanMetadata

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

the class CmNamespaceHandler method decorateManagedProperties.

private ComponentMetadata decorateManagedProperties(ParserContext context, Element element, ComponentMetadata component) {
    if (!(component instanceof MutableBeanMetadata)) {
        throw new ComponentDefinitionException("Element " + MANAGED_PROPERTIES_ELEMENT + " must be used inside a <bp:bean> element");
    }
    generateIdIfNeeded(context, ((MutableBeanMetadata) component));
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setRuntimeClass(CmManagedProperties.class);
    String persistentId = element.getAttribute(PERSISTENT_ID_ATTRIBUTE);
    // ManagedService if the persistentId is not an empty string.
    if (persistentId.length() > 0) {
        metadata.setInitMethod("init");
        metadata.setDestroyMethod("destroy");
    }
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    metadata.addProperty("persistentId", createValue(context, persistentId));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    if (element.hasAttribute(UPDATE_METHOD_ATTRIBUTE)) {
        metadata.addProperty("updateMethod", createValue(context, element.getAttribute(UPDATE_METHOD_ATTRIBUTE)));
    } else if ("component-managed".equals(updateStrategy)) {
        throw new ComponentDefinitionException(UPDATE_METHOD_ATTRIBUTE + " attribute must be set when " + UPDATE_STRATEGY_ATTRIBUTE + " is set to 'component-managed'");
    }
    metadata.addProperty("beanName", createIdRef(context, component.getId()));
    context.getComponentDefinitionRegistry().registerComponentDefinition(metadata);
    return component;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 74 with MutableBeanMetadata

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

the class CmNamespaceHandler method registerManagedObjectManager.

private void registerManagedObjectManager(ParserContext context, ComponentDefinitionRegistry registry) {
    if (registry.getComponentDefinition(MANAGED_OBJECT_MANAGER_NAME) == null) {
        MutableBeanMetadata beanMetadata = context.createMetadata(MutableBeanMetadata.class);
        beanMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
        beanMetadata.setId(MANAGED_OBJECT_MANAGER_NAME);
        beanMetadata.setRuntimeClass(ManagedObjectManager.class);
        registry.registerComponentDefinition(beanMetadata);
    }
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata)

Example 75 with MutableBeanMetadata

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

the class CmNamespaceHandler method parsePropertyPlaceholder.

private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    metadata.setRuntimeClass(CmPropertyPlaceholder.class);
    metadata.setInitMethod("init");
    metadata.setDestroyMethod("destroy");
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
    String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
    metadata.addProperty("placeholderPrefix", createValue(context, prefix));
    String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) : "}";
    metadata.addProperty("placeholderSuffix", createValue(context, suffix));
    String nullValue = element.hasAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) : null;
    if (nullValue != null) {
        metadata.addProperty("nullValue", createValue(context, nullValue));
    }
    String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
    if (defaultsRef != null) {
        metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
    }
    String ignoreMissingLocations = extractIgnoreMissingLocations(element);
    if (ignoreMissingLocations != null) {
        metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
    }
    String systemProperties = extractSystemPropertiesAttribute(element);
    if (systemProperties == null) {
        systemProperties = SYSTEM_PROPERTIES_NEVER;
    }
    metadata.addProperty("systemProperties", createValue(context, systemProperties));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    // Parse elements
    List<String> locations = new ArrayList<String>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
                    if (defaultsRef != null) {
                        throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
                    }
                    Metadata props = parseDefaultProperties(context, metadata, e);
                    metadata.addProperty("defaultProperties", props);
                }
            } else if (isExtNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, LOCATION_ELEMENT)) {
                    locations.add(getTextValue(e));
                }
            }
        }
    }
    if (!locations.isEmpty()) {
        metadata.addProperty("locations", createList(context, locations));
    }
    PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return metadata;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) MutableIdRefMetadata(org.apache.aries.blueprint.mutable.MutableIdRefMetadata) MutableComponentMetadata(org.apache.aries.blueprint.mutable.MutableComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Aggregations

MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)96 Element (org.w3c.dom.Element)23 Node (org.w3c.dom.Node)20 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)19 Metadata (org.osgi.service.blueprint.reflect.Metadata)19 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)18 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)18 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)17 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)16 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)15 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)13 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)12 NodeList (org.w3c.dom.NodeList)11 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)10 PassThroughMetadata (org.apache.aries.blueprint.PassThroughMetadata)9 JAXBException (javax.xml.bind.JAXBException)8 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)8 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)8 ExpressionNode (org.apache.camel.model.ExpressionNode)7 Attr (org.w3c.dom.Attr)7