diff --git a/src/main/java/com/jfinal/server/undertow/PropExt.java b/src/main/java/com/jfinal/server/undertow/PropExt.java index c99d6e416a797d0ec6afd6c7478f5912c7ee0130..7a1ad1c63558c0f950311174ecae2ddbc1607a64 100644 --- a/src/main/java/com/jfinal/server/undertow/PropExt.java +++ b/src/main/java/com/jfinal/server/undertow/PropExt.java @@ -16,51 +16,57 @@ package com.jfinal.server.undertow; +import com.jfinal.core.Const; +import com.jfinal.server.undertow.hotswap.ClassLoaderKit; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; -import com.jfinal.core.Const; -import com.jfinal.server.undertow.hotswap.ClassLoaderKit; /** * PropExt - * + * * 支持 undertow 从 config 目录以及 jar 包中读取配置文件 */ public class PropExt { - + protected Properties properties = null; static ClassLoaderKit classLoaderKit = null; - + /** - * 避免从 UndertowConfig 中获取 class loader,从而避免触发 UndertowConfig 中的 + * 避免从 UndertowConfig 中获取 class loader,从而避免触发 UndertowConfig 中的 * getHotSwapResolver()、getClassLoaderKit(),进而避免创建 UndertowConfig * 中的 hotSwapResolver、classLoaderKit 属性对象,进而确保这些对象的创建要晚于配置 - * + * * 即便从 UndertowConfig 中获取 class loader,当前的创建时机并无上述影响,在此仅为避免 * 随着开发的推进,在未来可能引起配置丢失问题 */ private ClassLoader getClassLoader() { - if (classLoaderKit == null) { + if (classLoaderKit == null) { classLoaderKit = new ClassLoaderKit(PropExt.class.getClassLoader(), null); } return classLoaderKit.getClassLoader(); } - + /* private ClassLoader getClassLoader() { ClassLoader ret = Thread.currentThread().getContextClassLoader(); return ret != null ? ret : getClass().getClassLoader(); }*/ - + /** * 支持 new PropExt().appendIfExists(...); */ public PropExt() { properties = new Properties(); } - + + + public PropExt(Properties properties) { + this.properties = properties; + } + /** * PropExt constructor. * @see #PropExt(String, String) @@ -68,17 +74,17 @@ public class PropExt { public PropExt(String fileName) { this(fileName, Const.DEFAULT_ENCODING); } - + /** * PropExt constructor *

* Example:
* PropExt prop = new PropExt("my_config.txt", "UTF-8");
* String userName = prop.get("userName");

- * + * * prop = new PropExt("com/jfinal/file_in_sub_path_of_classpath.txt", "UTF-8");
* String value = prop.get("key"); - * + * * @param fileName the properties file's name in classpath or the sub directory of classpath * @param encoding the encoding */ @@ -98,7 +104,7 @@ public class PropExt { if (inputStream != null) try {inputStream.close();} catch (IOException e) {UndertowKit.doNothing(e);} } } - + public PropExt append(PropExt prop) { if (prop == null) { throw new IllegalArgumentException("prop can not be null"); @@ -106,15 +112,15 @@ public class PropExt { properties.putAll(prop.getProperties()); return this; } - + public PropExt append(String fileName, String encoding) { return append(new PropExt(fileName, encoding)); } - + public PropExt append(String fileName) { return append(fileName, Const.DEFAULT_ENCODING); } - + public PropExt appendIfExists(String fileName, String encoding) { try { return append(new PropExt(fileName, encoding)); @@ -122,23 +128,23 @@ public class PropExt { return this; } } - + public PropExt appendIfExists(String fileName) { return appendIfExists(fileName, Const.DEFAULT_ENCODING); } - + public String get(String key) { return properties.getProperty(key); } - + public String get(String key, String defaultValue) { return properties.getProperty(key, defaultValue); } - + public Integer getInt(String key) { return getInt(key, null); } - + public Integer getInt(String key, Integer defaultValue) { String value = properties.getProperty(key); if (value != null) { @@ -146,11 +152,11 @@ public class PropExt { } return defaultValue; } - + public Long getLong(String key) { return getLong(key, null); } - + public Long getLong(String key, Long defaultValue) { String value = properties.getProperty(key); if (value != null) { @@ -158,11 +164,11 @@ public class PropExt { } return defaultValue; } - + public Boolean getBoolean(String key) { return getBoolean(key, null); } - + public Boolean getBoolean(String key, Boolean defaultValue) { String value = properties.getProperty(key); if (value != null) { @@ -176,31 +182,31 @@ public class PropExt { } return defaultValue; } - + public boolean containsKey(String key) { return properties.containsKey(key); } - + public boolean isEmpty() { return properties.isEmpty(); } - + public boolean notEmpty() { return ! properties.isEmpty(); } - + public Properties getProperties() { return properties; } - + // --------- - + @SuppressWarnings("serial") public static class FileNotFoundException extends RuntimeException { public FileNotFoundException(String msg) { super(msg); } - + @Override public Throwable fillInStackTrace() { return this; diff --git a/src/main/java/com/jfinal/server/undertow/UndertowConfig.java b/src/main/java/com/jfinal/server/undertow/UndertowConfig.java index b0583b59d37504f5a80a2c932adac4e7aa83a0d1..887e1cbe69066848fb0fee225d00f4b7399508d1 100644 --- a/src/main/java/com/jfinal/server/undertow/UndertowConfig.java +++ b/src/main/java/com/jfinal/server/undertow/UndertowConfig.java @@ -16,20 +16,20 @@ package com.jfinal.server.undertow; -import java.util.zip.Deflater; import com.jfinal.server.undertow.hotswap.ClassLoaderKit; import com.jfinal.server.undertow.hotswap.HotSwapResolver; import com.jfinal.server.undertow.ssl.SslConfig; import io.undertow.Undertow; import io.undertow.server.handlers.resource.ResourceManager; +import java.util.zip.Deflater; + /** * UndertowConfig */ public class UndertowConfig { static final String UNDERTOW_CONFIG = "undertow.txt"; - static final String UNDERTOW_CONFIG_PRO = "undertow-pro.txt"; static final String DEV_MODE = "undertow.devMode"; static final String PORT = "undertow.port"; @@ -101,10 +101,8 @@ public class UndertowConfig { public UndertowConfig(String jfinalConfigClass) { this.jfinalConfig = jfinalConfigClass; - - p = new PropExt() - .appendIfExists(UNDERTOW_CONFIG) - .appendIfExists(UNDERTOW_CONFIG_PRO); + + p = createPropExt(UNDERTOW_CONFIG); if (p.notEmpty()) { init(); @@ -128,14 +126,20 @@ public class UndertowConfig { this.jfinalConfig = jfinalConfigClass; undertowConfig = undertowConfig.trim(); - p = new PropExt() - .append(undertowConfig) // 指定的配置文件不存在时抛出异常 - .appendIfExists(buildUndertowConfigPro(undertowConfig)); // 尝试加载指定配置文件的 product 配置 - + p = createPropExt(undertowConfig); + if (p.notEmpty()) { init(); } } + + + protected PropExt createPropExt(String undertowConfig){ + return new PropExt() + .appendIfExists(undertowConfig) + .appendIfExists(buildUndertowConfigPro(undertowConfig)); // 尝试加载指定配置文件的 product 配置 + + } /** * 假定用户创建 UndertowServer 时指定 undertow 的配置文件为 abc.txt