KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > FileClassLoader


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.util;
24
25 import java.io.IOException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import com.sun.enterprise.util.LocalStringManagerImpl;
30
31 public class FileClassLoader extends ClassLoader JavaDoc {
32     String JavaDoc codebase;
33     Hashtable JavaDoc cache = new Hashtable JavaDoc();
34
35     private static LocalStringManagerImpl localStrings =
36         new LocalStringManagerImpl(FileClassLoader.class);
37
38     public FileClassLoader(String JavaDoc codebase)
39     {
40     this.codebase = codebase;
41     }
42
43     private byte[] loadClassData(String JavaDoc name)
44     throws IOException JavaDoc
45     {
46         // load the class data from the codebase
47
String JavaDoc sep = System.getProperty("file.separator");
48     String JavaDoc c = name.replace('.', sep.charAt(0)) + ".class";
49     File JavaDoc file = new File JavaDoc(codebase + sep + c);
50     if (!file.exists()) {
51         File JavaDoc wf = new File JavaDoc(codebase + sep + "WEB-INF" + sep + "classes" + sep + c);
52         if (wf.exists()) {
53         file = wf;
54         }
55     }
56     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
57     int avail = fis.available();
58     byte[] buf = new byte[avail];
59     fis.read(buf);
60         fis.close();
61     return buf;
62     }
63     
64     String JavaDoc getClassName(File JavaDoc f) throws IOException JavaDoc, ClassFormatError JavaDoc {
65     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(f);
66     int avail = fis.available();
67     byte[] buf = new byte[avail];
68     fis.read(buf);
69         fis.close();
70     Class JavaDoc c = super.defineClass(null, buf, 0, avail);
71     return c.getName();
72     }
73
74     /**
75      * @exception ClassNotFoundException if class load fails
76      */

77     public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
78     throws ClassNotFoundException JavaDoc
79     {
80         Class JavaDoc c = (Class JavaDoc)cache.get(name);
81         if (c == null) {
82         try {
83                 byte data[] = loadClassData(name);
84                 c = defineClass(null,data, 0, data.length);
85                 if( !name.equals(c.getName()) ) {
86                     throw new ClassNotFoundException JavaDoc(localStrings.getLocalString("classloader.wrongpackage", "", new Object JavaDoc[] { name, c.getName() }));
87                 }
88         }
89         catch ( Exception JavaDoc ex ) {
90         // Try to use default classloader. If this fails,
91
// then a ClassNotFoundException is thrown.
92
c = Class.forName(name);
93         }
94             cache.put(name, c);
95         }
96         if (resolve)
97             resolveClass(c);
98         return c;
99     }
100
101     public String JavaDoc toString()
102     {
103     return "FileClassLoader: Codebase = "+codebase+"\n";
104     }
105 }
106
Popular Tags