1 23 package com.sun.enterprise.deployment.annotation.impl; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileFilter ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.util.Set ; 31 import java.util.Enumeration ; 32 import java.util.HashSet ; 33 import java.util.logging.Level ; 34 import java.util.jar.JarFile ; 35 import java.util.jar.JarEntry ; 36 37 import java.nio.channels.Channels ; 38 import java.nio.channels.ReadableByteChannel ; 39 40 import com.sun.enterprise.deployment.annotation.Scanner; 41 import com.sun.enterprise.deployment.annotation.impl.JavaEEScanner; 42 import com.sun.enterprise.deployment.annotation.introspection.ClassFile; 43 44 49 abstract class ModuleScanner extends JavaEEScanner implements Scanner { 50 protected File archiveFile = null; 51 protected ClassLoader classLoader = null; 52 protected ClassFile classFile = new ClassFile(); 53 private boolean processAllClasses = Boolean.getBoolean("com.sun.enterprise.deployment.annotation.processAllClasses"); 54 55 56 private Set <String > entries = new HashSet <String >(); 57 58 62 protected void addScanClassName(String className) { 63 if (className!=null && className.length()!=0) 64 entries.add(className); 65 } 66 67 71 protected void addScanJar(File jarFile) throws IOException { 72 JarFile jf = new JarFile (jarFile); 73 74 75 Enumeration <JarEntry > entriesEnum = jf.entries(); 76 while(entriesEnum.hasMoreElements()) { 77 JarEntry je = entriesEnum.nextElement(); 78 if (je.getName().endsWith(".class")) { 79 if (processAllClasses) { 80 addEntry(je); 81 } else { 82 ReadableByteChannel channel = Channels.newChannel(jf.getInputStream(je)); 84 if (channel!=null) { 85 if (classFile.containsAnnotation(channel, je.getSize())) { 86 addEntry(je); 87 } 88 } 89 } 90 } 91 } 92 } 93 94 private void addEntry(JarEntry je) { 95 String className = je.getName().replace('/', '.'); 96 className = className.substring(0, className.length()-6); 97 entries.add(className); 98 } 99 100 private void addEntry(File top, File f) { 101 String fileName = f.getPath(); 102 fileName = fileName.substring(top.getPath().length()+1); 103 String className = fileName.replace(File.separatorChar, '.'); 104 className = className.substring(0, className.length()-6); 105 entries.add(className); 106 } 107 108 112 protected void addScanDirectory(File directory) throws IOException { 113 initScanDirectory(directory, directory); 114 } 115 116 private void initScanDirectory(File top, File directory) throws IOException { 117 118 File [] files = directory.listFiles(); 119 for (File file : files) { 120 if (file.isFile()) { 121 String fileName = file.getPath(); 122 if (fileName.endsWith(".class")) { 123 if (processAllClasses) { 124 addEntry(top, file); 125 } else { 126 FileInputStream fis = null; 127 try { 128 fis = new FileInputStream (file); 129 if (classFile.containsAnnotation(fis.getChannel(), file.length())) { 130 addEntry(top, file); 131 } 132 } finally { 133 if (fis != null) { 134 fis.close(); 135 } 136 } 137 } 138 } 139 } else { 140 initScanDirectory(top, file); 141 } 142 } 143 } 144 145 public ClassLoader getClassLoader() { 146 return classLoader; 147 } 148 149 public Set <Class > getElements() { 150 Set <Class > elements = new HashSet <Class >(); 151 if (getClassLoader() == null) { 152 AnnotationUtils.getLogger().severe("Class loader null"); 153 return elements; 154 } 155 156 for (String className : entries) { 157 if (AnnotationUtils.getLogger().isLoggable(Level.FINE)) { 158 AnnotationUtils.getLogger().fine("Getting " + className); 159 } 160 try { 161 elements.add(classLoader.loadClass(className)); 162 } catch(ClassNotFoundException cnfe) { 163 AnnotationUtils.getLogger().warning("Cannot load " + className + " reason : " + cnfe.getMessage()); 164 } 165 } 166 return elements; 167 } 168 } 169 | Popular Tags |