KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package com.sun.enterprise.deployment.annotation.impl;
25
26 import java.io.File JavaDoc;
27 import java.io.FileFilter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.net.URLClassLoader JavaDoc;
35
36 import com.sun.enterprise.deployment.annotation.Scanner;
37
38 /**
39  * Implementation of the Scanner interface for a directory
40  *
41  * @author Jerome Dochez
42  */

43 public class DirectoryScanner extends JavaEEScanner implements Scanner {
44     
45     File JavaDoc directory;
46     Set JavaDoc<String JavaDoc> entries = new HashSet JavaDoc<String JavaDoc>();
47     ClassLoader JavaDoc classLoader = null;
48     
49     /** Creates a new instance of JarScanner */
50     public DirectoryScanner(File JavaDoc directory) throws IOException JavaDoc {
51         this(directory, null);
52     }
53
54     public DirectoryScanner(File JavaDoc directory, ClassLoader JavaDoc classLoader)
55             throws IOException JavaDoc {
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 JavaDoc directory) throws java.io.IOException JavaDoc {
64         init(directory, directory);
65     }
66     
67     private void init(File JavaDoc top, File JavaDoc directory) throws java.io.IOException JavaDoc {
68         
69         File JavaDoc[] dirFiles = directory.listFiles(new FileFilter JavaDoc() {
70                 public boolean accept(File JavaDoc pathname) {
71                     return pathname.getAbsolutePath().endsWith(".class");
72                 }
73         });
74         for (File JavaDoc file : dirFiles) {
75             entries.add(file.getPath().substring(top.getPath().length()+1));
76         }
77         
78         File JavaDoc[] subDirs = directory.listFiles(new FileFilter JavaDoc() {
79                 public boolean accept(File JavaDoc pathname) {
80                     return pathname.isDirectory();
81                 }
82         });
83         for (File JavaDoc subDir : subDirs) {
84             init(top, subDir);
85         }
86     }
87     
88     protected Set JavaDoc<String JavaDoc> getEntries() {
89         return entries;
90     }
91
92     public ClassLoader JavaDoc getClassLoader() {
93         if (classLoader==null) {
94             URL JavaDoc[] urls = new URL JavaDoc[1];
95             try {
96                 urls[0] = directory.getAbsoluteFile().toURL();
97                 classLoader = new URLClassLoader JavaDoc(urls);
98             } catch(Exception JavaDoc e) {
99                 e.printStackTrace();
100             }
101         }
102         return classLoader;
103     }
104
105     public Set JavaDoc<Class JavaDoc> getElements() {
106         
107         
108         Set JavaDoc<Class JavaDoc> elements = new HashSet JavaDoc<Class JavaDoc>();
109         if (getClassLoader()==null) {
110             AnnotationUtils.getLogger().severe("Class loader null");
111             return elements;
112         }
113         for (String JavaDoc fileName : entries) {
114             // convert to a class name...
115
String JavaDoc 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 JavaDoc cnfe) {
122                 AnnotationUtils.getLogger().severe("cannot load " + className + " reason : " + cnfe.getMessage());
123             }
124         }
125         return elements;
126     }
127 }
128
Popular Tags