aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
blob: b0b9f37e5cb9ebc0a29f9590fe0f2909b0c1d319 (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
package cz.crcs.ectester.standalone.libs;

import cz.crcs.ectester.common.util.FileUtil;
import cz.crcs.ectester.standalone.ECTesterStandalone;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.Provider;
import java.util.Set;

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


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

    @Override
    public boolean initialize() {
        try {
            /* Determine what OS are we running on and use appropriate suffix and path. */
            String suffix = FileUtil.getLibSuffix();

            /* Resolve and create the ECTester directories in appData. */
            Path libDir = FileUtil.getLibDir();
            Path libReqDir = FileUtil.getRequirementsDir();
            Path libPath = libDir.resolve(resource + "." + suffix);

            /* Write the shim. */
            FileUtil.writeNewer(ECTesterStandalone.LIB_RESOURCE_DIR + resource + "." + suffix, libPath);

            /* Load the requirements, if they are bundled, write them in and load them. */
            try {
                for (String requirement : requriements) {
                    if (requirement.endsWith(suffix)) {
                        /* The requirement is bundled, write it */
                        Path reqPath = libReqDir.resolve(requirement);
                        FileUtil.writeNewer(ECTesterStandalone.LIB_RESOURCE_DIR + requirement, reqPath);
                        System.load(reqPath.toString());
                    } else {
                        System.loadLibrary(requirement);
                    }
                }
            } catch (UnsatisfiedLinkError ule) {
                return false;
            }

            System.load(libPath.toString());

            provider = createProvider();
            return super.initialize();
        } catch (IOException | UnsatisfiedLinkError ex) {
            System.err.println(ex.getMessage());
        }
        return false;
    }


    @Override
    public native Set<String> getNativeTimingSupport();

    @Override
    public native boolean setNativeTimingType(String type);

    @Override
    public native long getNativeTimingResolution();

    @Override
    public native String getNativeTimingUnit();

    @Override
    public native long getLastNativeTiming();

    abstract Provider createProvider();
}