JNIApp.java 3.6 KB
package com.viontech.keliu;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import sun.security.action.GetPropertyAction;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;

import static java.security.AccessController.doPrivileged;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/3/22
 */

@SpringBootApplication
@Slf4j
public class JNIApp implements CommandLineRunner {

    public static void main(String[] args) {
        try {
            SpringApplication.run(JNIApp.class, args);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    @Override
    public void run(String... args) throws IOException {
        log.info("系统:{}", System.getProperty("os.name"));
        Resource[] resources = null;
        Resource[] preResources = null;
        if (System.getProperty("os.name").toLowerCase().contains("linux")) {
            resources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/so/*");
            preResources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/preso/*");
        } else if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            resources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/dll/*");
            preResources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/predll/*");
        }

        HashSet<String> preResourceNames = new HashSet<>();
        if (preResources != null) {
            for (int i = 0; preResourceNames.size() != preResources.length; i++) {
                if (i >= preResources.length) {
                    i = 0;
                }
                Resource re = preResources[i];
                if (preResourceNames.contains(re.getFilename())) {
                    continue;
                }
                try {
                    loadResource(re);
                    preResourceNames.add(re.getFilename());
                } catch (UnsatisfiedLinkError ignore) {
                }
            }
        }
        log.info("==================================================PRE LOAD FINISH==================================================");
        if (resources != null) {
            for (Resource resource : resources) {
                loadResource(resource);
            }
        }
    }

    private void loadResource(Resource resource) {
        try {
            String filename = resource.getFilename();
            if (filename == null) {
                return;
            }

            File file = new File(Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir"))) + "/" + filename);
            if (!file.exists()) {
                file.createNewFile();
            }

            Files.copy(resource.getInputStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            System.load(file.getAbsolutePath());
            log.info("=====load===== [{}]", resource.getFilename());
            file.deleteOnExit();
        } catch (Exception e) {
            log.error("", e);
        }
    }
}