From: Samuele P. <ped...@us...> - 2000-12-15 03:19:03
|
Update of /cvsroot/jython/jython/org/python/core In directory slayer.i.sourceforge.net:/tmp/cvs-serv8926/org/python/core Modified Files: BytecodeLoader.java MakeProxies.java Options.java PySystemState.java imp.java Added Files: SyspathJavaLoader.java Log Message: Modified: multi classloader support (I) * Proxies retrieve classes for return-type conversions from their classloaders. * BytecodeLoaders are setup with a set of referents and look for classes first through the sys.path loader and then through the loaders of the referents. makeup * Added specific SyspathJavaLoader loader class. * - python.options.extendedClassLoader --- NEW FILE --- // Copyright © Corporation for National Research Initiatives // Copyright 2000 Samuele Pedroni package org.python.core; import java.io.*; import java.util.StringTokenizer; import java.util.Hashtable; public class SyspathJavaLoader extends ClassLoader { public InputStream getResourceAsStream(String res) { ClassLoader classLoader = Py.getSystemState().getClassLoader(); if (classLoader != null) return classLoader.getResourceAsStream(res); classLoader = this.getClass().getClassLoader(); InputStream ret; if (classLoader != null) ret = classLoader.getResourceAsStream(res); else ret = ClassLoader.getSystemResourceAsStream(res); if (ret != null) return ret; if(res.charAt(0) == '/') res = res.substring(1); res.replace('/',File.separatorChar); PyList path = Py.getSystemState().path; for (int i=0; i < path.__len__(); i++) { String dir = path.get(i).__str__().toString(); if (dir.length() == 0) dir = null; try { return new BufferedInputStream(new FileInputStream(new File(dir,res))); } catch (IOException e) { continue; } } return null; } // override from abstract base class protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { // System.err.println("loadClass("+name+", "+resolve+")"); // First, if the Python runtime system has a default class loader, // defer to it. ClassLoader classLoader = Py.getSystemState().getClassLoader(); if (classLoader != null) return classLoader.loadClass(name); // Search the sys.path for a .class file matching the named class. // TBD: This registry option is temporary. try { return Class.forName(name); } catch(ClassNotFoundException e) { } Class c = findLoadedClass(name); if (c != null) return c; /* previously: if Options.extendedClassLoader && // KLUDGE ALERT: without this test, running jpython // interactively from the build directory will fail with // ClassCircularityError or LinkageError. There's gotta be a // better way. !name.startsWith("org.python") */ { PyList path = Py.getSystemState().path; for (int i=0; i < path.__len__(); i++) { String dir = path.get(i).__str__().toString(); FileInputStream fis = open(dir, name); if (fis == null) continue; try { int size = fis.available(); byte[] buffer = new byte[size]; fis.read(buffer); fis.close(); return loadClassFromBytes(name, buffer); } catch (IOException e) { continue; } } } // couldn't find the .class file on sys.path throw new ClassNotFoundException(name); } private FileInputStream open(String dir, String name) { String accum = ""; boolean first = true; for (StringTokenizer t = new StringTokenizer(name, "."); t.hasMoreTokens();) { String token = t.nextToken(); if (!first) accum += File.separator; accum += token; first = false; } try { if (dir.length() == 0) dir = null; return new FileInputStream(new File(dir, accum+".class")); } catch (FileNotFoundException e) { return null; } } private Class loadClassFromBytes(String name, byte[] data) { // System.err.println("loadClassFromBytes("+name+", byte[])"); Class c = defineClass(name, data, 0, data.length); resolveClass(c); // This method has caused much trouble. Using it breaks jdk1.2rc1 // Not using it can make SUN's jdk1.1.6 JIT slightly unhappy. // Don't use by default, but allow python.options.compileClass to // override if (!Options.skipCompile) { // System.err.println("compile: "+name); Compiler.compileClass(c); } return c; } } Index: BytecodeLoader.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/BytecodeLoader.java,v retrieving revision 2.8 retrieving revision 2.9 diff -C2 -r2.8 -r2.9 *** BytecodeLoader.java 2000/11/19 22:32:45 2.8 --- BytecodeLoader.java 2000/12/15 03:19:00 2.9 *************** *** 5,42 **** import java.util.StringTokenizer; import java.util.Hashtable; public class BytecodeLoader extends ClassLoader { ! public InputStream getResourceAsStream(String res) { ! ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader != null) return classLoader.getResourceAsStream(res); ! ! classLoader = this.getClass().getClassLoader(); ! ! InputStream ret; ! ! if (classLoader != null) ret = classLoader.getResourceAsStream(res); ! else ret = ClassLoader.getSystemResourceAsStream(res); ! ! if (ret != null) return ret; ! ! if(res.charAt(0) == '/') res = res.substring(1); ! ! res.replace('/',File.separatorChar); ! ! PyList path = Py.getSystemState().path; ! for (int i=0; i < path.__len__(); i++) { ! String dir = path.get(i).__str__().toString(); ! if (dir.length() == 0) dir = null; ! try { ! return new BufferedInputStream(new FileInputStream(new File(dir,res))); ! } ! catch (IOException e) { ! continue; } } ! ! return null; } --- 5,36 ---- import java.util.StringTokenizer; import java.util.Hashtable; + import java.util.Vector; public class BytecodeLoader extends ClassLoader { ! private Vector parents; ! ! public BytecodeLoader() { ! this(null); ! } ! ! public BytecodeLoader(Vector referents) { ! parents = new Vector(); ! ! parents.addElement(imp.getSyspathJavaLoader()); ! ! if (referents != null) { ! for(int i = 0; i < referents.size(); i++) { ! try { ! ClassLoader cur = ((Class)referents.elementAt(i)).getClassLoader(); ! if (cur != null && !parents.contains(cur)) { ! parents.addElement(cur); ! } ! } catch(SecurityException e) { ! } } } ! } *************** *** 44,118 **** protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException ! { ! // System.err.println("loadClass("+name+", "+resolve+")"); ! // First, if the Python runtime system has a default class loader, ! // defer to it. ! ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader != null) ! return classLoader.loadClass(name); ! // Search the sys.path for a .class file matching the named class. ! // TBD: This registry option is temporary. ! try { ! return Class.forName(name); ! } ! catch(ClassNotFoundException e) { ! } ! Class c = findLoadedClass(name); if (c != null) return c; ! /* previously: if ! Options.extendedClassLoader && ! // KLUDGE ALERT: without this test, running jpython ! // interactively from the build directory will fail with ! // ClassCircularityError or LinkageError. There's gotta be a ! // better way. ! !name.startsWith("org.python") ! */ ! { ! PyList path = Py.getSystemState().path; ! for (int i=0; i < path.__len__(); i++) { ! String dir = path.get(i).__str__().toString(); ! FileInputStream fis = open(dir, name); ! if (fis == null) ! continue; ! try { ! int size = fis.available(); ! byte[] buffer = new byte[size]; ! fis.read(buffer); ! fis.close(); ! return loadClassFromBytes(name, buffer); ! } ! catch (IOException e) { ! continue; ! } } } // couldn't find the .class file on sys.path throw new ClassNotFoundException(name); - } - - private FileInputStream open(String dir, String name) { - String accum = ""; - boolean first = true; - for (StringTokenizer t = new StringTokenizer(name, "."); - t.hasMoreTokens();) - { - String token = t.nextToken(); - if (!first) - accum += File.separator; - accum += token; - first = false; - } - try { - if (dir.length() == 0) - dir = null; - return new FileInputStream(new File(dir, accum+".class")); - } - catch (FileNotFoundException e) { - return null; - } } --- 38,56 ---- protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException ! { Class c = findLoadedClass(name); if (c != null) return c; ! for (int i = 0; i < parents.size(); i++) { ! try { ! return ((ClassLoader)parents.elementAt(i)).loadClass(name); ! } catch(ClassNotFoundException e) { } } + // couldn't find the .class file on sys.path throw new ClassNotFoundException(name); } Index: MakeProxies.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/MakeProxies.java,v retrieving revision 2.11 retrieving revision 2.12 diff -C2 -r2.11 -r2.12 *** MakeProxies.java 2000/10/19 18:10:28 2.11 --- MakeProxies.java 2000/12/15 03:19:00 2.12 *************** *** 14,34 **** class MakeProxies { ! private static Class makeClass(Class referent, String name, ByteArrayOutputStream bytes) { ! BytecodeLoader bcl; ! ClassLoader cl; ! // Use the superclass's class loader if it is a BytecodeLoader. ! // referent might be null if we're deriving from an interface. In ! // that case, this may or may not be the RTTD. ! if (referent != null && ! (cl = referent.getClassLoader()) != null && ! cl instanceof BytecodeLoader) ! { ! bcl = (BytecodeLoader)cl; } ! else ! bcl = imp.getSyspathJavaLoader(); ! return bcl.makeClass(name, bytes.toByteArray()); } --- 14,35 ---- class MakeProxies { ! private static Class makeClass(Class referent, Vector secondary,String name, ByteArrayOutputStream bytes) { ! Vector referents = null; ! ! if (secondary != null) { ! if (referent != null) { ! secondary.insertElementAt(referent,0); ! referents = secondary; ! } ! } else { ! if (referent != null) { ! referents = new Vector(); ! referents.addElement(referent); ! } } ! ! return new BytecodeLoader(referents).makeClass(name, bytes.toByteArray()); } *************** *** 45,49 **** Py.saveClassFile(name, bytes); ! Class pc = makeClass(c, name, bytes); return pc; } --- 46,50 ---- Py.saveClassFile(name, bytes); ! Class pc = makeClass(c, null, name, bytes); return pc; } *************** *** 78,82 **** Py.saveClassFile(proxyName, bytes); ! return makeClass(superclass, jm.myClass, bytes); } catch (Exception exc) { --- 79,83 ---- Py.saveClassFile(proxyName, bytes); ! return makeClass(superclass, vinterfaces, jm.myClass, bytes); } catch (Exception exc) { Index: Options.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Options.java,v retrieving revision 2.3 retrieving revision 2.4 diff -C2 -r2.3 -r2.4 *** Options.java 2000/09/30 12:30:35 2.3 --- Options.java 2000/12/15 03:19:00 2.4 *************** *** 36,44 **** public static boolean respectJavaAccessibility = true; // Allow JPython's classloader to find and load .class files on // sys.path. This only happens for anonymous inner classes, but may // have unintended side-effects. This option is temporary. public static boolean extendedClassLoader = true; ! // TBD public static boolean importSite = true; --- 36,46 ---- public static boolean respectJavaAccessibility = true; + /* no longer necessary // Allow JPython's classloader to find and load .class files on // sys.path. This only happens for anonymous inner classes, but may // have unintended side-effects. This option is temporary. public static boolean extendedClassLoader = true; ! */ ! // TBD public static boolean importSite = true; *************** *** 102,109 **** getBooleanOption("security.respectJavaAccessibility", Options.respectJavaAccessibility); - - Options.extendedClassLoader = - getBooleanOption("options.extendedClassLoader", - Options.extendedClassLoader); Options.proxyDebugDirectory = --- 104,107 ---- Index: PySystemState.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PySystemState.java,v retrieving revision 2.35 retrieving revision 2.36 diff -C2 -r2.35 -r2.36 *** PySystemState.java 2000/12/06 20:58:56 2.35 --- PySystemState.java 2000/12/15 03:19:00 2.36 *************** *** 165,169 **** // This isn't quite right... ! builtins = PyJavaClass.lookup(__builtin__.class).__dict__; PyModule __builtin__ = new PyModule("__builtin__", builtins); modules.__setitem__("__builtin__", __builtin__); --- 165,169 ---- // This isn't quite right... ! builtins = PyJavaClass.lookup(__builtin__.class).__getattr__("__dict__"); PyModule __builtin__ = new PyModule("__builtin__", builtins); modules.__setitem__("__builtin__", __builtin__); Index: imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.32 retrieving revision 2.33 diff -C2 -r2.32 -r2.33 *** imp.java 2000/12/11 18:45:25 2.32 --- imp.java 2000/12/15 03:19:00 2.33 *************** *** 154,162 **** } ! private static BytecodeLoader syspathJavaLoader = null; ! public static synchronized BytecodeLoader getSyspathJavaLoader() { if (syspathJavaLoader == null) ! syspathJavaLoader = new BytecodeLoader(); return syspathJavaLoader; } --- 154,162 ---- } ! private static ClassLoader syspathJavaLoader = null; ! public static synchronized ClassLoader getSyspathJavaLoader() { if (syspathJavaLoader == null) ! syspathJavaLoader = new SyspathJavaLoader(); return syspathJavaLoader; } |