KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > SyspathJavaLoader


1 // Copyright (c) Corporation for National Research Initiatives
2
// Copyright 2000 Samuele Pedroni
3

4 package org.python.core;
5 import java.io.BufferedInputStream JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12 import java.util.zip.ZipEntry JavaDoc;
13
14 public class SyspathJavaLoader extends ClassLoader JavaDoc
15 {
16
17     public InputStream JavaDoc getResourceAsStream(String JavaDoc res) {
18         Py.writeDebug("resource", "trying resource: " + res);
19         ClassLoader JavaDoc classLoader = Py.getSystemState().getClassLoader();
20         if (classLoader != null) return classLoader.getResourceAsStream(res);
21
22         classLoader = this.getClass().getClassLoader();
23
24         InputStream JavaDoc 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 JavaDoc ze = archive.getEntry(res);
41                 if (ze != null) {
42                     try {
43                         return archive.getInputStream(ze);
44                     } catch (IOException JavaDoc e) { ; }
45                 }
46                 continue;
47             }
48             String JavaDoc dir = entry.__str__().toString();
49             if (dir.length() == 0) dir = null;
50             try {
51                 return new BufferedInputStream JavaDoc(
52                        new FileInputStream JavaDoc(new File JavaDoc(dir,res)));
53             }
54             catch (IOException JavaDoc e) {
55                 continue;
56             }
57         }
58
59         return null;
60     }
61
62     // override from abstract base class
63
protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
64         throws ClassNotFoundException JavaDoc
65     {
66 // System.err.println("loadClass("+name+", "+resolve+")");
67
// First, if the Python runtime system has a default class loader,
68
// defer to it.
69
ClassLoader JavaDoc classLoader = Py.getSystemState().getClassLoader();
70         if (classLoader != null)
71             return classLoader.loadClass(name);
72         // Search the sys.path for a .class file matching the named class.
73
// TBD: This registry option is temporary.
74
try {
75           return Class.forName(name);
76         }
77         catch(ClassNotFoundException JavaDoc e) {
78         }
79
80         Class JavaDoc c = findLoadedClass(name);
81         if (c != null)
82          return c;
83
84         /* previously: if
85             Options.extendedClassLoader &&
86             // KLUDGE ALERT: without this test, running jpython
87             // interactively from the build directory will fail with
88             // ClassCircularityError or LinkageError. There's gotta be a
89             // better way.
90             !name.startsWith("org.python")
91         */

92         {
93             PyList path = Py.getSystemState().path;
94             for (int i=0; i < path.__len__(); i++) {
95                 InputStream JavaDoc fis = null;
96                 PyObject entry = path.__getitem__(i);
97                 if (entry instanceof SyspathArchive) {
98                     SyspathArchive archive = (SyspathArchive) entry;
99                     String JavaDoc entryname = name.replace('.', File.separatorChar) +
100                                             ".class";
101                     ZipEntry JavaDoc ze = archive.getEntry(entryname);
102                     if (ze != null) {
103                         try {
104                             fis = archive.getInputStream(ze);
105                         } catch (IOException JavaDoc exc) { ; }
106                     }
107                 } else {
108                     String JavaDoc 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 JavaDoc e) {
120                     continue;
121                 }
122             }
123         }
124
125         // couldn't find the .class file on sys.path
126
throw new ClassNotFoundException JavaDoc(name);
127     }
128
129     private FileInputStream JavaDoc open(String JavaDoc dir, String JavaDoc name) {
130         String JavaDoc accum = "";
131         boolean first = true;
132         for (StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(name, ".");
133              t.hasMoreTokens();)
134         {
135             String JavaDoc 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 JavaDoc(new File JavaDoc(dir, accum+".class"));
145         }
146         catch (FileNotFoundException JavaDoc e) {
147             return null;
148         }
149     }
150
151     private Class JavaDoc loadClassFromBytes(String JavaDoc name, byte[] data) {
152 // System.err.println("loadClassFromBytes("+name+", byte[])");
153
Class JavaDoc c = defineClass(name, data, 0, data.length);
154         resolveClass(c);
155         // This method has caused much trouble. Using it breaks jdk1.2rc1
156
// Not using it can make SUN's jdk1.1.6 JIT slightly unhappy.
157
// Don't use by default, but allow python.options.compileClass to
158
// override
159
if (!Options.skipCompile) {
160 // System.err.println("compile: "+name);
161
Compiler.compileClass(c);
162         }
163         return c;
164     }
165
166 }
167
Popular Tags