From 1b6a23c1a9f8a2fedd8e9b90b44ee7a6ba46b444 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 22 Jun 2018 16:38:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0APP=5FCONF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E5=9C=A8=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E4=B8=AD=E5=8F=AF=E4=BB=A5=E6=8C=87=E5=AE=9A=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/boot/ApiDocsService.java | 2 +- src/org/redkale/boot/Application.java | 44 +++++++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/org/redkale/boot/ApiDocsService.java b/src/org/redkale/boot/ApiDocsService.java index 612218cbc..ce213ce0b 100644 --- a/src/org/redkale/boot/ApiDocsService.java +++ b/src/org/redkale/boot/ApiDocsService.java @@ -192,7 +192,7 @@ public final class ApiDocsService { final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "apidoc.json")); out.write(json.getBytes("UTF-8")); out.close(); - File doctemplate = new File(app.getHome(), "conf/apidoc-template.html"); + File doctemplate = new File(app.getConf(), "apidoc-template.html"); InputStream in = null; if (doctemplate.isFile() && doctemplate.canRead()) { in = new FileInputStream(doctemplate); diff --git a/src/org/redkale/boot/Application.java b/src/org/redkale/boot/Application.java index 577a72822..a9933ff3b 100644 --- a/src/org/redkale/boot/Application.java +++ b/src/org/redkale/boot/Application.java @@ -61,6 +61,11 @@ public final class Application { */ public static final String RESNAME_APP_HOME = "APP_HOME"; + /** + * 当前进程的配置目录,如果不是绝对路径则视为HOME目录下的相对路径 类型:String、File、Path + */ + public static final String RESNAME_APP_CONF = "APP_CONF"; + /** * application.xml 文件中resources节点的内容, 类型: AnyValue */ @@ -137,6 +142,9 @@ public final class Application { //进程根目录 private final File home; + //配置文件目录 + private final File conf; + //日志 private final Logger logger; @@ -170,6 +178,12 @@ public final class Application { try { this.resourceFactory.register(RESNAME_APP_HOME, root.getCanonicalPath()); this.home = root.getCanonicalFile(); + String confsubpath = System.getProperty(RESNAME_APP_CONF, "conf"); + if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) { + this.conf = new File(confsubpath).getCanonicalFile(); + } else { + this.conf = new File(this.home, confsubpath).getCanonicalFile(); + } } catch (IOException e) { throw new RuntimeException(e); } @@ -194,7 +208,7 @@ public final class Application { System.setProperty(RESNAME_APP_NODE, node); } //以下是初始化日志配置 - final File logconf = new File(root, "conf/logging.properties"); + final File logconf = new File(conf, "logging.properties"); if (logconf.isFile() && logconf.canRead()) { try { final String rootpath = root.getCanonicalPath().replace('\\', '/'); @@ -352,6 +366,10 @@ public final class Application { return home; } + public File getConf() { + return conf; + } + public long getStartTime() { return startTime; } @@ -373,12 +391,13 @@ public final class Application { System.setProperty("convert.bson.writer.buffer.defsize", "4096"); System.setProperty("convert.json.writer.buffer.defsize", "4096"); - File persist = new File(this.home, "conf/persistence.xml"); + File persist = new File(this.conf, "persistence.xml"); final String homepath = this.home.getCanonicalPath(); + final String confpath = this.conf.getCanonicalPath(); if (persist.isFile()) System.setProperty(DataSources.DATASOURCE_CONFPATH, persist.getCanonicalPath()); - logger.log(Level.INFO, "APP_JAVA = " + System.getProperty("java.version") + "\r\n" + RESNAME_APP_ADDR + " = " + this.localAddress.getHostAddress() + "\r\n" + RESNAME_APP_HOME + " = " + homepath); + logger.log(Level.INFO, "APP_JAVA = " + System.getProperty("java.version") + "\r\n" + RESNAME_APP_ADDR + " = " + this.localAddress.getHostAddress() + "\r\n" + RESNAME_APP_HOME + " = " + homepath + "\r\n" + RESNAME_APP_CONF + " = " + confpath); String lib = config.getValue("lib", "${APP_HOME}/libs/*").trim().replace("${APP_HOME}", homepath); - lib = lib.isEmpty() ? (homepath + "/conf") : (lib + ";" + homepath + "/conf"); + lib = lib.isEmpty() ? confpath : (lib + ";" + confpath); Server.loadLib(classLoader, logger, lib); //------------------------------------------------------------------------ @@ -391,7 +410,7 @@ public final class Application { if (dfloads != null) { for (String dfload : dfloads.split(";")) { if (dfload.trim().isEmpty()) continue; - final File df = (dfload.indexOf('/') < 0) ? new File(home, "conf/" + dfload) : new File(dfload); + final File df = (dfload.indexOf('/') < 0) ? new File(conf, "/" + dfload) : new File(dfload); if (df.isFile()) { Properties ps = new Properties(); InputStream in = new FileInputStream(df); @@ -524,8 +543,8 @@ public final class Application { public void restoreConfig() throws IOException { synchronized (this) { - File confFile = new File(this.home, "conf/application.xml"); - confFile.renameTo(new File(this.home, "conf/application_" + String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", System.currentTimeMillis()) + ".xml")); + File confFile = new File(this.conf, "application.xml"); + confFile.renameTo(new File(this.conf, "application_" + String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", System.currentTimeMillis()) + ".xml")); final PrintStream ps = new PrintStream(new FileOutputStream(confFile)); ps.append(config.toXML("application")); ps.close(); @@ -796,8 +815,15 @@ public final class Application { public static Application create(final boolean singleton) throws IOException { final String home = new File(System.getProperty(RESNAME_APP_HOME, "")).getCanonicalPath().replace('\\', '/'); System.setProperty(RESNAME_APP_HOME, home); - File appfile = new File(home, "conf/application.xml"); - return new Application(singleton, load(new FileInputStream(appfile))); + String confsubpath = System.getProperty(RESNAME_APP_CONF, "conf"); + File appfile; + if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) { + appfile = new File(confsubpath).getCanonicalFile(); + } else { + appfile = new File(new File(home), confsubpath); + } + File appconf = new File(appfile, "application.xml"); + return new Application(singleton, load(new FileInputStream(appconf))); } public static void main(String[] args) throws Exception {