Search in sources :

Example 1 with ReferenceMetadata

use of org.apache.felix.scr.impl.metadata.ReferenceMetadata in project felix by apache.

the class ComponentConstructor method newInstance.

/**
 * Create a new instance
 * @param componentContext The component context
 * @param parameterMap A map of reference parameters for handling references in the
 *                     constructor
 * @return The instance
 * @throws Exception If anything goes wrong, like constructor can't be found etc.
 */
public <T> S newInstance(final ComponentContextImpl<S> componentContext, final Map<ReferenceMetadata, DependencyManager.OpenStatus<S, ?>> parameterMap) throws Exception {
    // no constructor -> throw
    if (constructor == null) {
        throw new InstantiationException("Constructor not found.");
    }
    final Object[] args;
    if (constructorArgTypes == null) {
        args = null;
    } else {
        args = new Object[constructorArgTypes.length];
        for (int i = 0; i < args.length; i++) {
            final ReferenceMetadata refMetadata = this.constructorRefs[i];
            final DependencyManager.OpenStatus<S, ?> status = refMetadata == null ? null : parameterMap.get(refMetadata);
            if (refMetadata == null) {
                args[i] = ValueUtils.getValue(constructor.getDeclaringClass().getName(), constructorArgTypes[i], constructor.getParameterTypes()[i], componentContext, null);
            } else {
                final List<Object> refs = refMetadata.isMultiple() ? new ArrayList<>() : null;
                Object ref = null;
                for (final RefPair<S, ?> refPair : status.refs) {
                    if (!refPair.isDeleted() && !refPair.isFailed()) {
                        if (refPair.getServiceObject(componentContext) == null && (constructorArgTypes[i] == ValueType.ref_serviceType || constructorArgTypes[i] == ValueType.ref_tuple)) {
                            refPair.getServiceObject(componentContext, componentContext.getBundleContext());
                        }
                        ref = ValueUtils.getValue(constructor.getDeclaringClass().getName(), constructorArgTypes[i], constructor.getParameterTypes()[i], componentContext, refPair);
                        if (refMetadata.isMultiple() && ref != null) {
                            refs.add(ref);
                        }
                    }
                }
                if (!refMetadata.isMultiple()) {
                    if (ref == null) {
                        throw new InstantiationException("Unable to get service for reference " + refMetadata.getName());
                    }
                    args[i] = ref;
                } else {
                    args[i] = refs;
                }
            }
        }
    }
    final S component = constructor.newInstance(args);
    // activation fields
    for (int i = 0; i < activationFieldTypes.length; i++) {
        if (activationFieldTypes[i] != null && activationFieldTypes[i] != ValueType.ignore) {
            final Object value = ValueUtils.getValue(constructor.getDeclaringClass().getName(), activationFieldTypes[i], activationFields[i].getType(), componentContext, // null is ok as activation fields are not references
            null);
            FieldUtils.setField(activationFields[i], component, value, componentContext.getLogger());
        }
    }
    return component;
}
Also used : DependencyManager(org.apache.felix.scr.impl.manager.DependencyManager) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata)

Example 2 with ReferenceMetadata

use of org.apache.felix.scr.impl.metadata.ReferenceMetadata in project felix by apache.

the class ComponentMethodsImpl method initComponentMethods.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public synchronized void initComponentMethods(final ComponentMetadata componentMetadata, final Class<T> implementationObjectClass, final ComponentLogger logger) {
    if (m_activateMethod != null) {
        // do init only once
        return;
    }
    DSVersion dsVersion = componentMetadata.getDSVersion();
    boolean configurableServiceProperties = componentMetadata.isConfigurableServiceProperties();
    boolean supportsInterfaces = componentMetadata.isConfigureWithInterfaces();
    m_activateMethod = new ActivateMethod(componentMetadata.getActivate(), componentMetadata.isActivateDeclared(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    m_deactivateMethod = new DeactivateMethod(componentMetadata.getDeactivate(), componentMetadata.isDeactivateDeclared(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    m_modifiedMethod = new ModifiedMethod(componentMetadata.getModified(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    for (ReferenceMetadata referenceMetadata : componentMetadata.getDependencies()) {
        final String refName = referenceMetadata.getName();
        final List<ReferenceMethods> methods = new ArrayList<>();
        if (referenceMetadata.getField() != null) {
            methods.add(new FieldMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties));
        }
        if (referenceMetadata.getBind() != null) {
            methods.add(new BindMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties));
        }
        if (methods.isEmpty()) {
            bindMethodMap.put(refName, ReferenceMethods.NOPReferenceMethod);
        } else if (methods.size() == 1) {
            bindMethodMap.put(refName, methods.get(0));
        } else {
            bindMethodMap.put(refName, new DuplexReferenceMethods(methods));
        }
    }
    m_constructor = new ComponentConstructor(componentMetadata, implementationObjectClass, logger);
}
Also used : FieldMethods(org.apache.felix.scr.impl.inject.field.FieldMethods) ArrayList(java.util.ArrayList) BindMethods(org.apache.felix.scr.impl.inject.methods.BindMethods) ActivateMethod(org.apache.felix.scr.impl.inject.methods.ActivateMethod) DeactivateMethod(org.apache.felix.scr.impl.inject.methods.DeactivateMethod) DSVersion(org.apache.felix.scr.impl.metadata.DSVersion) ModifiedMethod(org.apache.felix.scr.impl.inject.methods.ModifiedMethod) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata)

Example 3 with ReferenceMetadata

use of org.apache.felix.scr.impl.metadata.ReferenceMetadata in project felix by apache.

the class ServiceComponentRuntimeImpl method refsToDTO.

private ReferenceDTO[] refsToDTO(List<ReferenceMetadata> dependencies) {
    ReferenceDTO[] dtos = new ReferenceDTO[dependencies.size()];
    int i = 0;
    for (ReferenceMetadata r : dependencies) {
        ReferenceDTO dto = new ReferenceDTO();
        dto.bind = r.getBind();
        dto.cardinality = r.getCardinality();
        dto.field = r.getField();
        dto.fieldOption = r.getFieldOption();
        dto.interfaceName = r.getInterface();
        dto.name = r.getName();
        dto.policy = r.getPolicy();
        dto.policyOption = r.getPolicyOption();
        dto.scope = r.getScope().name();
        dto.target = r.getTarget();
        dto.unbind = r.getUnbind();
        dto.updated = r.getUpdated();
        dtos[i++] = dto;
    }
    return dtos;
}
Also used : ReferenceDTO(org.osgi.service.component.runtime.dto.ReferenceDTO) SatisfiedReferenceDTO(org.osgi.service.component.runtime.dto.SatisfiedReferenceDTO) UnsatisfiedReferenceDTO(org.osgi.service.component.runtime.dto.UnsatisfiedReferenceDTO) ServiceReferenceDTO(org.osgi.framework.dto.ServiceReferenceDTO) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata)

Example 4 with ReferenceMetadata

use of org.apache.felix.scr.impl.metadata.ReferenceMetadata in project felix by apache.

the class XmlHandler method startElement.

/**
 * Method called when a tag opens
 *
 * @param   uri
 * @param   localName
 * @param   attributes
 * @exception   ParseException
 */
public void startElement(String uri, String localName, Attributes attributes) throws ParseException {
    // So we check this for the first element, we receive.
    if (firstElement) {
        firstElement = false;
        if (localName.equals("component") && "".equals(uri)) {
            overrideNamespace = NAMESPACE_URI;
        }
    }
    if (overrideNamespace != null && "".equals(uri)) {
        uri = overrideNamespace;
    }
    // the namespace - we allow both: with or without namespace!
    if (this.isComponent && "".equals(uri)) {
        uri = NAMESPACE_URI;
    }
    // get the namespace code for the namespace uri
    DSVersion namespaceCode = NAMESPACE_CODE_MAP.get(uri);
    // from now on uri points to the namespace
    if (namespaceCode != null) {
        try {
            // 112.4.3 Component Element
            if (localName.equals("component")) {
                this.isComponent = true;
                // Create a new ComponentMetadata
                m_currentComponent = new ComponentMetadata(namespaceCode);
                // name attribute is optional (since DS 1.1)
                if (attributes.getAttribute("name") != null) {
                    m_currentComponent.setName(attributes.getAttribute("name"));
                }
                // enabled attribute is optional
                if (attributes.getAttribute("enabled") != null) {
                    m_currentComponent.setEnabled(attributes.getAttribute("enabled").equals("true"));
                }
                // immediate attribute is optional
                if (attributes.getAttribute("immediate") != null) {
                    m_currentComponent.setImmediate(attributes.getAttribute("immediate").equals("true"));
                }
                // factory attribute is optional
                if (attributes.getAttribute("factory") != null) {
                    m_currentComponent.setFactoryIdentifier(attributes.getAttribute("factory"));
                }
                // configuration-policy is optional (since DS 1.1)
                if (attributes.getAttribute("configuration-policy") != null) {
                    m_currentComponent.setConfigurationPolicy(attributes.getAttribute("configuration-policy"));
                }
                // activate attribute is optional (since DS 1.1)
                if (attributes.getAttribute("activate") != null) {
                    m_currentComponent.setActivate(attributes.getAttribute("activate"));
                }
                // deactivate attribute is optional (since DS 1.1)
                if (attributes.getAttribute("deactivate") != null) {
                    m_currentComponent.setDeactivate(attributes.getAttribute("deactivate"));
                }
                // modified attribute is optional (since DS 1.1)
                if (attributes.getAttribute("modified") != null) {
                    m_currentComponent.setModified(attributes.getAttribute("modified"));
                }
                // configuration-pid attribute is optional (since DS 1.2)
                String configurationPidString = attributes.getAttribute("configuration-pid");
                if (configurationPidString != null) {
                    String[] configurationPid = configurationPidString.split(" ");
                    m_currentComponent.setConfigurationPid(configurationPid);
                }
                m_currentComponent.setConfigurableServiceProperties("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, CONFIGURABLE_SERVICE_PROPERTIES)));
                m_currentComponent.setPersistentFactoryComponent("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, PERSISTENT_FACTORY_COMPONENT)));
                m_currentComponent.setDeleteCallsModify("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, DELETE_CALLS_MODIFY)));
                if (attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, OBSOLETE_FACTORY_COMPONENT_FACTORY) != null) {
                    m_currentComponent.setObsoleteFactoryComponentFactory("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, OBSOLETE_FACTORY_COMPONENT_FACTORY)));
                } else if (!namespaceCode.isDS13()) {
                    m_currentComponent.setObsoleteFactoryComponentFactory(m_globalObsoleteFactoryComponentFactory);
                }
                m_currentComponent.setConfigureWithInterfaces("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, CONFIGURE_WITH_INTERFACES)));
                m_currentComponent.setDelayedKeepInstances(m_globalDelayedKeepInstances || "true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, DELAYED_KEEP_INSTANCES)));
                // Add this component to the list
                m_components.add(m_currentComponent);
            } else // not inside a component element, ignore current element
            if (!this.isComponent) {
                m_logger.log(LogService.LOG_DEBUG, "Not currently parsing a component; ignoring element {0} (bundle {1})", new Object[] { localName, m_bundle.getLocation() }, null, null, null);
            } else // 112.4.4 Implementation
            if (localName.equals("implementation")) {
                // Set the implementation class name (mandatory)
                m_currentComponent.setImplementationClassName(attributes.getAttribute("class"));
            } else // 112.4.5 [...] Property Elements
            if (localName.equals("property")) {
                PropertyMetadata prop = new PropertyMetadata();
                // name attribute is mandatory
                prop.setName(attributes.getAttribute("name"));
                // type attribute is optional
                if (attributes.getAttribute("type") != null) {
                    prop.setType(attributes.getAttribute("type"));
                }
                // 112.4.5: If the value attribute is specified, the body of the element is ignored.
                if (attributes.getAttribute("value") != null) {
                    prop.setValue(attributes.getAttribute("value"));
                    m_currentComponent.addProperty(prop);
                } else {
                    // hold the metadata pending
                    m_pendingProperty = prop;
                }
            } else // 112.4.5 Properties [...] Elements
            if (localName.equals("properties")) {
                readPropertiesEntry(attributes.getAttribute("entry"));
            } else // 112.4.6 Service Element
            if (localName.equals("service")) {
                m_currentService = new ServiceMetadata();
                // servicefactory attribute is optional
                if (attributes.getAttribute("servicefactory") != null) {
                    m_currentService.setServiceFactory(attributes.getAttribute("servicefactory").equals("true"));
                }
                if (attributes.getAttribute("scope") != null) {
                    m_currentService.setScope(attributes.getAttribute("scope"));
                }
                m_currentComponent.setService(m_currentService);
            } else if (localName.equals("provide")) {
                m_currentService.addProvide(attributes.getAttribute("interface"));
            } else // 112.4.7 Reference element
            if (localName.equals("reference")) {
                ReferenceMetadata ref = new ReferenceMetadata();
                // name attribute is optional (since DS 1.1)
                if (attributes.getAttribute("name") != null) {
                    ref.setName(attributes.getAttribute("name"));
                }
                ref.setInterface(attributes.getAttribute("interface"));
                // Cardinality
                if (attributes.getAttribute("cardinality") != null) {
                    ref.setCardinality(attributes.getAttribute("cardinality"));
                }
                if (attributes.getAttribute("policy") != null) {
                    ref.setPolicy(attributes.getAttribute("policy"));
                }
                if (attributes.getAttribute("policy-option") != null) {
                    ref.setPolicyOption(attributes.getAttribute("policy-option"));
                }
                if (attributes.getAttribute("scope") != null) {
                    ref.setScope(attributes.getAttribute("scope"));
                }
                if (attributes.getAttribute("target") != null) {
                    ref.setTarget(attributes.getAttribute("target"));
                    PropertyMetadata prop = new PropertyMetadata();
                    prop.setName((ref.getName() == null ? ref.getInterface() : ref.getName()) + ".target");
                    prop.setValue(attributes.getAttribute("target"));
                    m_currentComponent.addProperty(prop);
                }
                // method reference
                ref.setBind(attributes.getAttribute("bind"));
                ref.setUpdated(attributes.getAttribute("updated"));
                ref.setUnbind(attributes.getAttribute("unbind"));
                // field reference
                ref.setField(attributes.getAttribute("field"));
                ref.setFieldOption(attributes.getAttribute("field-option"));
                ref.setFieldCollectionType(attributes.getAttribute("field-collection-type"));
                m_currentComponent.addDependency(ref);
            } else // used by the Maven SCR Plugin, which is just silently ignored)
            if (!localName.equals("components")) {
                m_logger.log(LogService.LOG_DEBUG, "Ignoring unsupported element {0} (bundle {1})", new Object[] { localName, m_bundle.getLocation() }, null, null, null);
            }
        } catch (Exception ex) {
            throw new ParseException("Exception during parsing", ex);
        }
    } else // used by the Maven SCR Plugin, which is just silently ignored)
    if (!localName.equals("components")) {
        m_logger.log(LogService.LOG_DEBUG, "Ignoring unsupported element '{'{0}'}'{1} (bundle {2})", new Object[] { uri, localName, m_bundle.getLocation() }, null, null, null);
    }
}
Also used : DSVersion(org.apache.felix.scr.impl.metadata.DSVersion) PropertyMetadata(org.apache.felix.scr.impl.metadata.PropertyMetadata) ParseException(org.apache.felix.scr.impl.parser.ParseException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) ServiceMetadata(org.apache.felix.scr.impl.metadata.ServiceMetadata) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata) IOException(java.io.IOException) ParseException(org.apache.felix.scr.impl.parser.ParseException)

Example 5 with ReferenceMetadata

use of org.apache.felix.scr.impl.metadata.ReferenceMetadata in project felix by apache.

the class ComponentMethodsImpl method initComponentMethods.

public synchronized void initComponentMethods(ComponentMetadata componentMetadata, Class<?> implementationObjectClass) {
    if (m_activateMethod != null) {
        return;
    }
    DSVersion dsVersion = componentMetadata.getDSVersion();
    boolean configurableServiceProperties = componentMetadata.isConfigurableServiceProperties();
    boolean supportsInterfaces = componentMetadata.isConfigureWithInterfaces();
    m_activateMethod = new ActivateMethod(componentMetadata.getActivate(), componentMetadata.isActivateDeclared(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    m_deactivateMethod = new DeactivateMethod(componentMetadata.getDeactivate(), componentMetadata.isDeactivateDeclared(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    m_modifiedMethod = new ModifiedMethod(componentMetadata.getModified(), implementationObjectClass, dsVersion, configurableServiceProperties, supportsInterfaces);
    for (ReferenceMetadata referenceMetadata : componentMetadata.getDependencies()) {
        final String refName = referenceMetadata.getName();
        final ReferenceMethods methods;
        if (referenceMetadata.getField() != null && referenceMetadata.getBind() != null) {
            methods = new DuplexReferenceMethods(new FieldMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties), new BindMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties));
        } else if (referenceMetadata.getField() != null) {
            methods = new FieldMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties);
        } else {
            methods = new BindMethods(referenceMetadata, implementationObjectClass, dsVersion, configurableServiceProperties);
        }
        bindMethodMap.put(refName, methods);
    }
}
Also used : DuplexReferenceMethods(org.apache.felix.scr.impl.inject.DuplexReferenceMethods) FieldMethods(org.apache.felix.scr.impl.inject.FieldMethods) DeactivateMethod(org.apache.felix.scr.impl.inject.DeactivateMethod) DSVersion(org.apache.felix.scr.impl.metadata.DSVersion) ModifiedMethod(org.apache.felix.scr.impl.inject.ModifiedMethod) BindMethods(org.apache.felix.scr.impl.inject.BindMethods) ActivateMethod(org.apache.felix.scr.impl.inject.ActivateMethod) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata) DuplexReferenceMethods(org.apache.felix.scr.impl.inject.DuplexReferenceMethods) ReferenceMethods(org.apache.felix.scr.impl.helper.ReferenceMethods)

Aggregations

ReferenceMetadata (org.apache.felix.scr.impl.metadata.ReferenceMetadata)7 DSVersion (org.apache.felix.scr.impl.metadata.DSVersion)3 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 ReferenceMethods (org.apache.felix.scr.impl.helper.ReferenceMethods)1 ActivateMethod (org.apache.felix.scr.impl.inject.ActivateMethod)1 BindMethods (org.apache.felix.scr.impl.inject.BindMethods)1 DeactivateMethod (org.apache.felix.scr.impl.inject.DeactivateMethod)1 DuplexReferenceMethods (org.apache.felix.scr.impl.inject.DuplexReferenceMethods)1 FieldMethods (org.apache.felix.scr.impl.inject.FieldMethods)1 ModifiedMethod (org.apache.felix.scr.impl.inject.ModifiedMethod)1 FieldMethods (org.apache.felix.scr.impl.inject.field.FieldMethods)1 ActivateMethod (org.apache.felix.scr.impl.inject.methods.ActivateMethod)1 BindMethods (org.apache.felix.scr.impl.inject.methods.BindMethods)1 DeactivateMethod (org.apache.felix.scr.impl.inject.methods.DeactivateMethod)1 ModifiedMethod (org.apache.felix.scr.impl.inject.methods.ModifiedMethod)1 DependencyManager (org.apache.felix.scr.impl.manager.DependencyManager)1 ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)1