Changeset 35805 in osm for applications/editors/josm/plugins/javafx/src
- Timestamp:
- 2021-08-23T01:12:53+02:00 (3 years ago)
- Location:
- applications/editors/josm/plugins/javafx/src/org/openstreetmap/josm/plugins/javafx
- Files:
-
- 3 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/javafx/src/org/openstreetmap/josm/plugins/javafx/JavaFxPlugin.java
r35371 r35805 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.io.File;6 import java.io.IOException;7 import java.io.InputStream;8 import java.net.URISyntaxException;9 import java.nio.charset.StandardCharsets;10 import java.nio.file.FileVisitResult;11 import java.nio.file.Files;12 import java.nio.file.Path;13 import java.nio.file.Paths;14 import java.nio.file.SimpleFileVisitor;15 import java.nio.file.StandardCopyOption;16 import java.nio.file.attribute.BasicFileAttributes;17 import java.security.CodeSource;18 5 import java.util.Collections; 19 import java.util.Enumeration;20 import java.util.List;21 import java.util.Objects;22 import java.util.zip.ZipEntry;23 import java.util.zip.ZipFile;24 6 25 import org.openstreetmap.josm.data.Preferences;26 7 import org.openstreetmap.josm.gui.MainApplication; 27 8 import org.openstreetmap.josm.io.audio.AudioPlayer; 28 import org.openstreetmap.josm.plugins.DynamicURLClassLoader;29 9 import org.openstreetmap.josm.plugins.Plugin; 30 10 import org.openstreetmap.josm.plugins.PluginHandler; … … 33 13 import org.openstreetmap.josm.tools.Logging; 34 14 import org.openstreetmap.josm.tools.PlatformManager; 35 import org.openstreetmap.josm.tools.Utils;36 15 37 16 /** 38 * JavaFX plugin brings OpenJFX (JavaFX) to other plugins.17 * JavaFX plugin brings OpenJFX (JavaFX) additional features. 39 18 */ 40 abstractclass JavaFxPlugin extends Plugin {19 public class JavaFxPlugin extends Plugin { 41 20 42 21 /** 43 * Constructs a new {@code OpenJfxPlugin}.22 * Constructs a new {@code JavaFxPlugin}. 44 23 * @param info plugin info 45 * @param ext native libraries extension46 * @param orderedNativeLibraries native libraries that must be loaded in this order47 24 */ 48 protected JavaFxPlugin(PluginInformation info , String ext, List<String> orderedNativeLibraries) {25 protected JavaFxPlugin(PluginInformation info) { 49 26 super(info); 50 boolean isJavaFx = Utils.getSystemProperty("javafx.runtime.version") != null || ("Oracle Corporation".equals(Utils.getSystemProperty("java.vendor")) && Utils.getJavaVersion() < 11); 51 if (!isJavaFx && Utils.getJavaVersion() >= 10) { 52 extractNativeLibs(ext); 53 loadNativeLibs(ext, orderedNativeLibraries); 54 } else if (!isJavaFx) { 27 if (!isJavaFx()) { 55 28 Logging.error("JavaFX is not available"); 56 29 StringBuilder message = new StringBuilder(tr("JavaFX is not available.")); … … 69 42 } 70 43 71 private static void extractNativeLibs(String ext) { 72 CodeSource src = JavaFxPlugin.class.getProtectionDomain().getCodeSource(); 73 if (src != null) { 74 try (ZipFile zf = new ZipFile(Paths.get(src.getLocation().toURI()).toFile(), StandardCharsets.UTF_8)) { 75 Path dir = getNativeDir(); 76 Enumeration<? extends ZipEntry> es = zf.entries(); 77 while (es.hasMoreElements()) { 78 ZipEntry ze = es.nextElement(); 79 String name = ze.getName(); 80 if (name.endsWith(ext) || name.endsWith(".jar")) { 81 Path targetPath = dir.resolve(name); 82 File targetFile = targetPath.toFile(); 83 if (!targetFile.exists() || targetFile.lastModified() < ze.getTime()) { 84 try (InputStream is = zf.getInputStream(ze)) { 85 Logging.debug("Extracting " + targetPath); 86 Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING); 87 } 88 } 89 } 90 } 91 } catch (IOException | URISyntaxException e) { 92 Logging.error(e); 93 } 94 } else { 95 Logging.error("Unable to locate javafx jar file"); 96 } 97 } 98 99 private static Path getNativeDir() throws IOException { 100 return Files.createDirectories(new File(Preferences.main().getPluginsDirectory(), "javafx").toPath()); 101 } 102 103 private static class LibVisitor extends SimpleFileVisitor<Path> { 104 private final ClassLoader ccl = Thread.currentThread().getContextClassLoader(); 105 private final String ext; 106 private final List<String> orderedNativeLibraries; 107 108 public LibVisitor(String ext, List<String> orderedNativeLibraries) { 109 this.ext = Objects.requireNonNull(ext); 110 this.orderedNativeLibraries = Objects.requireNonNull(orderedNativeLibraries); 111 } 112 113 @Override 114 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 115 if (ccl instanceof DynamicURLClassLoader) { 116 String path = file.toAbsolutePath().toString(); 117 if (path.endsWith(ext) && !orderedNativeLibraries.contains(file.getFileName().toString())) { 118 loadNativeLib(path); 119 } else if (path.endsWith(".jar")) { 120 Logging.debug("Loading {0}", path); 121 ((DynamicURLClassLoader) ccl).addURL(file.toUri().toURL()); 122 } 123 } else { 124 Logging.error("Unexpected context class loader: " + ccl); 125 return FileVisitResult.TERMINATE; 126 } 127 return FileVisitResult.CONTINUE; 128 } 129 } 130 131 private static void loadNativeLib(String absolutePath) { 44 private boolean isJavaFx() { 132 45 try { 133 Logging.debug("Loading {0}", absolutePath); 134 System.load(absolutePath); 135 } catch (LinkageError e) { 136 Logging.error(e); 137 } 138 } 139 140 private static void loadNativeLibs(String ext, List<String> orderedNativeLibraries) { 141 try { 142 Path nativeDir = getNativeDir(); 143 Files.walkFileTree(nativeDir, new LibVisitor(ext, orderedNativeLibraries)); 144 for (String lib : orderedNativeLibraries) { 145 loadNativeLib(nativeDir.resolve(lib).toAbsolutePath().toString()); 146 } 147 } catch (IOException e) { 148 Logging.error(e); 46 return Class.forName("javafx.scene.Node") != null && Class.forName("javafx.scene.media.Media") != null; 47 } catch (ClassNotFoundException e) { 48 Logging.trace(e); 49 return false; 149 50 } 150 51 } -
applications/editors/josm/plugins/javafx/src/org/openstreetmap/josm/plugins/javafx/gui/JavaFxWrapper.java
r35492 r35805 101 101 102 102 /** 103 * Initializes FX 103 104 * @return The scene to be used for initializing JavaFX 104 105 */
Note:
See TracChangeset
for help on using the changeset viewer.