Search in sources :

Example 1 with MetatypeAttributeDefinition

use of org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition in project felix by apache.

the class MetaTypeIO method generateOCDXML.

private static void generateOCDXML(final MetatypeContainer ocd, final ContentHandler contentHandler) throws SAXException {
    final AttributesImpl ai = new AttributesImpl();
    IOUtils.addAttribute(ai, "id", ocd.getId());
    IOUtils.addAttribute(ai, "name", ocd.getName());
    IOUtils.addAttribute(ai, "description", ocd.getDescription());
    IOUtils.indent(contentHandler, 1);
    contentHandler.startElement(INNER_NAMESPACE_URI, OCD_ELEMENT, OCD_ELEMENT_QNAME, ai);
    if (ocd.getProperties().size() > 0) {
        IOUtils.newline(contentHandler);
        final Iterator<MetatypeAttributeDefinition> i = ocd.getProperties().iterator();
        while (i.hasNext()) {
            final MetatypeAttributeDefinition ad = i.next();
            generateAttributeXML(ad, contentHandler);
        }
        IOUtils.indent(contentHandler, 1);
    }
    contentHandler.endElement(INNER_NAMESPACE_URI, OCD_ELEMENT, OCD_ELEMENT_QNAME);
    IOUtils.newline(contentHandler);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) MetatypeAttributeDefinition(org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition)

Example 2 with MetatypeAttributeDefinition

use of org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition in project felix by apache.

the class SCRDescriptorGenerator method processProperties.

/**
 * Process property directives
 */
private void processProperties(final ClassDescription current, final ComponentContainer component, final MetatypeContainer ocd) {
    for (final PropertyDescription pd : current.getDescriptions(PropertyDescription.class)) {
        if (this.testProperty(current, component.getProperties(), pd, current == component.getClassDescription())) {
            final String name = pd.getName();
            if (org.osgi.framework.Constants.SERVICE_ID.equals(name)) {
                iLog.addError("Class " + current.getDescribedClass().getName() + " is declaring " + "the protected property 'service.id'.", current.getSource());
                continue;
            }
            if (ocd != null) {
                // metatype - is this property private?
                final boolean isPrivate;
                if (pd.isPrivate() != null) {
                    isPrivate = pd.isPrivate();
                } else {
                    if (isPrivateProperty(name)) {
                        isPrivate = true;
                    } else {
                        isPrivate = false;
                    }
                }
                if (!isPrivate) {
                    final MetatypeAttributeDefinition ad = new MetatypeAttributeDefinition();
                    ocd.getProperties().add(ad);
                    ad.setId(pd.getName());
                    ad.setType(pd.getType().name());
                    if (pd.getLabel() != null) {
                        ad.setName(pd.getLabel());
                    }
                    if (pd.getDescription() != null) {
                        ad.setDescription(pd.getDescription());
                    }
                    if (pd.getUnbounded() == PropertyUnbounded.DEFAULT) {
                        if (pd.getCardinality() != 0) {
                            ad.setCardinality(pd.getCardinality());
                        }
                    } else if (pd.getUnbounded() == PropertyUnbounded.ARRAY) {
                        // unlimited array
                        ad.setCardinality(new Integer(Integer.MAX_VALUE));
                    } else {
                        // unlimited vector
                        ad.setCardinality(new Integer(Integer.MIN_VALUE));
                    }
                    ad.setDefaultValue(pd.getValue());
                    ad.setDefaultMultiValue(pd.getMultiValue());
                    // check options
                    final String[] parameters = pd.getOptions();
                    if (parameters != null && parameters.length > 0) {
                        final Map<String, String> options = new LinkedHashMap<String, String>();
                        for (int j = 0; j < parameters.length; j = j + 2) {
                            final String optionLabel = parameters[j];
                            final String optionValue = (j < parameters.length - 1) ? parameters[j + 1] : null;
                            if (optionValue != null) {
                                options.put(optionLabel, optionValue);
                            }
                        }
                        ad.setOptions(options);
                    }
                }
            } else {
                // additional metatype checks (FELIX-4033)
                if (pd.isPrivate() != null && pd.isPrivate()) {
                    iLog.addWarning("Property " + pd.getName() + " in class " + current.getDescribedClass().getName() + " is set as private. " + "This is redundant as no metatype will be generated.", current.getSource());
                }
            }
        }
    }
}
Also used : PropertyDescription(org.apache.felix.scrplugin.description.PropertyDescription) MetatypeAttributeDefinition(org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MetatypeAttributeDefinition

use of org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition in project felix by apache.

the class MetaTypeIO method generateDescriptors.

public static List<String> generateDescriptors(final DescriptionContainer module, final Project project, final Options options, final Log logger) throws SCRDescriptorException {
    // create a list with relevant components
    final List<ComponentContainer> components = new ArrayList<ComponentContainer>();
    for (final ComponentContainer component : module.getComponents()) {
        if (component.getMetatypeContainer() != null) {
            components.add(component);
        }
    }
    // write meta type info
    final File mtDir = options.getMetaTypeDirectory();
    final File parentDir = mtDir.getParentFile();
    if (components.size() > 0) {
        // check for metatype.properties
        // we used to have this in OSGI-INF/metatype, but that is actually not allowed by the spec
        // we should break the build
        final File oldMtProps = new File(project.getClassesDirectory(), OLD_LOCATION + File.separator + PROPS_FILE);
        if (oldMtProps.exists()) {
            throw new SCRDescriptorException("metatype properties file must be stored outside of " + OLD_LOCATION + ", move it to " + NEW_LOCATION, oldMtProps.getAbsolutePath());
        }
        final File mtProps = new File(project.getClassesDirectory(), NEW_LOCATION + File.separator + PROPS_FILE);
        final boolean oldStyle = mtProps.exists();
        final List<String> fileNames = new ArrayList<String>();
        final List<ComponentContainerContainer> containers = ComponentContainerUtil.split(components);
        for (final ComponentContainerContainer ccc : containers) {
            mtDir.mkdirs();
            final File useFile = new File(mtDir, ccc.className + ".xml");
            String metatypeLocation = NEW_LOCATION.replace(File.separatorChar, '/') + "/metatype";
            // check if all labels and descriptions are inlined
            boolean allInlined = true;
            for (final ComponentContainer cc : ccc.components) {
                final MetatypeContainer mc = cc.getMetatypeContainer();
                if (mc.getName() == null) {
                    if (oldStyle) {
                        mc.setName("%" + cc.getComponentDescription().getName() + ".name");
                    } else {
                        mc.setName("Component " + cc.getComponentDescription().getName());
                    }
                }
                if (mc.getName() != null && mc.getName().startsWith("%")) {
                    allInlined = false;
                }
                if (mc.getDescription() == null) {
                    if (oldStyle) {
                        mc.setDescription("%" + cc.getComponentDescription().getName() + ".description");
                    } else {
                        mc.setDescription("Description for " + cc.getComponentDescription().getName());
                    }
                }
                if (mc.getDescription() != null && mc.getDescription().startsWith("%")) {
                    allInlined = false;
                }
                for (final MetatypeAttributeDefinition mad : mc.getProperties()) {
                    if (mad.getName() == null) {
                        if (oldStyle) {
                            mad.setName("%" + mad.getId() + ".name");
                        } else {
                            mad.setName("Property " + mad.getId());
                        }
                    }
                    if (mad.getName() != null && mad.getName().startsWith("%")) {
                        allInlined = false;
                    }
                    if (mad.getDescription() == null) {
                        if (oldStyle) {
                            mad.setDescription("%" + mad.getId() + ".description");
                        } else {
                            mad.setDescription("Description for " + mad.getId());
                        }
                    }
                    if (mad.getDescription() != null && mad.getDescription().startsWith("%")) {
                        allInlined = false;
                    }
                }
            }
            if (allInlined) {
                final Properties metatypeProps = new Properties();
                // externalize all labels and descriptions
                for (final ComponentContainer cc : ccc.components) {
                    final MetatypeContainer mc = cc.getMetatypeContainer();
                    final String baseKey = cc.getComponentDescription().getName().replace("$", ".");
                    if (mc.getName() != null) {
                        final String key = baseKey + ".name";
                        metatypeProps.put(key, mc.getName());
                        mc.setName("%" + key);
                    }
                    if (mc.getDescription() != null) {
                        final String key = baseKey + ".description";
                        metatypeProps.put(key, mc.getDescription());
                        mc.setDescription("%" + key);
                    }
                    for (final MetatypeAttributeDefinition mad : mc.getProperties()) {
                        if (mad.getName() != null) {
                            final String key = baseKey + "." + mad.getId() + ".name";
                            metatypeProps.put(key, mad.getName());
                            mad.setName("%" + key);
                        }
                        if (mad.getDescription() != null) {
                            final String key = baseKey + "." + mad.getId() + ".description";
                            metatypeProps.put(key, mad.getDescription());
                            mad.setDescription("%" + key);
                        }
                    }
                }
                if (metatypeProps.size() > 0) {
                    final int lastDot = useFile.getName().lastIndexOf(".");
                    final String baseName = useFile.getName().substring(0, lastDot);
                    final File propsFile = new File(options.getOutputDirectory(), NEW_LOCATION + File.separator + baseName + ".properties");
                    propsFile.getParentFile().mkdirs();
                    try {
                        final FileOutputStream fos = new FileOutputStream(propsFile);
                        try {
                            metatypeProps.store(fos, null);
                        } finally {
                            fos.close();
                        }
                    } catch (IOException e) {
                        throw new SCRDescriptorException("Unable to create metatype.properties", propsFile.getAbsolutePath());
                    }
                    fileNames.add(NEW_LOCATION.replace(File.separatorChar, '/') + '/' + propsFile.getName());
                    metatypeLocation = NEW_LOCATION.replace(File.separatorChar, '/') + "/" + baseName;
                }
            }
            logger.info("Generating " + ccc.components.size() + " MetaType Descriptors in " + useFile);
            MetaTypeIO.write(module, ccc.components, useFile, metatypeLocation);
            fileNames.add(parentDir.getName() + '/' + mtDir.getName() + '/' + useFile.getName());
        }
        return fileNames;
    }
    if (mtDir.exists() && !options.isIncremental()) {
        for (final File f : mtDir.listFiles()) {
            if (f.isFile()) {
                logger.debug("Removing obsolete metatype file " + f);
                f.delete();
            }
        }
    }
    return null;
}
Also used : MetatypeAttributeDefinition(org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition) ArrayList(java.util.ArrayList) MetatypeContainer(org.apache.felix.scrplugin.helper.MetatypeContainer) IOException(java.io.IOException) Properties(java.util.Properties) ComponentContainerContainer(org.apache.felix.scrplugin.helper.ComponentContainerUtil.ComponentContainerContainer) FileOutputStream(java.io.FileOutputStream) ComponentContainer(org.apache.felix.scrplugin.helper.ComponentContainer) File(java.io.File) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Aggregations

MetatypeAttributeDefinition (org.apache.felix.scrplugin.helper.MetatypeAttributeDefinition)3 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)1 PropertyDescription (org.apache.felix.scrplugin.description.PropertyDescription)1 ComponentContainer (org.apache.felix.scrplugin.helper.ComponentContainer)1 ComponentContainerContainer (org.apache.felix.scrplugin.helper.ComponentContainerUtil.ComponentContainerContainer)1 MetatypeContainer (org.apache.felix.scrplugin.helper.MetatypeContainer)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1