Ignore:
Timestamp:
2021-08-23T01:12:53+02:00 (3 years ago)
Author:
Don-vip
Message:

see #17858 - do no longer ship Java FX through giant openjfx plugin

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  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
    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;
    185import 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;
    246
    25 import org.openstreetmap.josm.data.Preferences;
    267import org.openstreetmap.josm.gui.MainApplication;
    278import org.openstreetmap.josm.io.audio.AudioPlayer;
    28 import org.openstreetmap.josm.plugins.DynamicURLClassLoader;
    299import org.openstreetmap.josm.plugins.Plugin;
    3010import org.openstreetmap.josm.plugins.PluginHandler;
     
    3313import org.openstreetmap.josm.tools.Logging;
    3414import org.openstreetmap.josm.tools.PlatformManager;
    35 import org.openstreetmap.josm.tools.Utils;
    3615
    3716/**
    38  * JavaFX plugin brings OpenJFX (JavaFX) to other plugins.
     17 * JavaFX plugin brings OpenJFX (JavaFX) additional features.
    3918 */
    40 abstract class JavaFxPlugin extends Plugin {
     19public class JavaFxPlugin extends Plugin {
    4120
    4221    /**
    43      * Constructs a new {@code OpenJfxPlugin}.
     22     * Constructs a new {@code JavaFxPlugin}.
    4423     * @param info plugin info
    45      * @param ext native libraries extension
    46      * @param orderedNativeLibraries native libraries that must be loaded in this order
    4724     */
    48     protected JavaFxPlugin(PluginInformation info, String ext, List<String> orderedNativeLibraries) {
     25    protected JavaFxPlugin(PluginInformation info) {
    4926        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()) {
    5528            Logging.error("JavaFX is not available");
    5629            StringBuilder message = new StringBuilder(tr("JavaFX is not available."));
     
    6942    }
    7043
    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() {
    13245        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;
    14950        }
    15051    }
  • applications/editors/josm/plugins/javafx/src/org/openstreetmap/josm/plugins/javafx/gui/JavaFxWrapper.java

    r35492 r35805  
    101101
    102102    /**
     103     * Initializes FX
    103104     * @return The scene to be used for initializing JavaFX
    104105     */
Note: See TracChangeset for help on using the changeset viewer.