Search in sources :

Example 11 with SCRDescriptorException

use of org.apache.felix.scrplugin.SCRDescriptorException in project felix by apache.

the class ClassScanner method readServiceComponentDescriptor.

/**
 * Read the service component description.
 *
 * @param artifact
 * @param entry
 * @throws IOException
 * @throws SCRDescriptorException
 */
private void readServiceComponentDescriptor(final File artifactFile, String entry) {
    this.log.debug("Reading " + entry + " from " + artifactFile);
    InputStream xml = null;
    try {
        xml = this.getFile(artifactFile, entry);
        if (xml == null) {
            throw new SCRDescriptorException("Entry " + entry + " not contained in JAR File ", artifactFile.toString());
        }
        this.readServiceComponentDescriptor(xml, artifactFile.toString() + ':' + entry);
    } catch (final IOException mee) {
        this.log.warn("Unable to read SCR descriptor file from JAR File " + artifactFile + " at " + entry);
        this.log.debug("Exception occurred during reading: " + mee.getMessage(), mee);
    } catch (final SCRDescriptorException mee) {
        this.log.warn("Unable to read SCR descriptor file from JAR File " + artifactFile + " at " + entry);
        this.log.debug("Exception occurred during reading: " + mee.getMessage(), mee);
    } finally {
        if (xml != null) {
            try {
                xml.close();
            } catch (final IOException ignore) {
            }
        }
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Example 12 with SCRDescriptorException

use of org.apache.felix.scrplugin.SCRDescriptorException in project felix by apache.

the class ClassScanner method scanSources.

/**
 * Scan all source class files for annotations and process them.
 */
public List<ClassDescription> scanSources() throws SCRDescriptorFailureException, SCRDescriptorException {
    final List<ClassDescription> result = new ArrayList<ClassDescription>();
    for (final Source src : project.getSources()) {
        if (src.getFile().getName().equals("package-info.java")) {
            log.debug("Skipping file " + src.getClassName());
            continue;
        }
        log.debug("Scanning class " + src.getClassName());
        try {
            // load the class
            final Class<?> annotatedClass = project.getClassLoader().loadClass(src.getClassName());
            this.process(annotatedClass, src, result);
        } catch (final SCRDescriptorFailureException e) {
            throw e;
        } catch (final SCRDescriptorException e) {
            throw e;
        } catch (final ClassNotFoundException e) {
            log.warn("ClassNotFoundException: " + e.getMessage());
        } catch (final NoClassDefFoundError e) {
            log.warn("NoClassDefFoundError: " + e.getMessage());
        } catch (final Throwable t) {
            throw new SCRDescriptorException("Unable to load compiled class: " + src.getClassName(), src.getFile().toString(), t);
        }
    }
    return result;
}
Also used : SCRDescriptorFailureException(org.apache.felix.scrplugin.SCRDescriptorFailureException) ArrayList(java.util.ArrayList) ClassDescription(org.apache.felix.scrplugin.description.ClassDescription) Source(org.apache.felix.scrplugin.Source) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Example 13 with SCRDescriptorException

use of org.apache.felix.scrplugin.SCRDescriptorException in project felix by apache.

the class ComponentDescriptorIO method generateDescriptorFiles.

/**
 * Generate descriptor file(s)
 */
public static List<String> generateDescriptorFiles(final DescriptionContainer module, final Options options, final Log logger) throws SCRDescriptorException, SCRDescriptorFailureException {
    // get the list of all relevant containers
    final List<ComponentContainer> components = new ArrayList<ComponentContainer>();
    for (final ComponentContainer container : module.getComponents()) {
        if (!container.getComponentDescription().isCreateDs()) {
            logger.debug("Ignoring descriptor for DS : " + container);
        } else if (!container.getComponentDescription().isAbstract()) {
            logger.debug("Adding descriptor for DS : " + container);
            components.add(container);
        }
    }
    // check descriptor file
    final File descriptorDir = options.getComponentDescriptorDirectory();
    // terminate if there is nothing else to write
    if (components.isEmpty()) {
        logger.debug("No Service Component Descriptors found in project.");
        // remove files if it exists
        if (descriptorDir.exists() && !options.isIncremental()) {
            for (final File f : descriptorDir.listFiles()) {
                if (f.isFile()) {
                    logger.debug("Removing obsolete service descriptor " + f);
                    f.delete();
                }
            }
        }
        return null;
    }
    // finally the descriptors have to be written ....
    // ensure parent dir
    descriptorDir.mkdirs();
    final List<String> fileNames = new ArrayList<String>();
    final List<ComponentContainerContainer> containers = ComponentContainerUtil.split(components);
    for (final ComponentContainerContainer ccc : containers) {
        final SpecVersion globalVersion = module.getOptions().getSpecVersion();
        SpecVersion sv = null;
        for (final ComponentContainer cc : ccc.components) {
            if (sv == null || sv.ordinal() < cc.getComponentDescription().getSpecVersion().ordinal()) {
                sv = cc.getComponentDescription().getSpecVersion();
            }
        }
        module.getOptions().setSpecVersion(sv);
        final File useFile = new File(descriptorDir, ccc.className + ".xml");
        try {
            ComponentDescriptorIO.generateXML(module, ccc.components, useFile, logger);
        } catch (final IOException e) {
            throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
        } catch (final TransformerException e) {
            throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
        } catch (final SAXException e) {
            throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
        }
        fileNames.add(PARENT_NAME + '/' + useFile.getName());
        module.getOptions().setSpecVersion(globalVersion);
    }
    return fileNames;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ComponentContainerContainer(org.apache.felix.scrplugin.helper.ComponentContainerUtil.ComponentContainerContainer) SpecVersion(org.apache.felix.scrplugin.SpecVersion) ComponentContainer(org.apache.felix.scrplugin.helper.ComponentContainer) File(java.io.File) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException) TransformerException(javax.xml.transform.TransformerException)

Example 14 with SCRDescriptorException

use of org.apache.felix.scrplugin.SCRDescriptorException 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

SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)14 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 FilterInputStream (java.io.FilterInputStream)3 InputStream (java.io.InputStream)3 TransformerException (javax.xml.transform.TransformerException)3 SCRDescriptorFailureException (org.apache.felix.scrplugin.SCRDescriptorFailureException)3 SpecVersion (org.apache.felix.scrplugin.SpecVersion)3 ClassDescription (org.apache.felix.scrplugin.description.ClassDescription)3 ComponentContainer (org.apache.felix.scrplugin.helper.ComponentContainer)3 Method (java.lang.reflect.Method)2 Options (org.apache.felix.scrplugin.Options)2 SCRDescriptorGenerator (org.apache.felix.scrplugin.SCRDescriptorGenerator)2 ScannedAnnotation (org.apache.felix.scrplugin.annotations.ScannedAnnotation)2 ComponentContainerContainer (org.apache.felix.scrplugin.helper.ComponentContainerUtil.ComponentContainerContainer)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassNode (org.objectweb.asm.tree.ClassNode)2