增加source.yml配置

This commit is contained in:
redkale
2024-08-15 12:02:32 +08:00
parent 4876191e7a
commit 8c0b9a0f6a
2 changed files with 129 additions and 54 deletions

View File

@@ -29,6 +29,7 @@ import static org.redkale.util.RedkaleClassLoader.putReflectionClass;
import static org.redkale.util.RedkaleClassLoader.putReflectionPublicConstructors; import static org.redkale.util.RedkaleClassLoader.putReflectionPublicConstructors;
import org.redkale.util.RedkaleException; import org.redkale.util.RedkaleException;
import org.redkale.util.Utility; import org.redkale.util.Utility;
import org.redkale.util.YamlReader;
/** /**
* 加载系统参数配置 * 加载系统参数配置
@@ -246,15 +247,29 @@ class AppConfig {
if ("file".equals(this.confDir.getScheme())) { if ("file".equals(this.confDir.getScheme())) {
File sourceFile = new File(new File(confDir), "source.properties"); File sourceFile = new File(new File(confDir), "source.properties");
if (sourceFile.isFile() && sourceFile.canRead()) { if (sourceFile.isFile() && sourceFile.canRead()) {
Properties props = new Properties();
try { try {
InputStream in = new FileInputStream(sourceFile); InputStream in = new FileInputStream(sourceFile);
Properties props = new Properties();
props.load(in); props.load(in);
in.close(); in.close();
this.localEnvProperties.putAll(props);
} catch (IOException e) { } catch (IOException e) {
throw new RedkaleException(e); throw new RedkaleException(e);
} }
} else {
sourceFile = new File(new File(confDir), "source.yml");
if (!sourceFile.isFile() || !sourceFile.canRead()) {
sourceFile = new File(new File(confDir), "source.yaml");
}
if (sourceFile.isFile() && sourceFile.canRead()) {
try {
InputStream in = new FileInputStream(sourceFile);
String content = Utility.readThenClose(in);
Properties props = new YamlReader(content).read().toProperties();
this.localEnvProperties.putAll(props); this.localEnvProperties.putAll(props);
} catch (IOException e) {
throw new RedkaleException(e);
}
} else { } else {
// 兼容 persistence.xml 【已废弃】 // 兼容 persistence.xml 【已废弃】
File persist = new File(new File(confDir), "persistence.xml"); File persist = new File(new File(confDir), "persistence.xml");
@@ -269,20 +284,37 @@ class AppConfig {
} }
} }
} }
}
} else { // 从url或jar文件中resources读取 } else { // 从url或jar文件中resources读取
Properties props = new Properties();
try { try {
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI( final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(
configFromCache ? null : this.confDir.toString(), "source.properties"); configFromCache ? null : this.confDir.toString(), "source.properties");
InputStream in = sourceURI.toURL().openStream(); InputStream in = sourceURI.toURL().openStream();
Properties props = new Properties();
props.load(in); props.load(in);
in.close(); in.close();
this.localEnvProperties.putAll(props);
} catch (Exception e) { } catch (Exception e) {
try {
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(
configFromCache ? null : this.confDir.toString(), "source.yml");
InputStream in = sourceURI.toURL().openStream();
String content = Utility.readThenClose(in);
props.putAll(new YamlReader(content).read().toProperties());
} catch (Exception e2) {
try {
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(
configFromCache ? null : this.confDir.toString(), "source.yaml");
InputStream in = sourceURI.toURL().openStream();
String content = Utility.readThenClose(in);
props.putAll(new YamlReader(content).read().toProperties());
} catch (Exception e3) {
// 没有文件 跳过 // 没有文件 跳过
} }
} }
} }
this.localEnvProperties.putAll(props);
}
}
/** 读取本地日志配置 */ /** 读取本地日志配置 */
private void initLogProperties() { private void initLogProperties() {
@@ -349,7 +381,10 @@ class AppConfig {
throw new IOException("Read application conf file (" + sysConfFile + ") error "); throw new IOException("Read application conf file (" + sysConfFile + ") error ");
} }
} }
return text.trim().startsWith("<") if (sysConfFile.endsWith(".yml") || sysConfFile.endsWith(".yaml")) {
return AnyValue.loadFromYaml(text).getAnyValue("redkale");
}
return sysConfFile.endsWith(".xml")
? AnyValue.loadFromXml(text, (k, v) -> v.replace("${" + RESNAME_APP_HOME + "}", home)) ? AnyValue.loadFromXml(text, (k, v) -> v.replace("${" + RESNAME_APP_HOME + "}", home))
.getAnyValue("application") .getAnyValue("application")
: AnyValue.loadFromProperties(text).getAnyValue("redkale"); : AnyValue.loadFromProperties(text).getAnyValue("redkale");
@@ -357,18 +392,25 @@ class AppConfig {
String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf"); String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf");
URI appConfFile; URI appConfFile;
boolean fromCache = false; boolean fromCache = false;
boolean yml = false; boolean yaml = false;
if (confDir.contains("://")) { // jar内部资源 if (confDir.contains("://")) { // jar内部资源
appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.xml"); appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.xml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
} catch (IOException e) { // 没有application.xml就尝试读application.yml } catch (IOException e) { // 没有application.xml就尝试读application.yaml
appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.yml"); appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.yml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
yml = true; yaml = true;
} catch (IOException e2) { // 没有application.xml就尝试读application.properties } catch (IOException e2) { // 没有application.yml就尝试读application.yaml
appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.properties"); appConfFile = URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.yaml");
try {
appConfFile.toURL().openStream().close();
yaml = true;
} catch (IOException e3) { // 没有application.yaml就尝试读application.properties
appConfFile =
URI.create(confDir + (confDir.endsWith("/") ? "" : "/") + "application.properties");
}
} }
} }
} else if (confDir.charAt(0) == '/' || confDir.indexOf(':') > 0) { // 绝对路径 } else if (confDir.charAt(0) == '/' || confDir.indexOf(':') > 0) { // 绝对路径
@@ -381,34 +423,50 @@ class AppConfig {
if (f.isFile() && f.canRead()) { if (f.isFile() && f.canRead()) {
appConfFile = f.toURI(); appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath(); confDir = f.getParentFile().getCanonicalPath();
yml = true; yaml = true;
} else {
f = new File(confDir, "application.yaml");
if (f.isFile() && f.canRead()) {
appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath();
yaml = true;
} else { } else {
f = new File(confDir, "application.properties"); f = new File(confDir, "application.properties");
if (f.isFile() && f.canRead()) { if (f.isFile() && f.canRead()) {
appConfFile = f.toURI(); appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath(); confDir = f.getParentFile().getCanonicalPath();
} else { } else {
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.xml"); // 不能传confDir // 不能传confDir
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.xml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
} catch (IOException e) { // 没有application.xml就尝试读application.yml } catch (IOException e) { // 没有application.xml就尝试读application.yml
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yml"); appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
yml = true; yaml = true;
} catch (IOException e2) { // 没有application.xml就尝试读application.properties } catch (IOException e2) { // 没有application.yml就尝试读application.yaml
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.properties"); appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yaml");
try {
appConfFile.toURL().openStream().close();
yaml = true;
} catch (IOException e3) { // 没有application.yaml就尝试读application.properties
appConfFile =
RedkaleClassLoader.getConfResourceAsURI(null, "application.properties");
}
} }
} }
confDir = appConfFile confDir = appConfFile
.toString() .toString()
.replace("/application.xml", "") .replace("/application.xml", "")
.replace("/application.yml", "") .replace("/application.yml", "")
.replace("/application.yaml", "")
.replace("/application.properties", ""); .replace("/application.properties", "");
fromCache = true; fromCache = true;
} }
} }
} }
}
} else { // 相对路径 } else { // 相对路径
File f = new File(new File(home, confDir), "application.xml"); File f = new File(new File(home, confDir), "application.xml");
if (f.isFile() && f.canRead()) { if (f.isFile() && f.canRead()) {
@@ -419,40 +477,56 @@ class AppConfig {
if (f.isFile() && f.canRead()) { if (f.isFile() && f.canRead()) {
appConfFile = f.toURI(); appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath(); confDir = f.getParentFile().getCanonicalPath();
yml = true; yaml = true;
} else {
f = new File(new File(home, confDir), "application.yaml");
if (f.isFile() && f.canRead()) {
appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath();
yaml = true;
} else { } else {
f = new File(new File(home, confDir), "application.properties"); f = new File(new File(home, confDir), "application.properties");
if (f.isFile() && f.canRead()) { if (f.isFile() && f.canRead()) {
appConfFile = f.toURI(); appConfFile = f.toURI();
confDir = f.getParentFile().getCanonicalPath(); confDir = f.getParentFile().getCanonicalPath();
} else { } else {
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.xml"); // 不能传confDir // 不能传confDir
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.xml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
} catch (IOException e) { // 没有application.xml就尝试读application.yml } catch (IOException e) { // 没有application.xml就尝试读application.yaml
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yml"); appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yml");
try { try {
appConfFile.toURL().openStream().close(); appConfFile.toURL().openStream().close();
yml = true; yaml = true;
} catch (IOException e2) { // 没有application.xml就尝试读application.properties } catch (IOException e2) { // 没有application.yml就尝试读application.yaml
appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.properties"); appConfFile = RedkaleClassLoader.getConfResourceAsURI(null, "application.yaml");
try {
appConfFile.toURL().openStream().close();
yaml = true;
} catch (IOException e3) { // 没有application.yaml就尝试读application.properties
appConfFile =
RedkaleClassLoader.getConfResourceAsURI(null, "application.properties");
}
} }
} }
confDir = appConfFile confDir = appConfFile
.toString() .toString()
.replace("/application.xml", "") .replace("/application.xml", "")
.replace("/application.yml", "") .replace("/application.yml", "")
.replace("/application.yaml", "")
.replace("/application.properties", ""); .replace("/application.properties", "");
fromCache = true; fromCache = true;
} }
} }
} }
} }
}
System.setProperty(RESNAME_APP_CONF_DIR, confDir); System.setProperty(RESNAME_APP_CONF_DIR, confDir);
String text = Utility.readThenClose(appConfFile.toURL().openStream()); String text = Utility.readThenClose(appConfFile.toURL().openStream());
AnyValue conf; AnyValue conf;
if (yml) { if (yaml) {
conf = AnyValue.loadFromYml(text).getAnyValue("redkale"); conf = AnyValue.loadFromYaml(text).getAnyValue("redkale");
} else if (text.trim().startsWith("<")) { } else if (text.trim().startsWith("<")) {
conf = AnyValue.loadFromXml(text, (k, v) -> v.replace("${APP_HOME}", home)) conf = AnyValue.loadFromXml(text, (k, v) -> v.replace("${APP_HOME}", home))
.getAnyValue("application"); .getAnyValue("application");

View File

@@ -222,13 +222,13 @@ public abstract class AnyValue {
} }
/** /**
* yml内容流转换成AnyValue对象 * yaml内容流转换成AnyValue对象
* *
* *
* @param text 文本内容 * @param text 文本内容
* @return AnyValue * @return AnyValue
*/ */
public static AnyValue loadFromYml(String text) { public static AnyValue loadFromYaml(String text) {
return new YamlReader(text).read(); return new YamlReader(text).read();
} }
@@ -360,6 +360,7 @@ public abstract class AnyValue {
try { try {
realIndex = Integer.parseInt(keyOrIndex); realIndex = Integer.parseInt(keyOrIndex);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// do nothing
} }
} }
if (realIndex >= 0) { // 数组结构 if (realIndex >= 0) { // 数组结构