KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > FileSystemClassLoader


1 package org.sapia.clazzy;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.net.URL JavaDoc;
6
7 import org.sapia.clazzy.loader.FileSystemLoader;
8
9 /**
10  * This class overrides the <code>BaseClassLoader</code> class and search
11  * classes in a given directory.
12  *
13  * @author Yanick Duchesne
14  *
15  * <dl>
16  * <dt><b>Copyright: </b>
17  * <dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open
18  * Source Software </a>. All Rights Reserved.</dd>
19  * </dt>
20  * <dt><b>License: </b>
21  * <dd>Read the license.txt file of the jar or visit the <a
22  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
23  * OSS web site</dd>
24  * </dt>
25  * </dl>
26  */

27 public class FileSystemClassLoader extends BaseClassLoader implements Consts{
28
29   private FileSystemLoader _loader;
30
31   public FileSystemClassLoader(File JavaDoc baseDir) {
32     _loader = new FileSystemLoader(baseDir);
33   }
34
35   public FileSystemClassLoader(ClassLoader JavaDoc parent, File JavaDoc baseDir) {
36     super(parent);
37     _loader = new FileSystemLoader(baseDir);
38   }
39
40   /**
41    * @return the <code>File</code> corresponding to the base directory in
42    * which this instance looks up.
43    */

44   public File JavaDoc getFile() {
45     return _loader.getBaseDir();
46   }
47
48   /**
49    * @see java.lang.ClassLoader#findClass(java.lang.String)
50    */

51   protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
52     if(!_loader.getBaseDir().exists() || !_loader.getBaseDir().isDirectory()) {
53       throw new ClassNotFoundException JavaDoc(name);
54     }
55     String JavaDoc className = name.replace('.', File.separatorChar)+".class";
56
57     
58     byte[] classBytes = _loader.loadBytes(className);
59     if(classBytes == null){
60       throw new ClassNotFoundException JavaDoc(name);
61     }
62     String JavaDoc pckg = Utils.getPackageNameFor(name);
63     if(pckg != null && getPackage(pckg) == null)
64       definePackage(pckg,
65                     PACKAGE_SPEC_TITLE,
66                     PACKAGE_SPEC_VERSION,
67                     PACKAGE_SPEC_VENDOR,
68                     PACKAGE_IMPL_TITLE,
69                     PACKAGE_IMPL_VERSION,
70                     PACKAGE_IMPL_VENDOR,
71                     null);
72     return defineClass(name, classBytes, 0, classBytes.length);
73   }
74   
75   /**
76    * @see java.lang.ClassLoader#findResource(java.lang.String)
77    */

78   protected URL JavaDoc findResource(String JavaDoc name) {
79     if(!_loader.getBaseDir().exists() || !_loader.getBaseDir().isDirectory()) {
80       return null;
81     }
82     try{
83       File JavaDoc f = new File JavaDoc(_loader.getBaseDir(), name);
84       if(f.exists() && f.isFile()){
85         return f.toURL();
86       }
87       return null;
88     }catch(IOException JavaDoc e){
89       return null;
90     }
91   }
92 }
93
Popular Tags