Changeset 34623 in osm for applications/editors/josm/plugins/openjfx/src
- Timestamp:
- 2018-09-09T22:03:36+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/openjfx/src/org/openstreetmap/josm/plugins/openjfx/OpenJfxPlugin.java
r34603 r34623 2 2 package org.openstreetmap.josm.plugins.openjfx; 3 3 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.net.URISyntaxException; 8 import java.nio.charset.StandardCharsets; 9 import java.nio.file.FileVisitResult; 10 import java.nio.file.Files; 11 import java.nio.file.Path; 12 import java.nio.file.Paths; 13 import java.nio.file.SimpleFileVisitor; 14 import java.nio.file.StandardCopyOption; 15 import java.nio.file.attribute.BasicFileAttributes; 16 import java.security.CodeSource; 17 import java.util.Enumeration; 18 import java.util.Objects; 19 import java.util.zip.ZipEntry; 20 import java.util.zip.ZipFile; 21 22 import org.openstreetmap.josm.data.Preferences; 4 23 import org.openstreetmap.josm.io.audio.AudioPlayer; 24 import org.openstreetmap.josm.plugins.DynamicURLClassLoader; 5 25 import org.openstreetmap.josm.plugins.Plugin; 6 26 import org.openstreetmap.josm.plugins.PluginInformation; 7 27 import org.openstreetmap.josm.plugins.openjfx.io.audio.JavaFxMediaPlayer; 28 import org.openstreetmap.josm.tools.Logging; 29 import org.openstreetmap.josm.tools.PlatformManager; 8 30 9 31 /** 10 * JAXBplugin brings OpenJFX (JavaFX) to other plugins.32 * OpenJFX plugin brings OpenJFX (JavaFX) to other plugins. 11 33 */ 12 34 public class OpenJfxPlugin extends Plugin { … … 19 41 super(info); 20 42 AudioPlayer.setSoundPlayerClass(JavaFxMediaPlayer.class); 43 String ext = null; 44 if (PlatformManager.isPlatformWindows()) { 45 ext = ".dll"; 46 } else if (PlatformManager.isPlatformUnixoid()) { 47 ext = ".so"; 48 } else if (PlatformManager.isPlatformOsx()) { 49 ext = ".dylib"; 50 } 51 extractNativeLibs(ext); 52 loadNativeLibs(ext); 53 } 54 55 private static void extractNativeLibs(String ext) { 56 CodeSource src = OpenJfxPlugin.class.getProtectionDomain().getCodeSource(); 57 if (src != null) { 58 try (ZipFile zf = new ZipFile(Paths.get(src.getLocation().toURI()).toFile(), StandardCharsets.UTF_8)) { 59 Path dir = getNativeDir(); 60 Enumeration<? extends ZipEntry> es = zf.entries(); 61 while (es.hasMoreElements()) { 62 ZipEntry ze = es.nextElement(); 63 String name = ze.getName(); 64 if (name.endsWith(ext) || name.endsWith(".jar")) { 65 Path targetPath = dir.resolve(name); 66 File targetFile = targetPath.toFile(); 67 if (!targetFile.exists() || targetFile.lastModified() < ze.getTime()) { 68 try (InputStream is = zf.getInputStream(ze)) { 69 Logging.debug("Extracting " + targetPath); 70 Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING); 71 } 72 } 73 } 74 } 75 } catch (IOException | URISyntaxException e) { 76 Logging.error(e); 77 } 78 } else { 79 Logging.error("Unable to locate openjfx jar file"); 80 } 81 } 82 83 private static Path getNativeDir() throws IOException { 84 return Files.createDirectories(new File(Preferences.main().getPluginsDirectory(), "openjfx").toPath()); 85 } 86 87 private static class LibVisitor extends SimpleFileVisitor<Path> { 88 private final ClassLoader ccl = Thread.currentThread().getContextClassLoader(); 89 private final String ext; 90 91 public LibVisitor(String ext) { 92 this.ext = Objects.requireNonNull(ext); 93 } 94 95 @Override 96 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 97 if (ccl instanceof DynamicURLClassLoader) { 98 if (file.endsWith(ext)) { 99 Logging.debug("Loading " + file); 100 System.load(file.toAbsolutePath().toString()); 101 } else if (file.endsWith(".jar")) { 102 Logging.debug("Loading " + file); 103 ((DynamicURLClassLoader) ccl).addURL(file.toUri().toURL()); 104 } 105 } else { 106 Logging.error("Unexpected context class loader: " + ccl); 107 return FileVisitResult.TERMINATE; 108 } 109 return FileVisitResult.CONTINUE; 110 } 111 } 112 113 private void loadNativeLibs(String ext) { 114 try { 115 Files.walkFileTree(getNativeDir(), new LibVisitor(ext)); 116 } catch (IOException e) { 117 Logging.error(e); 118 } 21 119 } 22 120 }
Note:
See TracChangeset
for help on using the changeset viewer.