1 package org.ibex.nestedvm;
6 import org.ibex.nestedvm.util.*;
8 // FIXME: This is totally broken now
10 // FEATURE: This is just a quick hack, it is really ugly and broken
12 // FEATURE: Cache based on org.ibex.util.Cache
13 // FEATURE: Base64 encode some id to form package name
14 // FEATURE: Timestamped cache entries, requests carry minimum timestamp
15 // NOTE: Need to handle binaries spanned accross many classfiles
17 public class ClassLoader extends java.lang.ClassLoader {
18 private Hashtable cache = new Hashtable();
20 public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
22 if(name.startsWith("nestedvm.")) {
23 throw new Error("probably shouldn't be here");
24 /*String path = name.substring(5).replace('.','/') + ".mips";
26 ByteArrayOutputStream bos = new ByteArrayOutputStream();
27 new ClassFileCompiler(path,name,bos).go();
29 byte[] buf = bos.toByteArray();
30 c = defineClass(name,buf,0,buf.length);
31 } catch(IOException e) {
32 throw new ClassNotFoundException(name);
33 } catch(Compiler.Exn e) {
34 throw new ClassNotFoundException(e.getMessage());
37 c = findSystemClass(name);
40 if(c == null) throw new ClassNotFoundException(name);
41 if(resolve) resolveClass(c);
45 /*public synchronized void clearCache() {
49 public synchronized Class getCachedClass(String key, long timeStamp) {
50 String name = "nestedvm." + Base64.encode(key);
51 CacheEnt ent = (CacheEnt) cache.get(name);
52 if(ent.timeStamp < timeStamp) {
59 public synchronized Class createClass(String key, long timeStamp, Seekable data) {
60 Class klass = getCachedClass(key,timeStamp);
61 if(klass != null) return klass;
63 ByteArrayOutputStream bos = new ByteArrayOutputStream();
64 new ClassFileCompiler(data,name,bos).go();
66 byte[] buf = bos.toByteArray();
67 klass = defineClass(name,buf,0,buf.length);
69 cache.put(key,new CacheEnt(klass,timeStamp));
71 } catch(Exception e) {
77 private class CacheEnt {
78 public CacheEnt(Class klass, long timeStamp) { this.klass = klass; this.timeStamp = timeStamp; }
80 public long timeStamp;
83 /*public Class classFromBinary(String path) throws ClassNotFoundException {
84 if(!path.endsWith(".mips")) throw new IllegalArgumentException("isn't a .mips");
85 return loadClass("mips." + path.substring(0,path.length()-5).replace('/','.'));
88 public Runtime runtimeFromBinary(String path) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
89 return (Runtime) classFromBinary(path).newInstance();
92 public static void main(String[] args) throws Exception {
93 System.exit(new ClassLoader().runtimeFromBinary(args[0]).run(args));