1 23 24 package com.sun.enterprise.deployment.util; 25 26 import com.sun.enterprise.deployment.annotation.introspection.ClassFile; 27 import com.sun.enterprise.deployment.annotation.introspection.ConstantPoolInfo; 28 import com.sun.enterprise.deployment.annotation.introspection.CustomAnnotationScanner; 29 import com.sun.enterprise.deployment.annotation.introspection.EjbComponentAnnotationScanner; 30 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 31 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 32 33 import java.io.BufferedInputStream ; 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.InputStream ; 38 import java.io.IOException ; 39 import java.nio.channels.Channels ; 40 import java.nio.channels.ReadableByteChannel ; 41 import java.util.Enumeration ; 42 import java.util.jar.JarEntry ; 43 import java.util.jar.JarFile ; 44 45 51 public abstract class AnnotationDetector { 52 53 private ClassFile classFile = new ClassFile(); 54 55 public AnnotationDetector() { 56 CustomAnnotationScanner scanner = createAnnotationScanner(); 57 ConstantPoolInfo poolInfo = new ConstantPoolInfo(scanner); 58 classFile.setConstantPoolInfo(poolInfo); 59 } 60 61 66 protected abstract CustomAnnotationScanner createAnnotationScanner(); 67 68 public boolean hasAnnotationInArchive(AbstractArchive archive) throws IOException { 69 File file = new File(archive.getArchiveUri()); 70 if (!file.exists()) { 71 throw new FileNotFoundException (archive.getArchiveUri()); 72 } 73 74 if (file.isDirectory()) { 78 return hasAnnotationInDirectory(archive); 79 } else { 80 return hasAnnotationInJar(archive); 81 } 82 } 83 84 87 private boolean hasAnnotationInJar(AbstractArchive archive) throws IOException { 88 JarFile jf = null; 89 try { 90 jf = new JarFile (new File(archive.getArchiveUri())); 91 Enumeration <JarEntry > entriesEnum = jf.entries(); 92 while(entriesEnum.hasMoreElements()) { 93 JarEntry je = entriesEnum.nextElement(); 94 if (je.getName().endsWith(".class")) { 95 if (containsAnnotation(jf, je)) { 96 return true; 97 } 98 } 99 } 100 } finally { 101 if (jf != null) { 102 jf.close(); 103 } 104 } 105 return false; 106 } 107 108 111 private boolean hasAnnotationInDirectory(AbstractArchive archive) throws IOException { 112 Enumeration entriesEnum = archive.entries(); 113 while(entriesEnum.hasMoreElements()) { 114 String entry = (String ) entriesEnum.nextElement(); 115 if (entry.endsWith(".class")) { 116 File file = null; 117 int ind = entry.lastIndexOf("/"); 118 if (ind != -1) { 119 String entryName = entry.substring(ind + 1); 120 String parent = archive.getArchiveUri() + File.separatorChar + 121 entry.substring(0, ind); 122 file = new File(parent.replace('/', File.separatorChar), entryName); 123 } else { 124 file = new File(archive.getArchiveUri(), entry); 125 } 126 if (containsAnnotation(file)) { 127 return true; 128 } 129 } 130 } 131 return false; 132 } 133 134 public boolean containsAnnotation(JarFile jarFile, JarEntry entry) throws IOException { 135 boolean result = false; 136 ReadableByteChannel channel = null; 138 try { 139 channel = Channels.newChannel(jarFile.getInputStream(entry)); 140 if (channel!=null) { 141 result = classFile.containsAnnotation(channel, entry.getSize()); 142 } 143 return result; 144 } finally { 145 if (channel != null) { 146 channel.close(); 147 } 148 } 149 } 150 151 public boolean containsAnnotation(File file) throws FileNotFoundException , IOException { 152 boolean result = false; 153 InputStream is = null; 155 try { 156 is = new BufferedInputStream (new FileInputStream (file)); 157 ReadableByteChannel channel = Channels.newChannel(is); 158 if (channel!=null) { 159 result = classFile.containsAnnotation(channel, file.length()); 160 } 161 return result; 162 } finally { 163 if (is != null) { 164 is.close(); 165 } 166 } 167 } 168 } 169 | Popular Tags |