|
| 1 | +package org.python.core; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.TreeMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * Helper class handling the VM specific java package detection. |
| 9 | + */ |
| 10 | +public class JavaImportHelper { |
| 11 | + |
| 12 | + /** |
| 13 | + * Try to add the java package. |
| 14 | + * <p> |
| 15 | + * This is handy in cases where the package scan cannot run, or when the initial classpath does not contain all .jar |
| 16 | + * files (such as in J2EE containers). |
| 17 | + * <p> |
| 18 | + * There is some self-healing in the sense that a correct, explicit import of a java class will succeed even if |
| 19 | + * sys.modules already contains a Py.None entry for the corresponding java package. |
| 20 | + * |
| 21 | + * @param packageName The dotted name of the java package |
| 22 | + * @param fromlist A tuple with the from names to import. Can be null or empty. |
| 23 | + * |
| 24 | + * @return <code>true</code> if a java package was doubtlessly identified and added, <code>false</code> |
| 25 | + * otherwise. |
| 26 | + */ |
| 27 | + protected static boolean tryAddPackage(String packageName, PyObject fromlist) { |
| 28 | + // build the actual map with the packages known to the VM |
| 29 | + Map packages = buildLoadedPackages(); |
| 30 | + |
| 31 | + // make sure we do not turn off the added flag, once it is set |
| 32 | + boolean packageAdded = false; |
| 33 | + |
| 34 | + // handle package name |
| 35 | + if (isLoadedPackage(packageName, packages)) { |
| 36 | + packageAdded = addPackage(packageName); |
| 37 | + } |
| 38 | + String parentPackageName = packageName; |
| 39 | + int dotPos = 0; |
| 40 | + do { |
| 41 | + dotPos = parentPackageName.lastIndexOf("."); |
| 42 | + if (dotPos > 0) { |
| 43 | + parentPackageName = parentPackageName.substring(0, dotPos); |
| 44 | + if (isLoadedPackage(parentPackageName, packages)) { |
| 45 | + boolean parentAdded = addPackage(parentPackageName); |
| 46 | + if (parentAdded) { |
| 47 | + packageAdded = true; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } while (dotPos > 0); |
| 52 | + |
| 53 | + // handle fromlist |
| 54 | + if (fromlist != null && fromlist != Py.EmptyTuple && fromlist instanceof PyTuple) { |
| 55 | + Iterator iterator = ((PyTuple) fromlist).iterator(); |
| 56 | + while (iterator.hasNext()) { |
| 57 | + Object obj = iterator.next(); |
| 58 | + if (obj instanceof String) { |
| 59 | + String fromName = (String) obj; |
| 60 | + if (!"*".equals(fromName)) { |
| 61 | + boolean fromAdded = false; |
| 62 | + if (isJavaClass(packageName, fromName)) { |
| 63 | + fromAdded = addPackage(packageName); |
| 64 | + if (fromAdded) { |
| 65 | + packageAdded = true; |
| 66 | + } |
| 67 | + } else { |
| 68 | + // handle cases like: from java import math |
| 69 | + String fromPackageName = packageName + "." + fromName; |
| 70 | + if (isLoadedPackage(fromPackageName, packages)) { |
| 71 | + fromAdded = addPackage(fromPackageName); |
| 72 | + if (fromAdded) { |
| 73 | + packageAdded = true; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return packageAdded; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if a java package is already known to the VM. |
| 86 | + * <p> |
| 87 | + * May return <code>false</code> even if the given package name is a valid java package ! |
| 88 | + * |
| 89 | + * @param packageName |
| 90 | + * |
| 91 | + * @return <code>true</code> if the package with the given name is already loaded by the VM, <code>false</code> |
| 92 | + * otherwise. |
| 93 | + */ |
| 94 | + protected static boolean isLoadedPackage(String packageName) { |
| 95 | + return isLoadedPackage(packageName, buildLoadedPackages()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Faster way to check if a java package is already known to the VM. |
| 100 | + * <p> |
| 101 | + * May return <code>false</code> even if the given package name is a valid java package ! |
| 102 | + * |
| 103 | + * @param packageName |
| 104 | + * @param packages A Map containing all packages actually known to the VM. Such a Map can be obtained using |
| 105 | + * {@link JavaImportHelper.buildLoadedPackagesTree()} |
| 106 | + * |
| 107 | + * @return <code>true</code> if the package with the given name is already loaded by the VM, <code>false</code> |
| 108 | + * otherwise. |
| 109 | + */ |
| 110 | + private static boolean isLoadedPackage(String javaPackageName, Map packages) { |
| 111 | + return packages.containsKey(javaPackageName); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Build a <code>Map</code> of the currently known packages to the VM. |
| 116 | + * <p> |
| 117 | + * All parent packages appear as single entries like python modules, e.g. <code>java</code>, |
| 118 | + * <code>java.lang</code>, <code>java.lang.reflect</code>, |
| 119 | + */ |
| 120 | + private static Map buildLoadedPackages() { |
| 121 | + TreeMap packageMap = new TreeMap(); |
| 122 | + Package[] packages = Package.getPackages(); |
| 123 | + for (int i = 0; i < packages.length; i++) { |
| 124 | + String packageName = packages[i].getName(); |
| 125 | + packageMap.put(packageName, ""); |
| 126 | + int dotPos = 0; |
| 127 | + do { |
| 128 | + dotPos = packageName.lastIndexOf("."); |
| 129 | + if (dotPos > 0) { |
| 130 | + packageName = packageName.substring(0, dotPos); |
| 131 | + packageMap.put(packageName, ""); |
| 132 | + } |
| 133 | + } while (dotPos > 0); |
| 134 | + } |
| 135 | + return packageMap; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Check a java class on VM level. |
| 140 | + * |
| 141 | + * @param packageName |
| 142 | + * @param className |
| 143 | + * |
| 144 | + * @return <code>true</code> if the java class can be doubtlessly identified, <code>false</code> otherwise. |
| 145 | + */ |
| 146 | + private static boolean isJavaClass(String packageName, String className) { |
| 147 | + if (className != null && className.length() > 0) { |
| 148 | + className = packageName.replace('.', '/') + "/" + className + ".class"; |
| 149 | + return Thread.currentThread().getContextClassLoader().getResource(className) != null; |
| 150 | + } else { |
| 151 | + return false; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Add a java package to sys.modules, if not already done |
| 157 | + * |
| 158 | + * @return <code>true</code> if something was really added, <code>false</code> otherwise |
| 159 | + */ |
| 160 | + private static boolean addPackage(String packageName) { |
| 161 | + boolean added = false; |
| 162 | + PyObject module = Py.getSystemState().modules.__finditem__(packageName.intern()); |
| 163 | + if (module == null || module == Py.None) { |
| 164 | + PyObject modules = Py.getSystemState().modules; |
| 165 | + int dotPos; |
| 166 | + do { |
| 167 | + String internedPackageName = packageName.intern(); |
| 168 | + if (modules.__finditem__(internedPackageName) == Py.None) { |
| 169 | + // a previously failed import could have created a Py.None entry in sys.modules |
| 170 | + modules.__delitem__(internedPackageName); |
| 171 | + } |
| 172 | + PyJavaPackage p = PySystemState.add_package(packageName); |
| 173 | + Py.getSystemState().modules.__setitem__(internedPackageName, p); |
| 174 | + added = true; |
| 175 | + dotPos = packageName.lastIndexOf("."); |
| 176 | + if (dotPos > 0) { |
| 177 | + packageName = packageName.substring(0, dotPos); |
| 178 | + } |
| 179 | + } while (dotPos > 0); |
| 180 | + } |
| 181 | + return added; |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments