Search in sources :

Example 1 with MutableServiceMetadata

use of org.apache.aries.blueprint.mutable.MutableServiceMetadata in project karaf by apache.

the class NamespaceHandler method parseCommand.

private void parseCommand(Element element, ParserContext context) {
    MutableBeanMetadata command = context.createMetadata(MutableBeanMetadata.class);
    command.setRuntimeClass(BlueprintCommand.class);
    command.addProperty(BLUEPRINT_CONTAINER, createRef(context, BLUEPRINT_CONTAINER));
    command.addProperty(BLUEPRINT_CONVERTER, createRef(context, BLUEPRINT_CONVERTER));
    NodeList children = element.getChildNodes();
    MutableBeanMetadata action = null;
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            Element childElement = (Element) child;
            if (nodeNameEquals(childElement, ACTION)) {
                action = parseAction(context, command, childElement);
                action.setId(getName());
                context.getComponentDefinitionRegistry().registerComponentDefinition(action);
                command.addProperty(ACTION_ID, createIdRef(context, action.getId()));
            } else if (nodeNameEquals(childElement, COMPLETERS)) {
                command.addProperty(COMPLETERS, parseCompleters(context, command, childElement));
            } else if (nodeNameEquals(childElement, OPTIONAL_COMPLETERS)) {
                command.addProperty(OPTIONAL_COMPLETERS_PROPERTY, parseOptionalCompleters(context, command, childElement));
            } else {
                throw new ComponentDefinitionException("Bad xml syntax: unknown element '" + childElement.getNodeName() + "'");
            }
        }
    }
    MutableServiceMetadata commandService = context.createMetadata(MutableServiceMetadata.class);
    commandService.setActivation(MutableServiceMetadata.ACTIVATION_LAZY);
    commandService.setId(getName());
    commandService.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    commandService.setServiceComponent(command);
    String scope;
    String function;
    if (SHELL_NAMESPACE_1_0_0.equals(element.getNamespaceURI())) {
        String location = element.getAttribute(NAME);
        location = location.replace('/', ':');
        if (location.lastIndexOf(':') >= 0) {
            scope = location.substring(0, location.lastIndexOf(':'));
            function = location.substring(location.lastIndexOf(':') + 1);
        } else {
            scope = "";
            function = location;
        }
    } else {
        try {
            Class actionClass = getBundle(context).loadClass(action.getClassName());
            scope = getScope(actionClass);
            function = getName(actionClass);
        } catch (Throwable e) {
            throw new ComponentDefinitionException("Unable to introspect action " + action.getClassName(), e);
        }
    }
    commandService.addServiceProperty(createStringValue(context, "osgi.command.scope"), createStringValue(context, scope));
    commandService.addServiceProperty(createStringValue(context, "osgi.command.function"), createStringValue(context, function));
    context.getComponentDefinitionRegistry().registerComponentDefinition(commandService);
    String subShellName = null;
    if (scope != null && !scope.isEmpty()) {
        // if it's shell 1.0.0 schema and scope is contained in the descriptor itself
        subShellName = ".subshell." + scope;
    }
    if (subShellName == null || !context.getComponentDefinitionRegistry().containsComponentDefinition(subShellName)) {
        // if the scope is unknown or if the scope has not been defined before
        createSubShell(context, scope, subShellName);
    }
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 2 with MutableServiceMetadata

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

the class AbstractPropertyPlaceholder method processServiceMetadata.

protected Metadata processServiceMetadata(ServiceMetadata component) {
    try {
        if (component instanceof MutableServiceMetadata) {
            processingStack.add("Service Component->");
            ((MutableServiceMetadata) component).setServiceComponent((Target) processMetadata(component.getServiceComponent()));
        } else {
            printWarning(component, "Service Component");
            processingStack.add("Service Component->");
            processMetadata(component.getServiceComponent());
        }
    } finally {
        processingStack.removeLast();
    }
    List<MapEntry> entries = new ArrayList<MapEntry>(component.getServiceProperties());
    if (!!!entries.isEmpty()) {
        try {
            if (component instanceof MutableServiceMetadata) {
                processingStack.add("Service Properties->");
                MutableServiceMetadata msm = (MutableServiceMetadata) component;
                for (MapEntry entry : entries) {
                    msm.removeServiceProperty(entry);
                }
                for (MapEntry entry : processMapEntries(entries)) {
                    msm.addServiceProperty(entry);
                }
            } else {
                printWarning(component, "Service Properties");
                processingStack.add("Service Properties->");
                processMapEntries(entries);
            }
        } finally {
            processingStack.removeLast();
        }
    }
    for (RegistrationListener listener : component.getRegistrationListeners()) {
        Target listenerComponent = listener.getListenerComponent();
        try {
            processingStack.add("Registration Listener " + listenerComponent + "->");
            if (listener instanceof MutableRegistrationListener) {
                ((MutableRegistrationListener) listener).setListenerComponent((Target) processMetadata(listenerComponent));
            } else {
                //Say that we can't change this listener, but continue processing
                //If the value is mutable then we may be ok!
                printWarning(listener, "Service Registration Listener");
                processMetadata(listenerComponent);
            }
        } finally {
            processingStack.removeLast();
        }
    }
    return component;
}
Also used : MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) MutableRegistrationListener(org.apache.aries.blueprint.mutable.MutableRegistrationListener) Target(org.osgi.service.blueprint.reflect.Target) MutableMapEntry(org.apache.aries.blueprint.mutable.MutableMapEntry) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) ArrayList(java.util.ArrayList) MutableRegistrationListener(org.apache.aries.blueprint.mutable.MutableRegistrationListener)

Example 3 with MutableServiceMetadata

use of org.apache.aries.blueprint.mutable.MutableServiceMetadata in project controller by opendaylight.

the class OpendaylightNamespaceHandler method decorateServiceType.

private static ComponentMetadata decorateServiceType(final Attr attr, final ComponentMetadata component, final ParserContext context) {
    if (!(component instanceof MutableServiceMetadata)) {
        throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata");
    }
    MutableServiceMetadata service = (MutableServiceMetadata) component;
    LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue());
    service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue()));
    return component;
}
Also used : MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 4 with MutableServiceMetadata

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

the class SpringOsgiNamespaceHandler method parseService.

private Metadata parseService(Element element, ParserContext context) {
    MutableServiceMetadata metadata = context.createMetadata(MutableServiceMetadata.class);
    // Parse attributes
    if (element.hasAttribute(ID_ATTRIBUTE)) {
        metadata.setId(element.getAttribute(ID_ATTRIBUTE));
    } else {
        metadata.setId(generateId(context));
    }
    if (nonEmpty(element.getAttribute(REF_ATTRIBUTE)) != null) {
        MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
        ref.setComponentId(element.getAttribute(REF_ATTRIBUTE));
        metadata.setServiceComponent(ref);
    }
    metadata.setRanking(nonEmpty(element.getAttribute(RANKING_ATTRIBUTE)) != null ? Integer.parseInt(element.getAttribute(RANKING_ATTRIBUTE)) : 0);
    String itf = nonEmpty(element.getAttribute(INTERFACE_ATTRIBUTE));
    if (itf != null) {
        metadata.addInterface(itf);
    }
    String[] dependsOn = StringUtils.tokenizeToStringArray(nonEmpty(element.getAttribute(DEPENDS_ON_ATTRIBUTE)), ",; ");
    metadata.setDependsOn(dependsOn != null ? Arrays.asList(dependsOn) : null);
    String autoExp = nonEmpty(element.getAttribute(AUTO_EXPORT_ATTRIBUTE));
    if (AUTO_EXPORT_INTERFACES.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL_CLASSES.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_DISABLED);
    }
    // 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);
                for (String intf : itfs) {
                    metadata.addInterface(intf);
                }
            } else if (REGISTRATION_LISTENER_ELEMENT.equals(child.getLocalName())) {
                String regMethod = nonEmpty(child.getAttribute(REGISTRATION_METHOD_ATTRIBUTE));
                String unregMethod = nonEmpty(child.getAttribute(UNREGISTRATION_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.addRegistrationListener(listenerComponent, regMethod, unregMethod);
            } else if (SERVICE_PROPERTIES_ELEMENT.equals(child.getLocalName())) {
                // TODO: @key-type
                for (Element e : getChildren(child)) {
                    if (ENTRY_ELEMENT.equals(e.getLocalName())) {
                        NonNullMetadata key;
                        Metadata val;
                        boolean hasKeyAttribute = e.hasAttribute(KEY_ATTRIBUTE);
                        boolean hasKeyRefAttribute = e.hasAttribute(KEY_REF_ATTRIBUTE);
                        if (hasKeyRefAttribute && !hasKeyAttribute) {
                            MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
                            r.setComponentId(e.getAttribute(KEY_REF_ATTRIBUTE));
                            key = r;
                        } else if (hasKeyAttribute && !hasKeyRefAttribute) {
                            MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
                            v.setStringValue(e.getAttribute(KEY_ATTRIBUTE));
                            key = v;
                        } else {
                            throw new IllegalStateException("Either key or key-ref must be specified");
                        }
                        // TODO: support key
                        boolean hasValAttribute = e.hasAttribute(VALUE_ATTRIBUTE);
                        boolean hasValRefAttribute = e.hasAttribute(VALUE_REF_ATTRIBUTE);
                        if (hasValRefAttribute && !hasValAttribute) {
                            MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
                            r.setComponentId(e.getAttribute(VALUE_REF_ATTRIBUTE));
                            val = r;
                        } else if (hasValAttribute && !hasValRefAttribute) {
                            MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
                            v.setStringValue(e.getAttribute(VALUE_ATTRIBUTE));
                            val = v;
                        } else {
                            throw new IllegalStateException("Either val or val-ref must be specified");
                        }
                        // TODO: support children elements ?
                        metadata.addServiceProperty(key, val);
                    }
                }
            }
        } else if (BLUEPRINT_NAMESPACE.equals(child.getNamespaceURI()) && BEAN_ELEMENT.equals(child.getLocalName())) {
            if (metadata.getServiceComponent() != null) {
                throw new IllegalArgumentException("Only one of @ref attribute and bean element is allowed");
            }
            Target bean = context.parseElement(BeanMetadata.class, metadata, child);
            metadata.setServiceComponent(bean);
        } else {
            if (metadata.getServiceComponent() != null) {
                throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
            }
            NamespaceHandler handler = context.getNamespaceHandler(URI.create(child.getNamespaceURI()));
            if (handler == null) {
                throw new IllegalStateException("No NamespaceHandler found for " + child.getNamespaceURI());
            }
            Metadata md = handler.parse(child, context);
            if (!(md instanceof Target)) {
                throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
            }
            metadata.setServiceComponent((Target) md);
        }
    }
    return metadata;
}
Also used : Element(org.w3c.dom.Element) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 5 with MutableServiceMetadata

use of org.apache.aries.blueprint.mutable.MutableServiceMetadata in project karaf by apache.

the class NamespaceHandler method parseKeystore.

public ComponentMetadata parseKeystore(Element element, ParserContext context) {
    MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
    bean.setRuntimeClass(ResourceKeystoreInstance.class);
    // Parse name
    String name = element.getAttribute("name");
    bean.addProperty("name", createValue(context, name));
    // Parse rank
    String rank = element.getAttribute("rank");
    if (rank != null && rank.length() > 0) {
        bean.addProperty("rank", createValue(context, rank));
    }
    // Parse path
    String path = element.getAttribute("path");
    if (path != null && path.length() > 0) {
        bean.addProperty("path", createValue(context, path));
    }
    // Parse keystorePassword
    String keystorePassword = element.getAttribute("keystorePassword");
    if (keystorePassword != null && keystorePassword.length() > 0) {
        bean.addProperty("keystorePassword", createValue(context, keystorePassword));
    }
    // Parse keyPasswords
    String keyPasswords = element.getAttribute("keyPasswords");
    if (keyPasswords != null && keyPasswords.length() > 0) {
        bean.addProperty("keyPasswords", createValue(context, keyPasswords));
    }
    // Publish Config
    MutableServiceMetadata service = context.createMetadata(MutableServiceMetadata.class);
    service.setId(name);
    service.setServiceComponent(bean);
    service.addInterface(KeystoreInstance.class.getName());
    return service;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) KeystoreInstance(org.apache.karaf.jaas.config.KeystoreInstance) ResourceKeystoreInstance(org.apache.karaf.jaas.config.impl.ResourceKeystoreInstance)

Aggregations

MutableServiceMetadata (org.apache.aries.blueprint.mutable.MutableServiceMetadata)7 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)4 Element (org.w3c.dom.Element)3 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)2 Target (org.osgi.service.blueprint.reflect.Target)2 NodeList (org.w3c.dom.NodeList)2 ArrayList (java.util.ArrayList)1 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)1 MutableMapEntry (org.apache.aries.blueprint.mutable.MutableMapEntry)1 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)1 MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)1 MutableRegistrationListener (org.apache.aries.blueprint.mutable.MutableRegistrationListener)1 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)1 JaasRealm (org.apache.karaf.jaas.config.JaasRealm)1 KeystoreInstance (org.apache.karaf.jaas.config.KeystoreInstance)1 ResourceKeystoreInstance (org.apache.karaf.jaas.config.impl.ResourceKeystoreInstance)1 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)1 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)1 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)1