Search in sources :

Example 1 with PropertyType

use of org.apache.felix.scrplugin.description.PropertyType in project felix by apache.

the class DSAnnotationProcessor method createComponent.

/**
 * Create a component description.
 *
 * @param cad The component annotation for the class.
 * @param scannedClass The scanned class.
 */
private ComponentDescription createComponent(final ClassAnnotation cad, final ClassDescription describedClass, final ScannedClass scannedClass) throws SCRDescriptorException {
    final ComponentDescription component = new ComponentDescription(cad);
    describedClass.add(component);
    // Although not defined in the spec, we support abstract classes.
    final boolean classIsAbstract = Modifier.isAbstract(scannedClass.getClass().getModifiers());
    component.setAbstract(classIsAbstract);
    // name
    component.setName(cad.getStringValue("name", scannedClass.getScannedClass().getName()));
    // services
    final List<String> listedInterfaces = new ArrayList<String>();
    if (cad.hasValue("service")) {
        final String[] interfaces = (String[]) cad.getValue("service");
        if (interfaces != null) {
            for (final String t : interfaces) {
                listedInterfaces.add(t);
            }
        }
    } else {
        // scan directly implemented interfaces
        this.searchInterfaces(listedInterfaces, scannedClass.getScannedClass());
    }
    if (listedInterfaces.size() > 0) {
        final ServiceDescription serviceDesc = new ServiceDescription(cad);
        describedClass.add(serviceDesc);
        for (final String name : listedInterfaces) {
            serviceDesc.addInterface(name);
        }
        serviceDesc.setServiceFactory(cad.getBooleanValue("servicefactory", false));
    }
    // factory
    component.setFactory(cad.getStringValue("factory", null));
    // enabled
    if (cad.getValue("enabled") != null) {
        component.setEnabled(cad.getBooleanValue("enabled", true));
    }
    // immediate
    if (cad.getValue("immediate") != null) {
        component.setImmediate(cad.getBooleanValue("immediate", false));
    }
    // property
    final String[] property = (String[]) cad.getValue("property");
    if (property != null) {
        // TODO - what do we do if the value is invalid?
        for (final String propDef : property) {
            final int pos = propDef.indexOf('=');
            if (pos != -1) {
                final String prefix = propDef.substring(0, pos);
                final String value = propDef.substring(pos + 1);
                final int typeSep = prefix.indexOf(':');
                final String key = (typeSep == -1 ? prefix : prefix.substring(0, typeSep));
                final String type = (typeSep == -1 ? PropertyType.String.name() : prefix.substring(typeSep + 1));
                final PropertyType propType = PropertyType.valueOf(type);
                // FELIX-4159 : check if this is a multi value prop
                final List<PropertyDescription> existingProps = describedClass.getDescriptions(PropertyDescription.class);
                PropertyDescription found = null;
                for (final PropertyDescription current : existingProps) {
                    if (current.getName().equals(key)) {
                        found = current;
                        break;
                    }
                }
                if (found == null) {
                    final PropertyDescription pd = new PropertyDescription(cad);
                    describedClass.add(pd);
                    pd.setName(key);
                    pd.setValue(value);
                    pd.setType(propType);
                    pd.setUnbounded(PropertyUnbounded.DEFAULT);
                } else {
                    if (propType != found.getType()) {
                        throw new SCRDescriptorException("Multi value property '" + key + "' has different types: " + found.getType() + " & " + propType, describedClass.getSource());
                    }
                    if (found.getValue() != null) {
                        final String[] values = new String[2];
                        values[0] = found.getValue();
                        values[1] = value;
                        found.setMultiValue(values);
                    } else {
                        final String[] oldValues = found.getMultiValue();
                        final String[] newValues = new String[oldValues.length + 1];
                        System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
                        newValues[oldValues.length] = value;
                        found.setMultiValue(newValues);
                    }
                }
            }
        }
    }
    // xmlns
    if (cad.getValue("xmlns") != null) {
        final SpecVersion spec = SpecVersion.fromNamespaceUrl(cad.getValue("xmlns").toString());
        if (spec == null) {
            throw new SCRDescriptorException("Unknown xmlns attribute value: " + cad.getValue("xmlns"), describedClass.getSource());
        }
        component.setSpecVersion(spec);
    }
    // configuration policy
    component.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("configurationPolicy", ComponentConfigurationPolicy.OPTIONAL.name())));
    // configuration pid
    Object configPid = cad.getValue("configurationPid");
    if (configPid instanceof String) {
        component.setConfigurationPid((String) configPid);
    } else if (configPid instanceof String[] && ((String[]) configPid).length == 1) {
        component.setConfigurationPid(((String[]) configPid)[0]);
    } else {
        component.setConfigurationPid(null);
    }
    component.setCreatePid(false);
    // no inheritance
    component.setInherit(false);
    return component;
}
Also used : ComponentDescription(org.apache.felix.scrplugin.description.ComponentDescription) ServiceDescription(org.apache.felix.scrplugin.description.ServiceDescription) ArrayList(java.util.ArrayList) PropertyType(org.apache.felix.scrplugin.description.PropertyType) PropertyDescription(org.apache.felix.scrplugin.description.PropertyDescription) SpecVersion(org.apache.felix.scrplugin.SpecVersion) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Aggregations

ArrayList (java.util.ArrayList)1 SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)1 SpecVersion (org.apache.felix.scrplugin.SpecVersion)1 ComponentDescription (org.apache.felix.scrplugin.description.ComponentDescription)1 PropertyDescription (org.apache.felix.scrplugin.description.PropertyDescription)1 PropertyType (org.apache.felix.scrplugin.description.PropertyType)1 ServiceDescription (org.apache.felix.scrplugin.description.ServiceDescription)1