| 1 | /* **************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | **************************************************************************** */ |
| 16 | package org.jpype.pkg; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.lang.reflect.Modifier; |
| 21 | import java.net.URI; |
| 22 | import java.nio.Buffer; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.file.Files; |
| 25 | import java.nio.file.Path; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Map; |
| 28 | import org.jpype.JPypeClassLoader; |
| 29 | import org.jpype.JPypeContext; |
| 30 | import org.jpype.JPypeKeywords; |
| 31 | |
| 32 | /** |
| 33 | * Representation of a JPackage in Java. |
| 34 | * |
| 35 | * This provides the dir and attributes for a JPackage and by extension jpype |
| 36 | * imports. Almost all of the actual work happens in the PackageManager which |
| 37 | * acts like the classloader to figure out what resource are available. |
| 38 | * |
| 39 | */ |
| 40 | public class JPypePackage |
| 41 | { |
| 42 | |
| 43 | // Name of the package |
| 44 | final String pkg; |
| 45 | // A mapping from Python names into Paths into the module/jar file system. |
| 46 | Map<String, URI> contents; |
| 47 | int code; |
| 48 | private final JPypeClassLoader classLoader; |
| 49 | |
| 50 | public JPypePackage(String pkg) |
| 51 | { |
| 52 | this.pkg = pkg; |
| 53 | this.contents = JPypePackageManager.getContentMap(pkg); |
| 54 | this.classLoader = ((JPypeClassLoader) (JPypeContext.getInstance().getClassLoader())); |
| 55 | this.code = classLoader.getCode(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get an object from the package. |
| 60 | * |
| 61 | * This is used by the importer to create the attributes for `getattro`. The |
| 62 | * type returned is polymorphic. We can potentially support any type of |
| 63 | * resource (package, classes, property files, xml, data, etc). But for now we |
| 64 | * are primarily interested in packages and classes. Packages are returned as |
| 65 | * strings as loading the package info is not guaranteed to work. Classes are |
| 66 | * returned as classes which are immediately converted into Python wrappers. |
| 67 | * We can return other resource types so long as they have either a wrapper |
| 68 | * type to place the instance into an Python object directly or a magic |
| 69 | * wrapper which will load the resource into a Python object type. |
| 70 | * |
| 71 | * This should match the acceptable types in getContents so that everything in |
| 72 | * the `dir` is also an attribute of JPackage. |
| 73 | * |
| 74 | * @param name is the name of the resource. |
| 75 | * @return the object or null if no resource is found with a matching name. |
| 76 | */ |
| 77 | public Object getObject(String name) |
| 78 | { |
| 79 | // We can't use the url contents as the contents may be incomplete due |
| 80 | // to bugs in the JVM classloaders. Instead we will have to probe. |
| 81 | String basename = pkg + "." + JPypeKeywords.unwrap(name); |
| 82 | ClassLoader cl = JPypeContext.getInstance().getClassLoader(); |
| 83 | try |
| 84 | { |
| 85 | // Check if it a package |
| 86 | if (JPypePackageManager.isPackage(basename)) |
| 87 | { |
| 88 | return basename; |
| 89 | } |
| 90 | |
| 91 | // Else probe for a class. |
| 92 | Class<?> cls = Class.forName(basename, false, JPypeContext.getInstance().getClassLoader()); |
| 93 | if (Modifier.isPublic(cls.getModifiers())) |
| 94 | return Class.forName(basename, true, cl); |
| 95 | } catch (ClassNotFoundException ex) |
| 96 | { |
| 97 | // Continue |
| 98 | } |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get a list of contents from a Java package. |
| 104 | * |
| 105 | * This will be used when creating the package `dir` |
| 106 | * |
| 107 | * @return |
| 108 | */ |
| 109 | public String[] getContents() |
| 110 | { |
| 111 | checkCache(); |
| 112 | ArrayList<String> out = new ArrayList<>(); |
| 113 | for (String key : contents.keySet()) |
| 114 | { |
| 115 | URI uri = contents.get(key); |
| 116 | // If there is anything null, then skip it. |
| 117 | if (uri == null) |
| 118 | continue; |
| 119 | Path p = JPypePackageManager.getPath(uri); |
| 120 | |
| 121 | // package are acceptable |
| 122 | if (Files.isDirectory(p)) |
| 123 | out.add(key); |
| 124 | |
| 125 | // classes must be public |
| 126 | else if (uri.toString().endsWith(".class")) |
| 127 | { |
| 128 | // Make sure it is public |
| 129 | if (isPublic(p)) |
| 130 | out.add(key); |
| 131 | } |
| 132 | } |
| 133 | return out.toArray(new String[out.size()]); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Determine if a class is public. |
| 138 | * |
| 139 | * This checks if a class file contains a public class. When importing classes |
| 140 | * we do not want to instantiate a class which is not public as it may result |
| 141 | * in instantiation of static variables or unwanted class resources. The only |
| 142 | * alternative is to read the class file and get the class modifier flags. |
| 143 | * Unfortunately, the developers of Java were rather stingy on their byte |
| 144 | * allocation and thus the field we want is not in the header but rather |
| 145 | * buried after the constant pool. Further as they didn't give the actual size |
| 146 | * of the tables in bytes, but rather in entries, that means we have to parse |
| 147 | * the whole table just to get the access flags after it. |
| 148 | * |
| 149 | * @param p |
| 150 | * @return |
| 151 | */ |
| 152 | static boolean isPublic(Path p) |
| 153 | { |
| 154 | try (InputStream is = Files.newInputStream(p)) |
| 155 | { |
| 156 | // Allocate a three byte buffer for traversing the constant pool. |
| 157 | // The minumum entry is a byte for the type and 2 data bytes. We |
| 158 | // will read these three bytes and then based on the type advance |
| 159 | // the read pointer to the next entry. |
| 160 | ByteBuffer buffer3 = ByteBuffer.allocate(3); |
| 161 | |
| 162 | // Check the magic |
| 163 | ByteBuffer header = ByteBuffer.allocate(4 + 2 + 2 + 2); |
| 164 | is.read(header.array()); |
| 165 | ((Buffer) header).rewind(); |
| 166 | int magic = header.getInt(); |
| 167 | if (magic != (int) 0xcafebabe) |
| 168 | return false; |
| 169 | header.getShort(); // skip major |
| 170 | header.getShort(); // skip minor |
| 171 | short cpitems = header.getShort(); // get the number of items |
| 172 | |
| 173 | // Traverse the cp pool |
| 174 | for (int i = 0; i < cpitems - 1; ++i) |
| 175 | { |
| 176 | is.read(buffer3.array()); |
| 177 | ((Buffer) buffer3).rewind(); |
| 178 | byte type = buffer3.get(); // First byte is the type |
| 179 | |
| 180 | // Now based on the entry type we will advance the pointer |
| 181 | switch (type) |
| 182 | { |
| 183 | case 1: // Strings are variable length |
| 184 | is.skip(buffer3.getShort()); |
| 185 | break; |
| 186 | case 7: |
| 187 | case 8: |
| 188 | case 16: |
| 189 | case 19: |
| 190 | case 20: |
| 191 | break; |
| 192 | case 15: |
| 193 | is.skip(1); |
| 194 | break; |
| 195 | case 3: |
| 196 | case 4: |
| 197 | case 9: |
| 198 | case 10: |
| 199 | case 11: |
| 200 | case 12: |
| 201 | case 17: |
| 202 | case 18: |
| 203 | is.skip(2); |
| 204 | break; |
| 205 | case 5: |
| 206 | case 6: |
| 207 | is.skip(6); // double and long are special as they are double entries |
| 208 | i++; // long and double take two slots |
| 209 | break; |
| 210 | default: |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Get the flags |
| 216 | is.read(buffer3.array()); |
| 217 | ((Buffer) buffer3).rewind(); |
| 218 | short flags = buffer3.getShort(); |
| 219 | return (flags & 1) == 1; // it is public if bit zero is set |
| 220 | } catch (IOException ex) |
| 221 | { |
| 222 | return false; // If anything goes wrong then it won't be considered a public class. |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void checkCache() |
| 227 | { |
| 228 | int current = classLoader.getCode(); |
| 229 | if (this.code == current) |
| 230 | return; |
| 231 | this.code = current; |
| 232 | this.contents = JPypePackageManager.getContentMap(pkg); |
| 233 | } |
| 234 | |
| 235 | } |