KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > OzoneClassLoader


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: OzoneClassLoader.java,v 1.5 2002/12/29 11:15:56 per_nyfelt Exp $
8

9 package org.ozoneDB.core;
10
11 import org.ozoneDB.DxLib.DxHashMap;
12 import org.ozoneDB.DxLib.DxMap;
13
14 import java.io.InputStream JavaDoc;
15
16
17 /**
18  * Ozone specific class loader. This compiles/runs with JDK 1.2 only. To make
19  * it run with JDK 1.1 see ClassManager.classForName() method.
20  *
21  *
22  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
23  * @version $Revision: 1.5 $Date: 2002/12/29 11:15:56 $
24  */

25 final class OzoneClassLoader extends ClassLoader JavaDoc {
26
27     private DxMap classTable;
28
29
30     public OzoneClassLoader() {
31         super();
32         flushCache();
33     }
34
35
36     protected void flushCache() {
37         classTable = new DxHashMap( 100 );
38     }
39
40
41     protected Class JavaDoc findClass( String JavaDoc name ) throws ClassNotFoundException JavaDoc {
42         if (name.startsWith( "java" )) {
43             throw new ClassNotFoundException JavaDoc( "I'm not here to load system classes." );
44         }
45
46         System.out.println("Finding class " + name);
47         String JavaDoc resourceName = name.replace('.', '/') + ".class";
48 // StringBuffer resourceName = new StringBuffer( name );
49
// for (int i = 0; i < resourceName.length(); i++) {
50
// if (resourceName.charAt( i ) == '.') {
51
// resourceName.setCharAt( i, '/' );
52
// }
53
// }
54
// resourceName.append( ".class" );
55

56         // we try the context classloader first
57
InputStream JavaDoc classIn = getResourceAsStream(resourceName);
58         if ( classIn == null ) {
59             System.out.println("could not find class " + name + " using context classloader, trying system:");
60             classIn = getSystemClassLoader().getSystemResourceAsStream( resourceName );
61         }
62         if (classIn == null) {
63             throw new ClassNotFoundException JavaDoc( "Unable to locate resource: " + resourceName );
64         }
65
66         try {
67             byte[] classBytes = new byte[classIn.available()];
68             classIn.read( classBytes );
69             classIn.close();
70
71             // TODO: this does not work, causes a ClassFormatError
72
Class JavaDoc cl = defineClass( name, classBytes, 0, classBytes.length, null );
73             return cl;
74         } catch (Exception JavaDoc e) {
75             e.printStackTrace();
76             throw new ClassNotFoundException JavaDoc( e.toString() );
77         }
78     }
79
80
81     public Class JavaDoc loadClass( String JavaDoc name ) throws ClassNotFoundException JavaDoc {
82         return loadClass( name, false );
83     }
84
85
86     protected Class JavaDoc loadClass( String JavaDoc name, boolean resolve ) throws ClassNotFoundException JavaDoc {
87         if (name.startsWith( "java" )) {
88             return getSystemClassLoader().loadClass( name );
89         } else {
90             Class JavaDoc cl = (Class JavaDoc)classTable.elementForKey( name );
91             if (cl == null) {
92                 cl = primitiveType( name );
93                 if (cl == null) {
94                     // this really loads the requested class in this ClassLoader but cause
95
// compatibility problems with already loaded classes
96
// cl = findClass (name);
97
// if (resolve) {
98
// resolveClass (cl);
99
//}
100

101                     // this works around the compatibility problems but causes the reloadClasses()
102
// method to not work
103
cl = Thread.currentThread().getContextClassLoader().loadClass( name );
104                 }
105                 classTable.addForKey( cl, name );
106             }
107             return cl;
108         }
109     }
110
111
112     /**
113      * Load primitive types with default class loader.
114      */

115     protected static Class JavaDoc primitiveType( String JavaDoc name ) {
116         if (name.equals( "int" )) {
117             return Integer.TYPE;
118         } else if (name.equals( "char" )) {
119             return Character.TYPE;
120         } else if (name.equals( "byte" )) {
121             return Byte.TYPE;
122         } else if (name.equals( "double" )) {
123             return Double.TYPE;
124         } else if (name.equals( "float" )) {
125             return Float.TYPE;
126         } else if (name.equals( "long" )) {
127             return Long.TYPE;
128         } else if (name.equals( "short" )) {
129             return Short.TYPE;
130         } else if (name.equals( "boolean" )) {
131             return Boolean.TYPE;
132         } else {
133             return null;
134         }
135     }
136
137 }
138
Popular Tags