package com.hbbh.adapter.app;
|
|
|
import com.google.common.collect.Lists;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* 设置 实时数据库环境程序
|
*/
|
public class RTDBPath {
|
|
|
public static void main(String[] args) {
|
printLibrary();
|
}
|
|
private static final String key = "java.library.path";
|
//Windows
|
private static final String regex_win = ";";
|
// mac
|
private static final String regex_mac = ":";
|
|
private static final String _mac = "mac";
|
private static final String _windows = "windows";
|
|
private static final String os_name = "os.name";
|
|
|
public static void printLibrary() {
|
String os = chooseOS();
|
setSystemPath(os);
|
|
String osName = System.getProperty(os_name).toLowerCase();
|
|
System.out.println("当前运行操作系统:" + osName);
|
System.out.println("-------------- 引入第三方包环境变量之前 --- Before -------------- ");
|
String propertyBefore = System.getProperty(key);
|
if (propertyBefore.contains(regex_win)) {
|
String[] split = propertyBefore.split(regex_win);
|
for (String s : split) {
|
System.out.println(s);
|
}
|
} else {
|
System.out.println(propertyBefore);
|
}
|
System.out.println("------------------------------------------ ");
|
}
|
|
//需要在 resources 下创建 depend 文件夹 将实时库依赖包放进去
|
private static String dependPath = Thread.currentThread().getContextClassLoader().getResource("depend").getPath();
|
|
private static void setSystemPath(String os) {
|
String propertyBefore = System.getProperty(key);
|
|
switch (os) {
|
case _windows:
|
String[] splitWin = propertyBefore.split(regex_win);
|
List<String> wins = Arrays.asList(splitWin);
|
List<String> winList = Lists.newArrayList(wins);
|
winList.add(dependPath);
|
spliceWin(wins);
|
break;
|
case _mac:
|
String[] splitMac = propertyBefore.split(regex_mac);
|
List<String> macs = Arrays.asList(splitMac);
|
List<String> macList = Lists.newArrayList(macs);
|
macList.add(dependPath);
|
spliceMac(macList);
|
break;
|
default:
|
throw new RuntimeException("当前操作系统不识别");
|
}
|
}
|
|
private static void spliceMac(List<String> macList) {
|
StringBuilder sb=new StringBuilder();
|
for (int i = 0; i < macList.size(); i++) {
|
if (i==(macList.size()-1)){
|
sb.append(macList.get(i));
|
}else {
|
sb.append(macList.get(i));
|
sb.append(regex_mac);
|
}
|
}
|
System.setProperty(key, sb.toString());
|
}
|
|
private static void spliceWin(List<String> winList) {
|
StringBuilder sb=new StringBuilder();
|
for (int i = 0; i < winList.size(); i++) {
|
if (i==(winList.size()-1)){
|
sb.append(winList.get(i));
|
}else {
|
sb.append(winList.get(i));
|
sb.append(regex_win);
|
}
|
}
|
System.setProperty(key, sb.toString());
|
}
|
|
private static String chooseOS() {
|
String osName = System.getProperty(os_name).toLowerCase();
|
if (osName.contains(_mac)) {
|
//throw new RuntimeException("警告:当前程序必须在Windows环境下执行");
|
} else if (osName.contains(_windows)) {
|
return _windows;
|
} else {
|
throw new RuntimeException("当前操作系统不识别");
|
}
|
|
throw new RuntimeException("当前操作系统不识别");
|
}
|
}
|