|
|
@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
@@ -23,13 +25,11 @@ public class MasterManager {
|
|
|
*/
|
|
|
private static Set<String> masters = new HashSet<>();
|
|
|
|
|
|
- private static final String DATACONFIG = "{\"UAS_DEV\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@192.168.253.6:1521:orcl\",\"username\":\"UAS_DEV\"},\"UAS\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@192.168.253.6:1521:orcl\",\"username\":\"UAS\"},\"XKN_TEST\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@127.0.0.1:1521:orcl\",\"username\":\"XKN_TEST\"}}";
|
|
|
private static Logger logger = LoggerFactory.getLogger(MasterManager.class);
|
|
|
|
|
|
static {
|
|
|
init();
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 初始化所有数据源
|
|
|
*/
|
|
|
@@ -37,33 +37,36 @@ public class MasterManager {
|
|
|
clear();
|
|
|
logger.info("数据源初始化......");
|
|
|
// 从配置中获取数据源信息
|
|
|
- /* File propertyFile = new File("application.properties");
|
|
|
- if (!propertyFile.exists()) {
|
|
|
+ File propertyFile = new File("application.properties");
|
|
|
+ Properties properties = new Properties();
|
|
|
+ if (!propertyFile.exists() || !propertyFile.isFile()) {
|
|
|
try {
|
|
|
- logger.warn("配置文件再次读取......");
|
|
|
- propertyFile = BaseUtil.getFile("application.properties");
|
|
|
- if (!propertyFile.exists() && !propertyFile.isFile()) {
|
|
|
- BaseUtil.showError("读取配置文件失败");
|
|
|
+ logger.info("配置文件再次读取......");
|
|
|
+ // 是否以 spring boot jar 形式运行
|
|
|
+ URL location = MasterManager.class.getProtectionDomain().getCodeSource().getLocation();
|
|
|
+ if (org.springframework.util.ResourceUtils.isJarURL(location)) {
|
|
|
+ // 以 spring boot jar 形式运行时,不能直接复制默认配置文件,需以输入流的方式读取默认配置
|
|
|
+ InputStream inputStream = MasterManager.class.getResourceAsStream("/" + "application.properties");
|
|
|
+ byte[] data = new byte[inputStream.available()];
|
|
|
+ inputStream.read(data);
|
|
|
+ logger.info("配置文件路径:"+ propertyFile.getPath());
|
|
|
+ write(propertyFile.getPath(), data, false);
|
|
|
+ logger.info("读取成功......");
|
|
|
+ }else{
|
|
|
+ logger.info("非jar包运行......");
|
|
|
+ propertyFile = BaseUtil.getFile("application.properties");
|
|
|
}
|
|
|
+ properties.load(new FileInputStream(propertyFile));
|
|
|
}catch (Exception e){
|
|
|
- logger.warn("配置文件读取失败......");
|
|
|
e.printStackTrace();
|
|
|
+ BaseUtil.showError("读取配置文件失败");
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- Properties properties = new Properties();
|
|
|
- try {
|
|
|
- properties.load(new FileInputStream(propertyFile));
|
|
|
- } catch (IOException e) {
|
|
|
- BaseUtil.showError("读取配置文件失败");
|
|
|
- e.printStackTrace();
|
|
|
- }*/
|
|
|
- // String datasourceConfig = properties.getProperty("datasourceConfig");
|
|
|
- // String datasourceConfig = "{\"UAS_DEV\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@192.168.253.6:1521:orcl\",\"username\":\"UAS_DEV\"},\"UAS\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@192.168.253.6:1521:orcl\",\"username\":\"UAS\"},\"XKN_TEST\":{\"password\":\"select!#%*(\",\"driverClassName\":\"oracle.jdbc.driver.OracleDriver\",\"url\":\"jdbc:oracle:thin:@127.0.0.1:1521:orcl\",\"username\":\"XKN_TEST\"}}";
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(DATACONFIG);
|
|
|
+ String datasourceConfig = properties.getProperty("datasourceConfig");
|
|
|
+ logger.info("datasourceConfig:" + datasourceConfig);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(datasourceConfig);
|
|
|
Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
|
|
|
for (Map.Entry<String, Object> entry : entrySet) {
|
|
|
- JSONObject object = JSONObject.parseObject(entry.getValue().toString());
|
|
|
DruidDataSource dataSource = JSONObject.parseObject(entry.getValue().toString(), DruidDataSource.class);
|
|
|
dataSources.put(entry.getKey().toUpperCase(), dataSource);
|
|
|
masters.add(entry.getKey().toUpperCase());
|
|
|
@@ -106,4 +109,76 @@ public class MasterManager {
|
|
|
}
|
|
|
return dataSource;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入文件
|
|
|
+ *
|
|
|
+ * @param filePath
|
|
|
+ * 文件路径
|
|
|
+ * @param data
|
|
|
+ * 数据
|
|
|
+ * @param allowNoData
|
|
|
+ * 是否允许数据为空(文件无内容)
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void write(String filePath, byte[] data, boolean allowNoData) throws IOException {
|
|
|
+ if (StringUtils.isEmpty(filePath)) {
|
|
|
+ throw new IllegalArgumentException("参数不能为空:filePath");
|
|
|
+ }
|
|
|
+ if (!allowNoData && StringUtils.isEmpty(data)) {
|
|
|
+ throw new IllegalArgumentException("参数不能为空:data");
|
|
|
+ }
|
|
|
+
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (file.getParentFile() != null && !file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
+ fos.write(data);
|
|
|
+ fos.flush();
|
|
|
+ logger.info("Writed... " + file.getPath());
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /* *//**
|
|
|
+ * 复制文件(夹)
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * 源文件(夹)
|
|
|
+ * @param dest
|
|
|
+ * 目的文件(夹)
|
|
|
+ * @throws IOException
|
|
|
+ *//*
|
|
|
+ public static void copy(File src, File dest) throws IOException {
|
|
|
+ if (src.equals(dest)) {
|
|
|
+ throw new IOException("不可置于相同路径下");
|
|
|
+ }
|
|
|
+ if (!src.isFile() && !dest.isFile() && isSub(src, dest)) {
|
|
|
+ throw new IOException("不可置于子路径下");
|
|
|
+ }
|
|
|
+ if (src.isFile()) {
|
|
|
+ if (dest.exists() && dest.isDirectory()) {
|
|
|
+ dest = new File(dest, src.getName());
|
|
|
+ copy(src, dest);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ReadableByteChannel in = Channels.newChannel(new FileInputStream(src));
|
|
|
+ WritableByteChannel out = Channels.newChannel(new FileOutputStream(dest));
|
|
|
+ copyChannel(in, out);
|
|
|
+ in.close();
|
|
|
+ out.close();
|
|
|
+ } else {
|
|
|
+ if (dest.exists() && dest.isFile()) {
|
|
|
+ throw new IOException("不可将文件夹置于文件下");
|
|
|
+ }
|
|
|
+ if (!dest.exists()) {
|
|
|
+ dest.mkdirs();
|
|
|
+ }
|
|
|
+ File[] files = src.listFiles();
|
|
|
+ for (File file : files) {
|
|
|
+ copy(file, new File(dest, file.getName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
}
|