JNIApp.java
3.6 KB
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
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);
}
}
}