Ignore:
Timestamp:
2018-09-09T22:03:36+02:00 (7 years ago)
Author:
donvip
Message:

see #josm16682 - first version of openjfx plugin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/openjfx/src/org/openstreetmap/josm/plugins/openjfx/OpenJfxPlugin.java

    r34603 r34623  
    22package org.openstreetmap.josm.plugins.openjfx;
    33
     4import java.io.File;
     5import java.io.IOException;
     6import java.io.InputStream;
     7import java.net.URISyntaxException;
     8import java.nio.charset.StandardCharsets;
     9import java.nio.file.FileVisitResult;
     10import java.nio.file.Files;
     11import java.nio.file.Path;
     12import java.nio.file.Paths;
     13import java.nio.file.SimpleFileVisitor;
     14import java.nio.file.StandardCopyOption;
     15import java.nio.file.attribute.BasicFileAttributes;
     16import java.security.CodeSource;
     17import java.util.Enumeration;
     18import java.util.Objects;
     19import java.util.zip.ZipEntry;
     20import java.util.zip.ZipFile;
     21
     22import org.openstreetmap.josm.data.Preferences;
    423import org.openstreetmap.josm.io.audio.AudioPlayer;
     24import org.openstreetmap.josm.plugins.DynamicURLClassLoader;
    525import org.openstreetmap.josm.plugins.Plugin;
    626import org.openstreetmap.josm.plugins.PluginInformation;
    727import org.openstreetmap.josm.plugins.openjfx.io.audio.JavaFxMediaPlayer;
     28import org.openstreetmap.josm.tools.Logging;
     29import org.openstreetmap.josm.tools.PlatformManager;
    830
    931/**
    10  * JAXB plugin brings OpenJFX (JavaFX) to other plugins.
     32 * OpenJFX plugin brings OpenJFX (JavaFX) to other plugins.
    1133 */
    1234public class OpenJfxPlugin extends Plugin {
     
    1941        super(info);
    2042        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        }
    21119    }
    22120}
Note: See TracChangeset for help on using the changeset viewer.