aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
blob: 5d1b9d7b8dc2c73c920730eeab7f4c82a01421a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cz.crcs.ectester.standalone.libs;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.Provider;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class NativeECLibrary extends ProviderECLibrary {
    private String resource;
    private String[] requriements;

    public static String LIB_RESOURCE_DIR = "/cz/crcs/ectester/standalone/libs/jni/";

    public NativeECLibrary(String resource, String... requirements) {
        this.resource = resource;
        this.requriements = requirements;
    }

    @Override
    public boolean initialize() {
        try {
            String suffix;
            Path appData;
            if (System.getProperty("os.name").startsWith("Windows")) {
                suffix = "dll";
                appData = Paths.get(System.getenv("AppData"));
            } else {
                suffix = "so";
                if (System.getProperty("os.name").startsWith("Linux")) {
                    String dataHome = System.getenv("XDG_DATA_HOME");
                    if (dataHome != null) {
                        appData = Paths.get(dataHome);
                    } else {
                        appData = Paths.get(System.getProperty("user.home"), ".local", "share");
                    }
                } else {
                    appData = Paths.get(System.getProperty("user.home"), ".local", "share");
                }
            }
            Path libDir = appData.resolve("ECTesterStandalone");
            File libDirFile = libDir.toFile();
            Path libPath = libDir.resolve(resource + "." + suffix);
            File libFile = libPath.toFile();

            URL jarURL = NativeECLibrary.class.getResource(LIB_RESOURCE_DIR + resource + "." + suffix);
            if (jarURL == null) {
                return false;
            }
            URLConnection jarConnection = jarURL.openConnection();

            boolean write = false;
            if (libDirFile.isDirectory() && libFile.isFile()) {
                long jarModified = jarConnection.getLastModified();

                long libModified = Files.getLastModifiedTime(libPath).toMillis();
                if (jarModified > libModified) {
                    write = true;
                }
            } else {
                libDir.toFile().mkdirs();
                libFile.createNewFile();
                write = true;
            }

            if (write) {
                Files.copy(jarConnection.getInputStream(), libPath, StandardCopyOption.REPLACE_EXISTING);
            }
            jarConnection.getInputStream().close();

            for (String requirement : requriements) {
                System.loadLibrary(requirement);
            }

            System.load(libPath.toString());

            provider = createProvider();
            return super.initialize();
        } catch (IOException ignored) {

        }
        return false;
    }

    abstract Provider createProvider();
}