FileDocCategorySizeDatePackage
EjbComponentAnnotationDetector.javaAPI DocGlassfish v2 API6692Fri May 04 22:31:58 BST 2007com.sun.enterprise.deployment.util

EjbComponentAnnotationDetector

public class EjbComponentAnnotationDetector extends Object
This is a utility class for detecting ejb component annotations. This class can be refactored to support other type of annotation dectection in the future.
author
Qingqing Ouyang

Fields Summary
private com.sun.enterprise.deployment.annotation.introspection.ClassFile
classFile
Constructors Summary
public EjbComponentAnnotationDetector()


      
        CustomAnnotationScanner scanner = new EjbComponentAnnotationScanner();
        ConstantPoolInfo poolInfo = new ConstantPoolInfo(scanner);
        classFile.setConstantPoolInfo(poolInfo);
    
Methods Summary
public booleanhasAnnotationInArchive(com.sun.enterprise.deployment.deploy.shared.AbstractArchive archive)

        File file = new File(archive.getArchiveUri());
        if (!file.exists()) {
            throw new FileNotFoundException(archive.getArchiveUri());
        }

        //@@@ note that the two methods could probably be combined into one.
        //The challenge is to find the size of each entry without having to
        //read the entry twice, once for the inputstream, the other for size.
        if (file.isDirectory()) {
            return hasAnnotationInDirectory(archive);
        } else {
            return hasAnnotationInJar(archive);
        }
    
private booleanhasAnnotationInDirectory(com.sun.enterprise.deployment.deploy.shared.AbstractArchive archive)

return
true if the directory contains any ejb component annotations

        Enumeration entriesEnum = archive.entries();
        while(entriesEnum.hasMoreElements()) {
            String entry = (String) entriesEnum.nextElement();
            if (entry.endsWith(".class")) {
                File file = null;
                int ind = entry.lastIndexOf("/");
                if (ind != -1) {
                    String entryName = entry.substring(ind + 1);
                    String parent = archive.getArchiveUri() + File.separatorChar + 
                                    entry.substring(0, ind);
                    file  = new File(parent.replace('/", File.separatorChar), entryName);
                } else {
                    file = new File(archive.getArchiveUri(), entry);
                }
                // check if it contains top level annotations...
                InputStream is = null;
                try {
                    is = 
                        new BufferedInputStream(new FileInputStream(file));
                    ReadableByteChannel channel = Channels.newChannel(is);
                    if (channel!=null) {
                        if (classFile.containsAnnotation(channel, file.length())) {
                            return true;
                        }
                     }
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            }
        }
        return false;
    
private booleanhasAnnotationInJar(com.sun.enterprise.deployment.deploy.shared.AbstractArchive archive)

return
true if the jar file contains any ejb component annotations

        JarFile jf = null;
        try {
            jf = new JarFile(new File(archive.getArchiveUri()));
            Enumeration<JarEntry> entriesEnum = jf.entries();
            while(entriesEnum.hasMoreElements()) {
                JarEntry je = entriesEnum.nextElement();
                if (je.getName().endsWith(".class")) {
                    // check if it contains top level annotations...
                    ReadableByteChannel channel = Channels.newChannel(jf.getInputStream(je));
                    if (channel!=null) {
                        if (classFile.containsAnnotation(channel, je.getSize())) {
                            return true;
                        }
                     }
                }
            }
        } finally {
            if (jf != null) {
                jf.close();
            }
        }
        return false;