KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
27 import java.io.FileFilter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35 import java.util.jar.JarEntry JavaDoc;
36
37 import java.nio.channels.Channels JavaDoc;
38 import java.nio.channels.ReadableByteChannel JavaDoc;
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 /**
45  * This is an abstract class of the Scanner interface for J2EE module.
46  *
47  * @author Shing Wai Chan
48  */

49 abstract class ModuleScanner extends JavaEEScanner implements Scanner {
50     protected File JavaDoc archiveFile = null;
51     protected ClassLoader JavaDoc 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 JavaDoc<String JavaDoc> entries = new HashSet JavaDoc<String JavaDoc>();
57
58     /**
59      * This add extra className to be scanned.
60      * @param className
61      */

62     protected void addScanClassName(String JavaDoc className) {
63         if (className!=null && className.length()!=0)
64             entries.add(className);
65     }
66
67     /**
68      * This add all classes in given jarFile to be scanned.
69      * @param jarFile
70      */

71     protected void addScanJar(File JavaDoc jarFile) throws IOException JavaDoc {
72         JarFile JavaDoc jf = new JarFile JavaDoc(jarFile);
73         
74
75         Enumeration JavaDoc<JarEntry JavaDoc> entriesEnum = jf.entries();
76         while(entriesEnum.hasMoreElements()) {
77             JarEntry JavaDoc je = entriesEnum.nextElement();
78             if (je.getName().endsWith(".class")) {
79                 if (processAllClasses) {
80                     addEntry(je);
81                 } else {
82                     // check if it contains top level annotations...
83
ReadableByteChannel JavaDoc 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 JavaDoc je) {
95         String JavaDoc className = je.getName().replace('/', '.');
96         className = className.substring(0, className.length()-6);
97         entries.add(className);
98     }
99     
100     private void addEntry(File JavaDoc top, File JavaDoc f) {
101         String JavaDoc fileName = f.getPath();
102         fileName = fileName.substring(top.getPath().length()+1);
103         String JavaDoc className = fileName.replace(File.separatorChar, '.');
104         className = className.substring(0, className.length()-6);
105         entries.add(className);
106     }
107
108     /**
109      * This will include all class in directory to be scanned.
110      * param directory
111      */

112     protected void addScanDirectory(File JavaDoc directory) throws IOException JavaDoc {
113         initScanDirectory(directory, directory);
114     }
115     
116     private void initScanDirectory(File JavaDoc top, File JavaDoc directory) throws IOException JavaDoc {
117    
118         File JavaDoc[] files = directory.listFiles();
119         for (File JavaDoc file : files) {
120             if (file.isFile()) {
121                 String JavaDoc fileName = file.getPath();
122                 if (fileName.endsWith(".class")) {
123                     if (processAllClasses) {
124                         addEntry(top, file);
125                     } else {
126                         FileInputStream JavaDoc fis = null;
127                         try {
128                             fis = new FileInputStream JavaDoc(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 JavaDoc getClassLoader() {
146         return classLoader;
147     }
148
149     public Set JavaDoc<Class JavaDoc> getElements() {
150         Set JavaDoc<Class JavaDoc> elements = new HashSet JavaDoc<Class JavaDoc>();
151         if (getClassLoader() == null) {
152             AnnotationUtils.getLogger().severe("Class loader null");
153             return elements;
154         }
155
156         for (String JavaDoc 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 JavaDoc cnfe) {
163                 AnnotationUtils.getLogger().warning("Cannot load " + className + " reason : " + cnfe.getMessage());
164             }
165         }
166         return elements;
167     }
168 }
169
Popular Tags