1 23 package com.sun.enterprise.deployment.util; 24 25 import com.sun.enterprise.deployment.annotation.introspection.ClassFile; 26 import com.sun.enterprise.deployment.annotation.introspection.ConstantPoolInfo; 27 import com.sun.enterprise.deployment.annotation.introspection.CustomAnnotationScanner; 28 import com.sun.enterprise.deployment.annotation.introspection.EjbComponentAnnotationScanner; 29 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 30 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 31 32 import java.io.BufferedInputStream ; 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileNotFoundException ; 36 import java.io.InputStream ; 37 import java.io.IOException ; 38 import java.nio.channels.Channels ; 39 import java.nio.channels.ReadableByteChannel ; 40 import java.util.Enumeration ; 41 import java.util.jar.JarEntry ; 42 import java.util.jar.JarFile ; 43 44 51 public class EjbComponentAnnotationDetector { 52 53 private ClassFile classFile = new ClassFile(); 54 55 public EjbComponentAnnotationDetector() { 56 CustomAnnotationScanner scanner = new EjbComponentAnnotationScanner(); 57 ConstantPoolInfo poolInfo = new ConstantPoolInfo(scanner); 58 classFile.setConstantPoolInfo(poolInfo); 59 } 60 61 public boolean hasAnnotationInArchive(AbstractArchive archive) throws IOException { 62 File file = new File(archive.getArchiveUri()); 63 if (!file.exists()) { 64 throw new FileNotFoundException (archive.getArchiveUri()); 65 } 66 67 if (file.isDirectory()) { 71 return hasAnnotationInDirectory(archive); 72 } else { 73 return hasAnnotationInJar(archive); 74 } 75 } 76 77 80 private boolean hasAnnotationInJar(AbstractArchive archive) throws IOException { 81 JarFile jf = null; 82 try { 83 jf = new JarFile (new File(archive.getArchiveUri())); 84 Enumeration <JarEntry > entriesEnum = jf.entries(); 85 while(entriesEnum.hasMoreElements()) { 86 JarEntry je = entriesEnum.nextElement(); 87 if (je.getName().endsWith(".class")) { 88 ReadableByteChannel channel = Channels.newChannel(jf.getInputStream(je)); 90 if (channel!=null) { 91 if (classFile.containsAnnotation(channel, je.getSize())) { 92 return true; 93 } 94 } 95 } 96 } 97 } finally { 98 if (jf != null) { 99 jf.close(); 100 } 101 } 102 return false; 103 } 104 105 108 private boolean hasAnnotationInDirectory(AbstractArchive archive) throws IOException { 109 Enumeration entriesEnum = archive.entries(); 110 while(entriesEnum.hasMoreElements()) { 111 String entry = (String ) entriesEnum.nextElement(); 112 if (entry.endsWith(".class")) { 113 File file = null; 114 int ind = entry.lastIndexOf("/"); 115 if (ind != -1) { 116 String entryName = entry.substring(ind + 1); 117 String parent = archive.getArchiveUri() + File.separatorChar + 118 entry.substring(0, ind); 119 file = new File(parent.replace('/', File.separatorChar), entryName); 120 } else { 121 file = new File(archive.getArchiveUri(), entry); 122 } 123 InputStream is = null; 125 try { 126 is = 127 new BufferedInputStream (new FileInputStream (file)); 128 ReadableByteChannel channel = Channels.newChannel(is); 129 if (channel!=null) { 130 if (classFile.containsAnnotation(channel, file.length())) { 131 return true; 132 } 133 } 134 } finally { 135 if (is != null) { 136 is.close(); 137 } 138 } 139 } 140 } 141 return false; 142 } 143 } 144 | Popular Tags |