| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package org.jpype.pkg; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.net.URI; |
| 20 |
import java.net.URISyntaxException; |
| 21 |
import java.net.URL; |
| 22 |
import java.net.URLDecoder; |
| 23 |
import java.nio.charset.StandardCharsets; |
| 24 |
import java.nio.file.FileSystem; |
| 25 |
import java.nio.file.FileSystemNotFoundException; |
| 26 |
import java.nio.file.FileSystems; |
| 27 |
import java.nio.file.Files; |
| 28 |
import java.nio.file.Path; |
| 29 |
import java.nio.file.Paths; |
| 30 |
import java.nio.file.ProviderNotFoundException; |
| 31 |
import java.nio.file.spi.FileSystemProvider; |
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Arrays; |
| 34 |
import java.util.Enumeration; |
| 35 |
import java.util.HashMap; |
| 36 |
import java.util.LinkedList; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
import org.jpype.JPypeContext; |
| 40 |
import org.jpype.JPypeKeywords; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class JPypePackageManager |
| 56 |
{ |
| 57 |
|
| 58 |
final static List<FileSystem> bases = new ArrayList(); |
| 59 |
final static List<ModuleDirectory> modules = getModules(); |
| 60 |
final static FileSystemProvider jfsp = getFileSystemProvider("jar"); |
| 61 |
final static Map<String, String> env = new HashMap<>(); |
| 62 |
final static LinkedList<FileSystem> fs = new LinkedList<>(); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static boolean isPackage(String name) |
| 72 |
{ |
| 73 |
if (name.indexOf('.') != -1) |
| 74 |
name = name.replace(".", "/"); |
| 75 |
if (isModulePackage(name) || isBasePackage(name) || isJarPackage(name)) |
| 76 |
return true; |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static Map<String, URI> getContentMap(String packageName) |
| 87 |
{ |
| 88 |
Map<String, URI> out = new HashMap<>(); |
| 89 |
packageName = packageName.replace(".", "/"); |
| 90 |
|
| 91 |
getJarContents(out, packageName); |
| 92 |
getBaseContents(out, packageName); |
| 93 |
getModuleContents(out, packageName); |
| 94 |
return out; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
static Path getPath(URI uri) |
| 106 |
{ |
| 107 |
try |
| 108 |
{ |
| 109 |
return Paths.get(uri); |
| 110 |
} catch (java.nio.file.FileSystemNotFoundException ex) |
| 111 |
{ |
| 112 |
} |
| 113 |
|
| 114 |
if (uri.getScheme().equals("jar")) |
| 115 |
{ |
| 116 |
try |
| 117 |
{ |
| 118 |
|
| 119 |
fs.add(jfsp.newFileSystem(uri, env)); |
| 120 |
if (fs.size() > 8) |
| 121 |
fs.removeFirst().close(); |
| 122 |
return Paths.get(uri); |
| 123 |
} catch (IOException ex) |
| 124 |
{ |
| 125 |
} |
| 126 |
} |
| 127 |
throw new FileSystemNotFoundException("Unknown filesystem for " + uri); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private static FileSystemProvider getFileSystemProvider(String str) |
| 136 |
{ |
| 137 |
for (FileSystemProvider fsp : FileSystemProvider.installedProviders()) |
| 138 |
{ |
| 139 |
if (fsp.getScheme().equals(str)) |
| 140 |
return fsp; |
| 141 |
} |
| 142 |
throw new FileSystemNotFoundException("Unable to find filesystem for " + str); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static |
| 153 |
{ |
| 154 |
env.put("create", "true"); |
| 155 |
|
| 156 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
| 157 |
URI uri = null; |
| 158 |
try |
| 159 |
{ |
| 160 |
|
| 161 |
|
| 162 |
uri = cl.getResource("java/lang/String.class").toURI(); |
| 163 |
if (uri != null && uri.getScheme().equals("jar")) |
| 164 |
{ |
| 165 |
FileSystem fs = jfsp.newFileSystem(uri, env); |
| 166 |
if (fs != null) |
| 167 |
bases.add(fs); |
| 168 |
} |
| 169 |
uri = cl.getResource("javax/crypto/Cipher.class").toURI(); |
| 170 |
if (uri != null && uri.getScheme().equals("jar")) |
| 171 |
{ |
| 172 |
FileSystem fs = jfsp.newFileSystem(uri, env); |
| 173 |
if (fs != null) |
| 174 |
bases.add(fs); |
| 175 |
} |
| 176 |
} catch (URISyntaxException | IOException ex) |
| 177 |
{ |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
private static void getBaseContents(Map<String, URI> out, String packageName) |
| 182 |
{ |
| 183 |
for (FileSystem b : bases) |
| 184 |
{ |
| 185 |
collectContents(out, b.getPath(packageName)); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private static boolean isBasePackage(String name) |
| 196 |
{ |
| 197 |
try |
| 198 |
{ |
| 199 |
if (name.isEmpty()) |
| 200 |
return false; |
| 201 |
for (FileSystem jar : bases) |
| 202 |
{ |
| 203 |
if (Files.isDirectory(jar.getPath(name))) |
| 204 |
return true; |
| 205 |
} |
| 206 |
return false; |
| 207 |
} catch (Exception ex) |
| 208 |
{ |
| 209 |
throw new RuntimeException("Fail checking package '" + name + "'", ex); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static List<ModuleDirectory> getModules() |
| 224 |
{ |
| 225 |
ArrayList<ModuleDirectory> out = new ArrayList<>(); |
| 226 |
try |
| 227 |
{ |
| 228 |
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
| 229 |
Path modulePath = fs.getPath("modules"); |
| 230 |
for (Path module : Files.newDirectoryStream(modulePath)) |
| 231 |
{ |
| 232 |
out.add(new ModuleDirectory(module)); |
| 233 |
} |
| 234 |
} catch (ProviderNotFoundException | IOException ex) |
| 235 |
{ |
| 236 |
} |
| 237 |
return out; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
private static boolean isModulePackage(String name) |
| 247 |
{ |
| 248 |
if (modules.isEmpty()) |
| 249 |
return false; |
| 250 |
String[] split = name.split("/"); |
| 251 |
String search = name; |
| 252 |
if (split.length > 3) |
| 253 |
search = String.join("/", Arrays.copyOfRange(split, 0, 3)); |
| 254 |
for (ModuleDirectory module : modules) |
| 255 |
{ |
| 256 |
if (module.contains(search)) |
| 257 |
{ |
| 258 |
if (Files.isDirectory(module.modulePath.resolve(name))) |
| 259 |
return true; |
| 260 |
} |
| 261 |
} |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
private static void getModuleContents(Map<String, URI> out, String name) |
| 272 |
{ |
| 273 |
if (modules.isEmpty()) |
| 274 |
return; |
| 275 |
String[] split = name.split("/"); |
| 276 |
String search = name; |
| 277 |
if (split.length > 3) |
| 278 |
search = String.join("/", Arrays.copyOfRange(split, 0, 3)); |
| 279 |
for (ModuleDirectory module : modules) |
| 280 |
{ |
| 281 |
if (module.contains(search)) |
| 282 |
{ |
| 283 |
Path path2 = module.modulePath.resolve(name); |
| 284 |
if (Files.isDirectory(path2)) |
| 285 |
collectContents(out, path2); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
private static class ModuleDirectory |
| 298 |
{ |
| 299 |
|
| 300 |
List<String> contents = new ArrayList<>(); |
| 301 |
private final Path modulePath; |
| 302 |
|
| 303 |
ModuleDirectory(Path module) |
| 304 |
{ |
| 305 |
this.modulePath = module; |
| 306 |
listPackages(contents, module, module, 0); |
| 307 |
} |
| 308 |
|
| 309 |
boolean contains(String path) |
| 310 |
{ |
| 311 |
for (String s : contents) |
| 312 |
{ |
| 313 |
if (s.equals(path)) |
| 314 |
return true; |
| 315 |
} |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
private static void listPackages(List<String> o, Path base, Path p, int depth) |
| 320 |
{ |
| 321 |
try |
| 322 |
{ |
| 323 |
if (depth >= 3) |
| 324 |
return; |
| 325 |
for (Path d : Files.newDirectoryStream(p)) |
| 326 |
{ |
| 327 |
if (Files.isDirectory(d)) |
| 328 |
{ |
| 329 |
o.add(base.relativize(d).toString()); |
| 330 |
listPackages(o, base, d, depth + 1); |
| 331 |
} |
| 332 |
} |
| 333 |
} catch (IOException ex) |
| 334 |
{ |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
private static boolean isJarPackage(String name) |
| 354 |
{ |
| 355 |
ClassLoader cl = JPypeContext.getInstance().getClassLoader(); |
| 356 |
try |
| 357 |
{ |
| 358 |
Enumeration<URL> resources = cl.getResources(name); |
| 359 |
while (resources.hasMoreElements()) |
| 360 |
{ |
| 361 |
URI uri = resources.nextElement().toURI(); |
| 362 |
if (Files.isDirectory(getPath(uri))) |
| 363 |
return true; |
| 364 |
} |
| 365 |
} catch (IOException | URISyntaxException ex) |
| 366 |
{ |
| 367 |
} |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
private static void getJarContents(Map<String, URI> out, String packageName) |
| 379 |
{ |
| 380 |
ClassLoader cl = JPypeContext.getInstance().getClassLoader(); |
| 381 |
try |
| 382 |
{ |
| 383 |
String path = packageName.replace('.', '/'); |
| 384 |
Enumeration<URL> resources = cl.getResources(path); |
| 385 |
while (resources.hasMoreElements()) |
| 386 |
{ |
| 387 |
URI resource = resources.nextElement().toURI(); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
String schemePart = resource.getSchemeSpecificPart(); |
| 395 |
int index = schemePart.indexOf("!"); |
| 396 |
if (index != -1) |
| 397 |
{ |
| 398 |
if (schemePart.substring(index + 1).startsWith("/META-INF/versions/")) |
| 399 |
{ |
| 400 |
int index2 = schemePart.indexOf('/', index + 20); |
| 401 |
schemePart = schemePart.substring(0, index + 1) + schemePart.substring(index2); |
| 402 |
URI resource2 = new URI(resource.getScheme() + ":" + schemePart); |
| 403 |
Path path3 = getPath(resource2); |
| 404 |
collectContents(out, path3); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
Path path2 = getPath(resource); |
| 409 |
collectContents(out, path2); |
| 410 |
} |
| 411 |
} catch (IOException | URISyntaxException ex) |
| 412 |
{ |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
private static void collectContents(Map<String, URI> out, Path path2) |
| 430 |
{ |
| 431 |
try |
| 432 |
{ |
| 433 |
for (Path file : Files.newDirectoryStream(path2)) |
| 434 |
{ |
| 435 |
String filename = file.getFileName().toString(); |
| 436 |
if (Files.isDirectory(file)) |
| 437 |
{ |
| 438 |
|
| 439 |
if (filename.endsWith(file.getFileSystem().getSeparator())) |
| 440 |
filename = filename.substring(0, filename.length() - 1); |
| 441 |
out.put(JPypeKeywords.wrap(filename), toURI(file)); |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
if (filename.contains("$")) |
| 446 |
continue; |
| 447 |
|
| 448 |
|
| 449 |
if (filename.endsWith(".class")) |
| 450 |
{ |
| 451 |
String key = JPypeKeywords.wrap(filename.substring(0, filename.length() - 6)); |
| 452 |
out.put(key, toURI(file)); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
} |
| 458 |
} catch (IOException ex) |
| 459 |
{ |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
private static URI toURI(Path path) |
| 464 |
{ |
| 465 |
URI uri = path.toUri(); |
| 466 |
|
| 467 |
try |
| 468 |
{ |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
uri = new URI( |
| 473 |
uri.getScheme(), |
| 474 |
URLDecoder.decode(uri.getSchemeSpecificPart(), StandardCharsets.UTF_8), |
| 475 |
uri.getFragment() |
| 476 |
); |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
return new URI(uri.toASCIIString()); |
| 485 |
} catch (Exception e) |
| 486 |
{ |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
throw new RuntimeException("Failed to encode URI: " + uri, e); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
} |