1 4 package org.python.core; 5 import java.io.BufferedInputStream ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.util.StringTokenizer ; 12 import java.util.zip.ZipEntry ; 13 14 public class SyspathJavaLoader extends ClassLoader 15 { 16 17 public InputStream getResourceAsStream(String res) { 18 Py.writeDebug("resource", "trying resource: " + res); 19 ClassLoader classLoader = Py.getSystemState().getClassLoader(); 20 if (classLoader != null) return classLoader.getResourceAsStream(res); 21 22 classLoader = this.getClass().getClassLoader(); 23 24 InputStream ret; 25 26 if (classLoader != null) ret = classLoader.getResourceAsStream(res); 27 else ret = ClassLoader.getSystemResourceAsStream(res); 28 29 if (ret != null) return ret; 30 31 if(res.charAt(0) == '/') res = res.substring(1); 32 33 res.replace('/',File.separatorChar); 34 35 PyList path = Py.getSystemState().path; 36 for (int i=0; i < path.__len__(); i++) { 37 PyObject entry = path.__getitem__(i); 38 if (entry instanceof SyspathArchive) { 39 SyspathArchive archive = (SyspathArchive) entry; 40 ZipEntry ze = archive.getEntry(res); 41 if (ze != null) { 42 try { 43 return archive.getInputStream(ze); 44 } catch (IOException e) { ; } 45 } 46 continue; 47 } 48 String dir = entry.__str__().toString(); 49 if (dir.length() == 0) dir = null; 50 try { 51 return new BufferedInputStream ( 52 new FileInputStream (new File (dir,res))); 53 } 54 catch (IOException e) { 55 continue; 56 } 57 } 58 59 return null; 60 } 61 62 protected Class loadClass(String name, boolean resolve) 64 throws ClassNotFoundException 65 { 66 ClassLoader classLoader = Py.getSystemState().getClassLoader(); 70 if (classLoader != null) 71 return classLoader.loadClass(name); 72 try { 75 return Class.forName(name); 76 } 77 catch(ClassNotFoundException e) { 78 } 79 80 Class c = findLoadedClass(name); 81 if (c != null) 82 return c; 83 84 92 { 93 PyList path = Py.getSystemState().path; 94 for (int i=0; i < path.__len__(); i++) { 95 InputStream fis = null; 96 PyObject entry = path.__getitem__(i); 97 if (entry instanceof SyspathArchive) { 98 SyspathArchive archive = (SyspathArchive) entry; 99 String entryname = name.replace('.', File.separatorChar) + 100 ".class"; 101 ZipEntry ze = archive.getEntry(entryname); 102 if (ze != null) { 103 try { 104 fis = archive.getInputStream(ze); 105 } catch (IOException exc) { ; } 106 } 107 } else { 108 String dir = entry.__str__().toString(); 109 fis = open(dir, name); 110 } 111 if (fis == null) { 112 continue; 113 } 114 try { 115 byte[] buffer = FileUtil.readBytes( fis ); 116 fis.close(); 117 return loadClassFromBytes(name, buffer); 118 } 119 catch (IOException e) { 120 continue; 121 } 122 } 123 } 124 125 throw new ClassNotFoundException (name); 127 } 128 129 private FileInputStream open(String dir, String name) { 130 String accum = ""; 131 boolean first = true; 132 for (StringTokenizer t = new StringTokenizer (name, "."); 133 t.hasMoreTokens();) 134 { 135 String token = t.nextToken(); 136 if (!first) 137 accum += File.separator; 138 accum += token; 139 first = false; 140 } 141 try { 142 if (dir.length() == 0) 143 dir = null; 144 return new FileInputStream (new File (dir, accum+".class")); 145 } 146 catch (FileNotFoundException e) { 147 return null; 148 } 149 } 150 151 private Class loadClassFromBytes(String name, byte[] data) { 152 Class c = defineClass(name, data, 0, data.length); 154 resolveClass(c); 155 if (!Options.skipCompile) { 160 Compiler.compileClass(c); 162 } 163 return c; 164 } 165 166 } 167 | Popular Tags |