1 23 24 package com.sun.enterprise.deployment.annotation.impl; 25 26 import java.io.File ; 27 import java.io.FileFilter ; 28 import java.io.IOException ; 29 import java.util.Set ; 30 import java.util.Iterator ; 31 import java.util.Enumeration ; 32 import java.util.HashSet ; 33 import java.net.URL ; 34 import java.net.URLClassLoader ; 35 36 import com.sun.enterprise.deployment.annotation.Scanner; 37 38 43 public class DirectoryScanner extends JavaEEScanner implements Scanner { 44 45 File directory; 46 Set <String > entries = new HashSet <String >(); 47 ClassLoader classLoader = null; 48 49 50 public DirectoryScanner(File directory) throws IOException { 51 this(directory, null); 52 } 53 54 public DirectoryScanner(File directory, ClassLoader classLoader) 55 throws IOException { 56 AnnotationUtils.getLogger().finer("dir is " + directory); 57 AnnotationUtils.getLogger().finer("classLoader is " + classLoader); 58 this.directory = directory; 59 this.classLoader = classLoader; 60 init(directory); 61 } 62 63 private void init(File directory) throws java.io.IOException { 64 init(directory, directory); 65 } 66 67 private void init(File top, File directory) throws java.io.IOException { 68 69 File [] dirFiles = directory.listFiles(new FileFilter () { 70 public boolean accept(File pathname) { 71 return pathname.getAbsolutePath().endsWith(".class"); 72 } 73 }); 74 for (File file : dirFiles) { 75 entries.add(file.getPath().substring(top.getPath().length()+1)); 76 } 77 78 File [] subDirs = directory.listFiles(new FileFilter () { 79 public boolean accept(File pathname) { 80 return pathname.isDirectory(); 81 } 82 }); 83 for (File subDir : subDirs) { 84 init(top, subDir); 85 } 86 } 87 88 protected Set <String > getEntries() { 89 return entries; 90 } 91 92 public ClassLoader getClassLoader() { 93 if (classLoader==null) { 94 URL [] urls = new URL [1]; 95 try { 96 urls[0] = directory.getAbsoluteFile().toURL(); 97 classLoader = new URLClassLoader (urls); 98 } catch(Exception e) { 99 e.printStackTrace(); 100 } 101 } 102 return classLoader; 103 } 104 105 public Set <Class > getElements() { 106 107 108 Set <Class > elements = new HashSet <Class >(); 109 if (getClassLoader()==null) { 110 AnnotationUtils.getLogger().severe("Class loader null"); 111 return elements; 112 } 113 for (String fileName : entries) { 114 String className = fileName.replace(File.separatorChar, '.'); 116 className = className.substring(0, className.length()-6); 117 System.out.println("Getting " + className); 118 try { 119 elements.add(classLoader.loadClass(className)); 120 121 } catch(Throwable cnfe) { 122 AnnotationUtils.getLogger().severe("cannot load " + className + " reason : " + cnfe.getMessage()); 123 } 124 } 125 return elements; 126 } 127 } 128 | Popular Tags |