You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
7.5 KiB
Java

package com.nmgs;
import com.nmgs.config.Constant;
import com.nmgs.util.PathUtil;
import com.nmgs.util.PropertiesUtil;
import com.trkf.PasswordEncryption.PassWordUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import java.io.File;
import java.io.FileInputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@Configuration
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
System.out.println("OverLimitDetection读取公用配置文件");
//公用配置文件路径
String path = PathUtil.applicationPath;
//本项目配置文件路径
String NewPath = PathUtil.projectApplicationPath;
System.out.println("Loading local settings from : " + path);
System.out.println("Loading local settings from : " + NewPath);
Properties properties = new Properties();
Properties Newproperties = new Properties();
try {
File fileproperties = new File(path);
File fileNewproperties = new File(NewPath);
// 判断文件夹是否存在
if (!fileproperties.exists()) {
fileproperties.createNewFile();
}
if (!fileNewproperties.exists()) {
fileNewproperties.createNewFile();
}
properties.load(new FileInputStream(path));
String DBType = properties.getProperty("DBType") == null ? "1" : properties.getProperty("DBType");
Newproperties.load(new FileInputStream(NewPath));
//获取是否是goldenDB数据库
String IsGolDen = properties.getProperty("IsGolDen") == null ? "0" : properties.getProperty("IsGolDen");
properties.setProperty("spring.logback.logPath", PathUtil.TomcatPath + "/logs");
properties.setProperty("spring.datasource.url", properties.getProperty("DBUrl"));
properties.setProperty("spring.datasource.username", properties.getProperty("DBUserName"));
properties.setProperty("spring.datasource.password", PassWordUtils.decrypt(properties.getProperty("DBPassWord")));
if(Constant.STR_ONE.equals(IsGolDen)){
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("GoldenDBClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("GoldenvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("GoldenvalidationQuery"));
}else {
switch (DBType) {
case "1":
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("sqlClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("sqlvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("sqlvalidationQuery"));
break;
case "2":
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("OracleClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("OraclevalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("OraclevalidationQuery"));
break;
case "3":
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("DMClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("DMvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("DMvalidationQuery"));
break;
case "4":
properties.setProperty("spring.datasource.driver-class-name", Newproperties.getProperty("MySQLClassName"));
properties.setProperty("spring.datasource.validationQuery", Newproperties.getProperty("MySQLvalidationQuery"));
properties.setProperty("spring.datasource.hikari.connection-test-query", Newproperties.getProperty("MySQLvalidationQuery"));
break;
}
}
System.out.println("---------------------------OverLimitDetection本次载入数据库----------------------");
System.out.println("server.port: " + properties.getProperty("server.port"));
System.out.println(" url : " + properties.getProperty("spring.datasource.url"));
System.out.println(" username : " + properties.getProperty("spring.datasource.username"));
System.out.println(" password : " + properties.getProperty("spring.datasource.password"));
String appName=Newproperties.getProperty("spring.logback.appName");
String versionPath =PathUtil.versionPath;
File resourceDir = new File(versionPath);
String version = getVersionFileName(resourceDir);
new PropertiesUtil(PathUtil.applicationTextPath).set(appName+"Version",version);
System.out.println(" version : "+appName+"--->"+version);
//环境名称随意取,但尽量不能和其他环境名称相同,避免不生效
PropertiesPropertySource propertySource = new PropertiesPropertySource("environmentPostProcessor", properties);
PropertiesPropertySource newPropertySource = new PropertiesPropertySource("newEnvironmentPostProcessor", Newproperties);
//外部的文件是最先导入的
configurableEnvironment.getPropertySources().addFirst(propertySource);
//如果公用配置文件没有配置的则使用本项目的配置文件
configurableEnvironment.getPropertySources().addLast(newPropertySource);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws UnknownHostException {
String s = "123456";
String decrypt = PassWordUtils.encrypt(s);
System.out.println(decrypt);
String decrypt2 = PassWordUtils.decrypt("LihnYTkrOClRNXA/Zz1XLkhgMFdWRXVJ");
System.out.println(decrypt2);
}
private static String getVersionFileName(File dir) {
String version="";
List<String> fileNames = new ArrayList<>();
if (dir == null || !dir.exists() || !dir.isDirectory()) {
return version;
}
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
fileNames.add(file.getName());
}
}
}
if (!fileNames.isEmpty()) {
version = fileNames.get(0).replace(".txt", "");
}
return version;
}
}