KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > impl > JarScanner


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.impl;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.jar.JarEntry JavaDoc;
35 import com.sun.enterprise.deployment.annotation.Scanner;
36
37 /**
38  * Implements the scanner interface on a jar file.
39  *
40  * @author Jerome Dochez
41  */

42 public class JarScanner extends JavaEEScanner implements Scanner {
43     
44     File JavaDoc jarFile;
45     Set JavaDoc<JarEntry JavaDoc> entries = new HashSet JavaDoc<JarEntry JavaDoc>();
46     ClassLoader JavaDoc classLoader = null;
47     
48     /** Creates a new instance of JarScanner */
49     public JarScanner(File JavaDoc jarFile) throws IOException JavaDoc {
50         this.jarFile = jarFile;
51         init();
52     }
53     
54     private void init() throws java.io.IOException JavaDoc {
55         JarFile JavaDoc jf = new JarFile JavaDoc(jarFile);
56         
57         Enumeration JavaDoc<JarEntry JavaDoc> entriesEnum = jf.entries();
58         while(entriesEnum.hasMoreElements()) {
59             JarEntry JavaDoc je = entriesEnum.nextElement();
60             if (je.getName().endsWith(".class")) {
61                 entries.add(je);
62             }
63         }
64     }
65     
66     public ClassLoader JavaDoc getClassLoader() {
67         if (classLoader==null) {
68             URL JavaDoc[] urls = new URL JavaDoc[1];
69             try {
70                 urls[0] = jarFile.getAbsoluteFile().toURL();
71                 classLoader = new URLClassLoader JavaDoc(urls);
72             } catch(Exception JavaDoc e) {
73                 e.printStackTrace();
74             }
75         }
76         return classLoader;
77     }
78     
79     public Set JavaDoc<Class JavaDoc> getElements() {
80         
81         
82         Set JavaDoc<Class JavaDoc> elements = new HashSet JavaDoc<Class JavaDoc>();
83         if (getClassLoader()==null) {
84             AnnotationUtils.getLogger().severe("Class loader null");
85             return elements;
86         }
87         for (JarEntry JavaDoc je : entries) {
88             String JavaDoc fileName = je.getName();
89             // convert to a class name...
90
String JavaDoc 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 JavaDoc cnfe) {
96                 cnfe.printStackTrace();
97             }
98         }
99         return elements;
100     }
101     
102     
103 }
104
Popular Tags