001: /*
002: * JavaClassLoader.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: * $Id: JavaClassLoader.java,v 1.10 2004/09/15 13:20:04 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.io.DataInputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.util.Collections;
028: import java.util.HashSet;
029: import java.util.Set;
030:
031: public class JavaClassLoader extends ClassLoader {
032: private static final boolean isSableVM;
033:
034: static {
035: String vm = System.getProperty("java.vm.name");
036: if (vm != null && vm.equals("SableVM"))
037: isSableVM = true;
038: else
039: isSableVM = false;
040: }
041:
042: private static JavaClassLoader persistentInstance;
043:
044: private static Set packages = Collections
045: .synchronizedSet(new HashSet());
046:
047: public static JavaClassLoader getPersistentInstance() {
048: return getPersistentInstance(null);
049: }
050:
051: public static JavaClassLoader getPersistentInstance(
052: String packageName) {
053: if (persistentInstance == null)
054: persistentInstance = new JavaClassLoader();
055: definePackage(packageName);
056: return persistentInstance;
057: }
058:
059: private static void definePackage(String packageName) {
060: if (packageName != null && !packages.contains(packageName)) {
061: persistentInstance.definePackage(packageName, "", "1.0",
062: "", "", "1.0", "", null);
063: packages.add(packageName);
064: }
065: }
066:
067: protected Class loadClassFromFile(File file) {
068: try {
069: long length = file.length();
070: if (length < Integer.MAX_VALUE) {
071: byte[] classbytes = new byte[(int) length];
072: DataInputStream in = new DataInputStream(
073: new FileInputStream(file));
074: in.readFully(classbytes);
075: in.close();
076: final String name;
077: // SableVM requires a non-null name.
078: if (isSableVM) {
079: String s = file.getName();
080: int index = s.lastIndexOf('.');
081: if (index >= 0)
082: s = s.substring(0, index);
083: name = "org.armedbear.lisp.".concat(s.replace('-',
084: '_'));
085: } else
086: name = null;
087: Class c = defineClass(name, classbytes, 0, (int) length);
088: if (c != null) {
089: resolveClass(c);
090: return c;
091: }
092: }
093: } catch (Throwable t) {
094: Debug.trace(t);
095: }
096: return null;
097: }
098:
099: public Class loadClassFromByteArray(String className,
100: byte[] classbytes) {
101: try {
102: long length = classbytes.length;
103: if (length < Integer.MAX_VALUE) {
104: Class c = defineClass(className, classbytes, 0,
105: (int) length);
106: if (c != null) {
107: resolveClass(c);
108: return c;
109: }
110: }
111: } catch (LinkageError e) {
112: throw e;
113: } catch (Throwable t) {
114: Debug.trace(t);
115: }
116: return null;
117: }
118:
119: public Class loadClassFromByteArray(String className, byte[] bytes,
120: int offset, int length) {
121: try {
122: Class c = defineClass(className, bytes, offset, length);
123: if (c != null) {
124: resolveClass(c);
125: return c;
126: }
127: } catch (Throwable t) {
128: Debug.trace(t);
129: }
130: return null;
131: }
132: }
|