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 94 95 96 97 98 99 100 101 102 103 104 105
| import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.*;
public class NativeLibLoader { private static final Logger logger = LoggerFactory.getLogger(NativeLibLoader.class);
public static void load(String[] snks) { String currentOS = System.getProperty("os.name"); if (currentOS.contains("Windows")) { linuxPrefix(new String[]{"dir"}); for (String snk : snks) { if (StrUtil.endWithIgnoreCase(snk, "dll")) { loadFile(snk, snk); } } } else if (currentOS.contains("Linux")) { String linuxPrefix = linuxPrefix(new String[]{"/bin/bash", "-c", "cat /etc/*-release"}); for (String snk : snks) { if (StrUtil.endWithIgnoreCase(snk, "so")) { loadFile(linuxPrefix + "-" + snk, snk); } } } else if (currentOS.contains("Mac OS X")) { linuxPrefix(new String[]{"ls"}); for (String snk : snks) { if (StrUtil.endWithIgnoreCase(snk, "jnilib") || StrUtil.endWithIgnoreCase(snk, "dylib")) { loadFile(snk, snk); } } } else { logger.info("NativeLibLoader : not supported " + currentOS); } }
private static String linuxPrefix(String[] args) { StringBuilder sbRead = new StringBuilder(); StringBuilder sbErr = new StringBuilder(); try { Process pro = Runtime.getRuntime().exec(args); pro.waitFor(); try (BufferedReader read = new BufferedReader(new InputStreamReader(pro.getInputStream())); BufferedReader err = new BufferedReader(new InputStreamReader(pro.getErrorStream()))) { String line; while ((line = read.readLine()) != null) { logger.info(line); sbRead.append(line); }
while ((line = err.readLine()) != null) { logger.error(line); sbErr.append(line); } } } catch (Exception e) { e.printStackTrace(); }
String releaseInfo = sbRead.toString(); if (releaseInfo.contains("Debian")) { return "debian"; } return "centos"; }
public static void loadFile(String src, String snk) { String srcAndPath = "classpath:" + src; try { logger.info("NativeLibLoader : copy " + srcAndPath + " to " + snk); File file = copyResourceToTempDirFile(srcAndPath, snk); String filePath = file.getAbsolutePath(); System.load(filePath); logger.info("NativeLibLoader : load " + filePath + " successful"); } catch (IOException e) { e.printStackTrace(); } }
private static File copyResourceToTempDirFile(String src, String snk) throws IOException { File tempDir = new File(System.getProperty("java.io.tmpdir")); File tempDirFile = new File(tempDir, snk);
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = patternResolver.getResources(src);
if (resources.length == 0) { return null; }
try (InputStream input = resources[0].getInputStream(); OutputStream output = new FileOutputStream(tempDirFile)) { IoUtil.copy(input, output); tempDirFile.deleteOnExit(); return tempDirFile; } catch (Exception e) { throw new RuntimeException(e); } } }
|