Search in sources :

Example 6 with ClassDescription

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

the class ClassScanner method getComponentDescriptors.

/**
 * Returns a map of component descriptors which may be extended by the java
 * sources.
 * <p>
 * This method calls the {@link #getDependencies()} method and checks for
 * any Service-Component descriptors in the returned files.
 * <p>
 * This method may be overwritten by extensions of this class.
 *
 * @throws SCRDescriptorException May be thrown if an error occurs
 *             gathering the component descriptors.
 */
private Map<String, ClassDescription> getComponentDescriptors() throws SCRDescriptorException {
    if (loadedDependencies == null) {
        loadedDependencies = new HashMap<String, ClassDescription>();
        final Collection<File> dependencies = this.project.getDependencies();
        for (final File artifact : dependencies) {
            try {
                this.log.debug("Trying to get scrinfo from artifact " + artifact);
                // First try to read the private scr info file from previous scr generator versions
                InputStream scrInfoFile = null;
                try {
                    scrInfoFile = this.getFile(artifact, ABSTRACT_DESCRIPTOR_ARCHIV_PATH);
                    if (scrInfoFile != null) {
                        this.readServiceComponentDescriptor(scrInfoFile, artifact.toString() + ':' + ABSTRACT_DESCRIPTOR_ARCHIV_PATH);
                        continue;
                    }
                    this.log.debug("Artifact has no scrinfo file (it's optional): " + artifact);
                } catch (final IOException ioe) {
                    throw new SCRDescriptorException("Unable to get scrinfo from artifact", artifact.toString(), ioe);
                } finally {
                    if (scrInfoFile != null) {
                        try {
                            scrInfoFile.close();
                        } catch (final IOException ignore) {
                        }
                    }
                }
                this.log.debug("Trying to get manifest from artifact " + artifact);
                final Manifest manifest = this.getManifest(artifact);
                if (manifest != null) {
                    // read Service-Component entry
                    if (manifest.getMainAttributes().getValue(SERVICE_COMPONENT) != null) {
                        final String serviceComponent = manifest.getMainAttributes().getValue(SERVICE_COMPONENT);
                        this.log.debug("Found Service-Component: " + serviceComponent + " in artifact " + artifact);
                        final StringTokenizer st = new StringTokenizer(serviceComponent, ",");
                        while (st.hasMoreTokens()) {
                            final String entry = st.nextToken().trim();
                            if (entry.length() > 0) {
                                this.readServiceComponentDescriptor(artifact, entry);
                            }
                        }
                    } else {
                        this.log.debug("Artifact has no service component entry in manifest " + artifact);
                    }
                } else {
                    this.log.debug("Unable to get manifest from artifact " + artifact);
                }
            } catch (IOException ioe) {
                throw new SCRDescriptorException("Unable to get manifest from artifact", artifact.toString(), ioe);
            }
        }
    }
    return this.loadedDependencies;
}
Also used : StringTokenizer(java.util.StringTokenizer) FilterInputStream(java.io.FilterInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassDescription(org.apache.felix.scrplugin.description.ClassDescription) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Example 7 with ClassDescription

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

the class ClassScanner method readServiceComponentDescriptor.

/**
 * Parses the descriptors read from the given input stream. This method may
 * be called by the {@link #getComponentDescriptors()} method to parse the
 * descriptors gathered in an implementation dependent way.
 *
 * @throws SCRDescriptorException If an error occurs reading the
 *             descriptors from the stream.
 */
private void readServiceComponentDescriptor(final InputStream file, final String location) throws SCRDescriptorException {
    final List<ClassDescription> list = ComponentDescriptorIO.read(file, this.project.getClassLoader(), iLog, location);
    if (list != null) {
        for (final ClassDescription cd : list) {
            final String name;
            if (cd.getDescribedClass() == null) {
                name = cd.getDescription(ComponentDescription.class).getName();
            } else {
                name = cd.getDescribedClass().getName();
            }
            loadedDependencies.put(name, cd);
        }
    }
}
Also used : ClassDescription(org.apache.felix.scrplugin.description.ClassDescription)

Example 8 with ClassDescription

use of org.apache.felix.scrplugin.description.ClassDescription 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)

Aggregations

ClassDescription (org.apache.felix.scrplugin.description.ClassDescription)8 SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)3 FileInputStream (java.io.FileInputStream)2 FilterInputStream (java.io.FilterInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ComponentContainer (org.apache.felix.scrplugin.helper.ComponentContainer)2 Validator (org.apache.felix.scrplugin.helper.Validator)2 File (java.io.File)1 StringTokenizer (java.util.StringTokenizer)1 JarFile (java.util.jar.JarFile)1 Manifest (java.util.jar.Manifest)1 SCRDescriptorFailureException (org.apache.felix.scrplugin.SCRDescriptorFailureException)1 Source (org.apache.felix.scrplugin.Source)1 AnnotationProcessor (org.apache.felix.scrplugin.annotations.AnnotationProcessor)1 ScannedAnnotation (org.apache.felix.scrplugin.annotations.ScannedAnnotation)1 ScannedClass (org.apache.felix.scrplugin.annotations.ScannedClass)1 ComponentDescription (org.apache.felix.scrplugin.description.ComponentDescription)1 PropertyDescription (org.apache.felix.scrplugin.description.PropertyDescription)1