KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.python.core;
5
6 import java.util.Properties JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8 import java.io.*;
9
10 /** System package manager.
11  * Used by org.python.core.PySystemState.
12  */

13 public class SysPackageManager extends PathPackageManager {
14
15     protected void message(String JavaDoc msg) {
16         Py.writeMessage("*sys-package-mgr*",msg);
17     }
18
19     protected void warning(String JavaDoc warn) {
20         Py.writeWarning("*sys-package-mgr*",warn);
21     }
22
23     protected void comment(String JavaDoc msg) {
24         Py.writeComment("*sys-package-mgr*",msg);
25     }
26
27     protected void debug(String JavaDoc msg) {
28         Py.writeDebug("*sys-package-mgr*",msg);
29     }
30
31     public SysPackageManager(File cachedir, Properties JavaDoc registry) {
32         if(useCacheDir(cachedir)) {
33             initCache();
34             findAllPackages(registry);
35             saveCache();
36         }
37     }
38
39     public void addJar(String JavaDoc jarfile, boolean cache) {
40         addJarToPackages(new File(jarfile), cache);
41         if (cache)
42             saveCache();
43     }
44
45     public void addJarDir(String JavaDoc jdir, boolean cache) {
46         addJarDir(jdir, cache, cache);
47     }
48
49     private void addJarDir(String JavaDoc jdir, boolean cache, boolean saveCache) {
50         File file = new File(jdir);
51         if (!file.isDirectory()) return;
52         String JavaDoc[] files = file.list();
53         for(int i=0; i<files.length; i++) {
54             String JavaDoc entry = files[i];
55             if (entry.endsWith(".jar") || entry.endsWith(".zip")) {
56                 addJarToPackages(new File(jdir,entry), cache);
57             }
58         }
59         if (saveCache)
60             saveCache();
61     }
62
63     private void addJarPath(String JavaDoc path) {
64         StringTokenizer JavaDoc tok =
65         new StringTokenizer JavaDoc(path, java.io.File.pathSeparator);
66         while (tok.hasMoreTokens()) {
67             // ??pending: do jvms trim? how is interpreted entry=""?
68
String JavaDoc entry = tok.nextToken();
69             addJarDir(entry, true, false);
70         }
71     }
72
73     private void findAllPackages(Properties JavaDoc registry) {
74         String JavaDoc paths = registry.getProperty(
75                               "python.packages.paths",
76                               "java.class.path,sun.boot.class.path");
77         String JavaDoc directories = registry.getProperty(
78                               "python.packages.directories",
79                               "java.ext.dirs");
80         String JavaDoc fakepath = registry.getProperty(
81                               "python.packages.fakepath", null);
82         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(paths, ",");
83         while (tok.hasMoreTokens()) {
84             String JavaDoc entry = tok.nextToken().trim();
85             String JavaDoc tmp = registry.getProperty(entry);
86             if (tmp == null) continue;
87             addClassPath(tmp);
88         }
89
90         tok = new StringTokenizer JavaDoc(directories, ",");
91         while (tok.hasMoreTokens()) {
92             String JavaDoc entry = tok.nextToken().trim();
93             String JavaDoc tmp = registry.getProperty(entry);
94             if (tmp == null) continue;
95             addJarPath(tmp);
96         }
97
98         if (fakepath != null) addClassPath(fakepath);
99     }
100
101     public void notifyPackageImport(String JavaDoc pkg, String JavaDoc name) {
102         if (pkg != null && pkg.length()>0) name = pkg + '.' + name;
103         Py.writeComment("import","'"+name+"' as java package");
104     }
105
106     public Class JavaDoc findClass(String JavaDoc pkg,String JavaDoc name) {
107         Class JavaDoc c = super.findClass(pkg,name);
108         if (c != null)
109             Py.writeComment("import","'"+name+"' as java class");
110         return c;
111     }
112
113     public Class JavaDoc findClass(String JavaDoc pkg, String JavaDoc name, String JavaDoc reason) {
114         if (pkg != null && pkg.length()>0) name = pkg + '.' + name;
115         return Py.findClassEx(name,reason);
116     }
117
118     public PyList doDir(PyJavaPackage jpkg, boolean instantiate,
119                         boolean exclpkgs)
120     {
121         PyList basic = basicDoDir(jpkg, instantiate, exclpkgs);
122         PyList ret = new PyList();
123
124         doDir(searchPath, ret, jpkg, instantiate, exclpkgs);
125
126         PySystemState system = Py.getSystemState();
127
128         if (system.getClassLoader() == null)
129             doDir(system.path, ret, jpkg, instantiate, exclpkgs);
130
131         return merge(basic,ret);
132     }
133
134     public boolean packageExists(String JavaDoc pkg, String JavaDoc name) {
135         if (packageExists(searchPath, pkg, name)) return true;
136
137         PySystemState system = Py.getSystemState();
138
139         if (system.getClassLoader() == null &&
140                  packageExists(Py.getSystemState().path,pkg,name)) {
141             return true;
142         }
143
144         return false;
145     }
146
147 }
148
Popular Tags