Search in sources :

Example 1 with RegistrationListener

use of org.osgi.service.blueprint.reflect.RegistrationListener in project aries by apache.

the class ServiceMetadataImpl method addRegistrationListener.

public RegistrationListener addRegistrationListener(Target listenerComponent, String registrationMethodName, String unregistrationMethodName) {
    RegistrationListener listener = new RegistrationListenerImpl(listenerComponent, registrationMethodName, unregistrationMethodName);
    addRegistrationListener(listener);
    return listener;
}
Also used : RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener)

Example 2 with RegistrationListener

use of org.osgi.service.blueprint.reflect.RegistrationListener in project aries by apache.

the class BlueprintContainerImpl method getMetadata.

private <T extends ComponentMetadata> void getMetadata(Class<T> clazz, Metadata component, Collection<T> metadatas) {
    if (component == null) {
        return;
    }
    if (clazz.isInstance(component)) {
        metadatas.add(clazz.cast(component));
    }
    if (component instanceof BeanMetadata) {
        getMetadata(clazz, ((BeanMetadata) component).getFactoryComponent(), metadatas);
        for (BeanArgument arg : ((BeanMetadata) component).getArguments()) {
            getMetadata(clazz, arg.getValue(), metadatas);
        }
        for (BeanProperty prop : ((BeanMetadata) component).getProperties()) {
            getMetadata(clazz, prop.getValue(), metadatas);
        }
    }
    if (component instanceof CollectionMetadata) {
        for (Metadata m : ((CollectionMetadata) component).getValues()) {
            getMetadata(clazz, m, metadatas);
        }
    }
    if (component instanceof MapMetadata) {
        for (MapEntry m : ((MapMetadata) component).getEntries()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
    }
    if (component instanceof PropsMetadata) {
        for (MapEntry m : ((PropsMetadata) component).getEntries()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
    }
    if (component instanceof ServiceReferenceMetadata) {
        for (ReferenceListener l : ((ServiceReferenceMetadata) component).getReferenceListeners()) {
            getMetadata(clazz, l.getListenerComponent(), metadatas);
        }
    }
    if (component instanceof ServiceMetadata) {
        getMetadata(clazz, ((ServiceMetadata) component).getServiceComponent(), metadatas);
        for (MapEntry m : ((ServiceMetadata) component).getServiceProperties()) {
            getMetadata(clazz, m.getKey(), metadatas);
            getMetadata(clazz, m.getValue(), metadatas);
        }
        for (RegistrationListener l : ((ServiceMetadata) component).getRegistrationListeners()) {
            getMetadata(clazz, l.getListenerComponent(), metadatas);
        }
    }
}
Also used : BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListener(org.osgi.service.blueprint.reflect.ReferenceListener) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty)

Example 3 with RegistrationListener

use of org.osgi.service.blueprint.reflect.RegistrationListener 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) ArrayList(java.util.ArrayList) 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 4 with RegistrationListener

use of org.osgi.service.blueprint.reflect.RegistrationListener in project aries by apache.

the class AbstractParserProxy method traverseComponent.

/**
	   * Traverse {@link ComponentMetadata} instances to find all nested {@link ComponentMetadata} instances
	   * @param component
	   * @param output
	   */
private void traverseComponent(ComponentMetadata component, Set<ComponentMetadata> output) {
    if (!!!output.add(component))
        return;
    if (component instanceof BeanMetadata) {
        BeanMetadata bean = (BeanMetadata) component;
        traverse(bean.getFactoryComponent(), output);
        for (BeanArgument argument : bean.getArguments()) {
            traverse(argument.getValue(), output);
        }
        for (BeanProperty property : bean.getProperties()) {
            traverse(property.getValue(), output);
        }
    } else if (component instanceof ServiceMetadata) {
        ServiceMetadata service = (ServiceMetadata) component;
        traverse(service.getServiceComponent(), output);
        for (RegistrationListener listener : service.getRegistrationListeners()) {
            traverse(listener.getListenerComponent(), output);
        }
        for (MapEntry e : service.getServiceProperties()) {
            traverse(e.getKey(), output);
            traverse(e.getValue(), output);
        }
    } else if (component instanceof ServiceReferenceMetadata) {
        ServiceReferenceMetadata reference = (ServiceReferenceMetadata) component;
        for (ReferenceListener listener : reference.getReferenceListeners()) {
            traverse(listener.getListenerComponent(), output);
        }
    }
}
Also used : BeanArgument(org.osgi.service.blueprint.reflect.BeanArgument) RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) ReferenceListener(org.osgi.service.blueprint.reflect.ReferenceListener) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty)

Example 5 with RegistrationListener

use of org.osgi.service.blueprint.reflect.RegistrationListener 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)

Aggregations

RegistrationListener (org.osgi.service.blueprint.reflect.RegistrationListener)6 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)3 ArrayList (java.util.ArrayList)2 BeanArgument (org.osgi.service.blueprint.reflect.BeanArgument)2 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)2 BeanProperty (org.osgi.service.blueprint.reflect.BeanProperty)2 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)2 ReferenceListener (org.osgi.service.blueprint.reflect.ReferenceListener)2 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)2 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)2 WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)1 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)1 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)1 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)1 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)1 MutableMapEntry (org.apache.aries.blueprint.mutable.MutableMapEntry)1 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)1 MutableRegistrationListener (org.apache.aries.blueprint.mutable.MutableRegistrationListener)1 MutableServiceMetadata (org.apache.aries.blueprint.mutable.MutableServiceMetadata)1 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)1