001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: OzoneClassLoader.java,v 1.2 2002/04/18 13:41:09 mediumnet Exp $
008:
009: package org.ozoneDB.core;
010:
011: import java.io.*;
012: import org.ozoneDB.*;
013: import org.ozoneDB.DxLib.*;
014:
015: /**
016: * Ozone specific class loader. This compiles/runs with JDK 1.2 only. To make
017: * it run with JDK 1.1 see ClassManager.classForName() method.
018: *
019: *
020: * @author <a href="http://www.softwarebuero.de/">SMB</a>
021: * @version $Revision: 1.2 $Date: 2002/04/18 13:41:09 $
022: */
023: final class OzoneClassLoader extends ClassLoader {
024:
025: private DxMap classTable;
026:
027: public OzoneClassLoader() {
028: super ();
029: flushCache();
030: }
031:
032: protected void flushCache() {
033: classTable = new DxHashMap(100);
034: }
035:
036: protected Class findClass(String name)
037: throws ClassNotFoundException {
038: if (name.startsWith("java")) {
039: throw new ClassNotFoundException(
040: "I'm not here to load system classes.");
041: }
042:
043: StringBuffer resourceName = new StringBuffer(name);
044: for (int i = 0; i < resourceName.length(); i++) {
045: if (resourceName.charAt(i) == '.') {
046: resourceName.setCharAt(i, '/');
047: }
048: }
049: resourceName.append(".class");
050:
051: InputStream classIn = getSystemClassLoader()
052: .getSystemResourceAsStream(resourceName.toString());
053: if (classIn == null) {
054: throw new ClassNotFoundException(
055: "Unable to locate resource.");
056: }
057:
058: try {
059: byte[] classBytes = new byte[classIn.available()];
060: classIn.read(classBytes);
061: classIn.close();
062: Class cl = defineClass(name, classBytes, 0,
063: classBytes.length, null);
064: return cl;
065: } catch (Exception e) {
066: e.printStackTrace();
067: throw new ClassNotFoundException(e.toString());
068: }
069: }
070:
071: public Class loadClass(String name) throws ClassNotFoundException {
072: return loadClass(name, false);
073: }
074:
075: protected Class loadClass(String name, boolean resolve)
076: throws ClassNotFoundException {
077: if (name.startsWith("java")) {
078: return getSystemClassLoader().loadClass(name);
079: } else {
080: Class cl = (Class) classTable.elementForKey(name);
081: if (cl == null) {
082: cl = primitiveType(name);
083: if (cl == null) {
084: // this really loads the requested class in this ClassLoader but cause
085: // compatibility problems with already loaded classes
086: //cl = findClass (name);
087: //if (resolve)
088: // resolveClass (cl);
089:
090: // this works around the compatibility problems but causes the reloadClasses()
091: // method to not work
092: cl = getSystemClassLoader().loadClass(name);
093: }
094: classTable.addForKey(cl, name);
095: }
096: return cl;
097: }
098: }
099:
100: /**
101: * Load primitive types with default class loader.
102: */
103: protected static Class primitiveType(String name) {
104: if (name.equals("int")) {
105: return Integer.TYPE;
106: } else if (name.equals("char")) {
107: return Character.TYPE;
108: } else if (name.equals("byte")) {
109: return Byte.TYPE;
110: } else if (name.equals("double")) {
111: return Double.TYPE;
112: } else if (name.equals("float")) {
113: return Float.TYPE;
114: } else if (name.equals("long")) {
115: return Long.TYPE;
116: } else if (name.equals("short")) {
117: return Short.TYPE;
118: } else if (name.equals("boolean")) {
119: return Boolean.TYPE;
120: } else {
121: return null;
122: }
123: }
124:
125: }
|