001: /*
002: * @(#)ArrayClassLoader.java 1.0.0 11/17/2000 - 12:01:17
003: *
004: * Copyright (C) 2000,2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.classes.v1.jdk0;
028:
029: import java.util.Hashtable;
030: import java.util.Enumeration;
031: import java.util.Vector;
032:
033: /**
034: * Load classes by byte Arrays. JDK 1.0+ compatible.
035: *
036: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
037: * @version $Date: 2003/02/10 22:52:36 $
038: * @since November 17, 2000 (GroboUtils Alpha 0.9.0)
039: */
040: public class ArrayClassLoader extends ClassLoader {
041: //----------------------------
042: // Public data
043:
044: //----------------------------
045: // Private data
046:
047: /**
048: * list of all classes
049: */
050: private Hashtable m_classList = new Hashtable();
051: private Hashtable m_classCache = new Hashtable();
052:
053: /**
054: * The original bytecode source, for linking.
055: */
056: private BytecodeSource m_bytecodeSource = null;
057:
058: //----------------------------
059: // constructors
060:
061: /**
062: * Default constructor
063: */
064: public ArrayClassLoader() {
065: // do nothing
066: }
067:
068: //----------------------------
069: // Public methods
070:
071: /**
072: * Sets the reference to the original bytecode loader. By default,
073: * the value is <tt>null</tt>. If the value is <tt>null</tt>, then
074: * there is the possibility for a link error.
075: */
076: public void setBytecodeSource(BytecodeSource bs) {
077: this .m_bytecodeSource = bs;
078: }
079:
080: /**
081: * Add a new class to the internal list.
082: */
083: public void addClass(String name, byte[] bytecode) {
084: if (name == null || bytecode == null) {
085: throw new IllegalArgumentException("no null args");
086: }
087: this .m_classList.put(name, bytecode);
088: }
089:
090: // inherited from ClassLoader
091: /**
092: * @exception ClassNotFoundException thrown if the given class name
093: * could not be found, or if there was a problem loading the
094: * bytecode for the class.
095: */
096: public Class loadClass(String name, boolean resolve)
097: throws ClassNotFoundException {
098: Class c;
099:
100: if (name == null) {
101: throw new IllegalArgumentException("classname is null");
102: }
103:
104: c = (Class) this .m_classCache.get(name);
105: if (c == null) {
106: try {
107: c = findSystemClass(name);
108: } catch (Exception ex) {
109: byte bytecode[] = getBytecode(name);
110: if (bytecode == null) {
111: System.out
112: .println(this .getClass().getName()
113: + "::loadClass( '"
114: + name
115: + "' ): bytecode for class was never defined.");
116: throw new ClassNotFoundException(name);
117: } else {
118: try {
119: c = defineClass(name, bytecode, 0,
120: bytecode.length);
121: this .m_classCache.put(name, c);
122: } catch (Exception ex2) {
123: // something wrong with the class format
124: throw new ClassNotFoundException(
125: "Bad class format for class " + name);
126: }
127: }
128: }
129: }
130: if (resolve) {
131: resolveClass(c);
132: }
133: return c;
134: }
135:
136: //----------------------------
137: // Protected methods
138:
139: /**
140: * Retrieves the internal bytecode for the given class. If not known,
141: * then will attempt to pass it to the bytecode source.
142: *
143: * @param className a non-null class name.
144: */
145: protected byte[] getBytecode(String className) {
146: byte bytecode[] = (byte[]) this .m_classList.get(className);
147: if (bytecode == null) {
148: if (this .m_bytecodeSource != null) {
149: bytecode = this .m_bytecodeSource.getBytecode(className);
150: if (bytecode != null) {
151: addClass(className, bytecode);
152: }
153: }
154: }
155: return bytecode;
156: }
157:
158: //----------------------------
159: // Private methods
160: }
|