KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.python.core;
5
6 /** Abstract package manager.
7  */

8 public abstract class PackageManager extends Object JavaDoc {
9
10     public PyJavaPackage topLevelPackage;
11
12     public PackageManager() {
13         topLevelPackage = new PyJavaPackage("",this);
14     }
15
16     abstract public Class JavaDoc findClass(String JavaDoc pkg,String JavaDoc name,String JavaDoc reason);
17
18     public Class JavaDoc findClass(String JavaDoc pkg,String JavaDoc name) {
19         return findClass(pkg,name,"java class");
20     }
21
22     public void notifyPackageImport(String JavaDoc pkg,String JavaDoc name) {}
23
24     /** Dynamically check if pkg.name exists as java pkg in the
25      * controlled hierarchy.
26      * Should be overriden.
27      * @param pkg parent pkg name
28      * @param name candidate name
29      * @return true if pkg exists
30      */

31     public abstract boolean packageExists(String JavaDoc pkg,String JavaDoc name);
32
33     /** Reports the specified package content names. Should be overriden.
34      * Used by {@link PyJavaPackage#__dir__} and
35      * {@link PyJavaPackage#fillDir}.
36      * @return resulting list of names (PyList of PyString)
37      * @param jpkg queried package
38      * @param instantiate if true then instatiate reported names in
39      * package dict
40      * @param exclpkgs exclude packages (just when instantiate is false)
41      */

42     public abstract PyList doDir(PyJavaPackage jpkg, boolean instantiate,
43                                  boolean exclpkgs);
44
45     /**
46      * Append a directory to the list of directories searched for
47      * java packages and java classes.
48      *
49      * @param dir A directory.
50      */

51     public abstract void addDirectory(java.io.File JavaDoc dir);
52
53     /**
54      * Append a directory to the list of directories searched for
55      * java packages and java classes.
56      *
57      * @param dir A directory name.
58      */

59     public abstract void addJarDir(String JavaDoc dir, boolean cache);
60
61     /**
62      * Append a jar file to the list of locations searched for
63      * java packages and java classes.
64      *
65      * @param jarfile A directory name.
66      */

67     public abstract void addJar(String JavaDoc jarfile, boolean cache);
68
69     /** Basic helper implementation of {@link #doDir}.
70      * It merges information from jpkg {@link PyJavaPackage#clsSet}
71      * and {@link PyJavaPackage#__dict__}.
72      */

73     protected PyList basicDoDir(PyJavaPackage jpkg, boolean instantiate,
74                                 boolean exclpkgs)
75     {
76         PyStringMap dict = jpkg.__dict__;
77         PyStringMap cls = jpkg.clsSet;
78
79         if(!instantiate) {
80             PyList ret = cls.keys();
81
82             PyList dictKeys = dict.keys();
83
84             for(int i=0; i < dictKeys.__len__(); i++) {
85                 PyObject name = dictKeys.pyget(i);
86                 if (!cls.has_key(name)) {
87                     if (exclpkgs && dict.get(name) instanceof PyJavaPackage)
88                         continue;
89                     ret.append(name);
90                 }
91             }
92
93             return ret;
94         }
95
96         PyList clsNames = cls.keys();
97
98         for(int i=0; i < clsNames.__len__(); i++) {
99             PyObject name = clsNames.pyget(i);
100             if(!dict.has_key(name)) jpkg.addLazyClass(name.toString());
101         }
102
103         return dict.keys();
104     }
105
106     /** Helper merging list2 into list1. Returns list1.
107      */

108     protected PyList merge(PyList list1,PyList list2) {
109         for(int i=0; i < list2.__len__() ;i++) {
110             PyObject name = list2.pyget(i);
111             list1.append(name);
112         }
113
114         return list1;
115     }
116
117     public PyObject lookupName(String JavaDoc name) {
118         PyObject top = topLevelPackage;
119         do {
120             int dot = name.indexOf('.');
121             String JavaDoc firstName = name;
122             String JavaDoc lastName = null;
123             if (dot != -1) {
124                 firstName = name.substring(0,dot);
125                 lastName = name.substring(dot+1, name.length());
126             }
127             firstName = firstName.intern();
128             top = top.__findattr__(firstName);
129             if (top == null) return null;
130             // ??pending: test for jpkg/jclass?
131
name = lastName;
132         } while (name != null);
133         return top;
134     }
135
136     /** Creates package/updates statically known classes info.
137      * Uses {@link PyJavaPackage#addPackage(java.lang.String,
138      * java.lang.String) }, {@link PyJavaPackage#addPlaceholders}.
139      *
140      * @param name package name
141      * @param classes comma-separated string
142      * @param jarfile involved jarfile; can be null
143      * @return created/updated package
144      */

145     public PyJavaPackage makeJavaPackage(String JavaDoc name, String JavaDoc classes,
146                                          String JavaDoc jarfile)
147     {
148         PyJavaPackage p = topLevelPackage;
149         if(name.length() != 0) p=p.addPackage(name,jarfile);
150
151         if (classes != null) p.addPlaceholders(classes);
152
153         return p;
154     }
155
156     private static final int MAXSKIP = 512;
157
158     /** Check that a given stream is a valid Java .class file.
159      * And return its access permissions as an int.
160      */

161     static protected int checkAccess(java.io.InputStream JavaDoc cstream)
162         throws java.io.IOException JavaDoc
163     {
164         java.io.DataInputStream JavaDoc istream=new java.io.DataInputStream JavaDoc(cstream);
165
166         int magic = istream.readInt();
167         int minor = istream.readShort();
168         int major = istream.readShort();
169         if (magic != 0xcafebabe) return -1;
170         // Check versions???
171
//System.out.println("magic: "+magic+", "+major+", "+minor);
172
int nconstants = istream.readShort();
173         for(int i=1; i<nconstants; i++) {
174             int cid = istream.readByte();
175             //System.out.println(""+i+" : "+cid);
176
switch (cid) {
177                 case 7: istream.skipBytes(2); break;
178                 case 9:
179                 case 10:
180                 case 11: istream.skipBytes(4); break;
181                 case 8: istream.skipBytes(2); break;
182                 case 3:
183                 case 4: istream.skipBytes(4); break;
184                 case 5:
185                 case 6: istream.skipBytes(8); i++; break;
186                 case 12: istream.skipBytes(4); break;
187                 case 1:
188                 //System.out.println("utf: "+istream.readUTF()+";");
189
int slength = istream.readUnsignedShort();
190                 while (slength > MAXSKIP) { // workaround to java1.1 bug
191
istream.skipBytes(MAXSKIP);
192                     slength -= MAXSKIP;
193                 }
194                 istream.skipBytes(slength);
195                 break;
196                 default:
197                 //System.err.println("unexpected cid: "+cid+", "+i+", "+
198
// nconstants);
199
//for (int j=0; j<10; j++)
200
// System.err.print(", "+istream.readByte());
201
//System.err.println();
202
return -1;
203             }
204         }
205         return istream.readShort();
206     }
207
208
209 }
210
Popular Tags