more preliminary exec() stuff
[nestedvm.git] / src / org / ibex / nestedvm / ClassLoader.java
1 package org.ibex.nestedvm;
2
3 //import java.io.*;
4 import java.util.*;
5
6 //import org.ibex.nestedvm.util.*;
7
8 // FIXME: This is totally broken now
9
10 // FEATURE: This is just a quick hack, it is really ugly and broken
11
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
16
17 public class ClassLoader extends java.lang.ClassLoader {
18     private Hashtable cache = new Hashtable();
19     
20     public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
21         Class c;
22         if(name.startsWith("nestedvm.")) {
23             throw new Error("probably shouldn't be here");
24             /*String path = name.substring(5).replace('.','/') + ".mips";
25             try {
26                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
27                 new ClassFileCompiler(path,name,bos).go();
28                 bos.close();
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());
35             }*/
36         } else {
37             c = findSystemClass(name);
38         }
39             
40         if(c == null) throw new ClassNotFoundException(name);
41         if(resolve) resolveClass(c);
42         return c;
43     }
44     
45     /*public synchronized void clearCache() {
46         cache.clear();
47     }
48     
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) {
53                     cache.remove(key);
54             return null;
55         }
56         return ent.klass;
57     }
58     
59     public synchronized Class createClass(String key, long timeStamp, Seekable data) {
60             Class klass = getCachedClass(key,timeStamp);
61         if(klass != null) return klass;
62         try {
63             ByteArrayOutputStream bos = new ByteArrayOutputStream();
64             new ClassFileCompiler(data,name,bos).go();
65             bos.close();
66             byte[] buf = bos.toByteArray();
67             klass = defineClass(name,buf,0,buf.length);
68             resolveClass(klass);
69             cache.put(key,new CacheEnt(klass,timeStamp));
70             return klass;
71         } catch(Exception e) {
72                     e.printStackTrace();
73             return null;
74         }
75     }
76     
77     private class CacheEnt {
78         public CacheEnt(Class klass, long timeStamp) { this.klass = klass; this.timeStamp = timeStamp; }
79             public Class klass;
80         public long timeStamp;
81     }*/
82     
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('/','.'));
86     }
87     
88     public Runtime runtimeFromBinary(String path) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
89         return (Runtime) classFromBinary(path).newInstance();
90     }
91     
92     public static void main(String[] args) throws Exception {
93         System.exit(new ClassLoader().runtimeFromBinary(args[0]).run(args));
94     }*/
95 }