1 23 package com.sun.enterprise.deployment.annotation.impl; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.util.Enumeration ; 30 import java.util.HashSet ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.util.jar.JarFile ; 34 import java.util.jar.JarEntry ; 35 import com.sun.enterprise.deployment.annotation.Scanner; 36 37 42 public class JarScanner extends JavaEEScanner implements Scanner { 43 44 File jarFile; 45 Set <JarEntry > entries = new HashSet <JarEntry >(); 46 ClassLoader classLoader = null; 47 48 49 public JarScanner(File jarFile) throws IOException { 50 this.jarFile = jarFile; 51 init(); 52 } 53 54 private void init() throws java.io.IOException { 55 JarFile jf = new JarFile (jarFile); 56 57 Enumeration <JarEntry > entriesEnum = jf.entries(); 58 while(entriesEnum.hasMoreElements()) { 59 JarEntry je = entriesEnum.nextElement(); 60 if (je.getName().endsWith(".class")) { 61 entries.add(je); 62 } 63 } 64 } 65 66 public ClassLoader getClassLoader() { 67 if (classLoader==null) { 68 URL [] urls = new URL [1]; 69 try { 70 urls[0] = jarFile.getAbsoluteFile().toURL(); 71 classLoader = new URLClassLoader (urls); 72 } catch(Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 return classLoader; 77 } 78 79 public Set <Class > getElements() { 80 81 82 Set <Class > elements = new HashSet <Class >(); 83 if (getClassLoader()==null) { 84 AnnotationUtils.getLogger().severe("Class loader null"); 85 return elements; 86 } 87 for (JarEntry je : entries) { 88 String fileName = je.getName(); 89 String className = fileName.replace(File.separatorChar, '.'); 91 className = className.substring(0, className.length()-6); 92 try { 93 elements.add(classLoader.loadClass(className)); 94 95 } catch(ClassNotFoundException cnfe) { 96 cnfe.printStackTrace(); 97 } 98 } 99 return elements; 100 } 101 102 103 } 104 | Popular Tags |