|
@@ -24,27 +24,29 @@ import com.jfinal.core.Const;
|
|
|
* PropKit. PropKit can load properties file from CLASSPATH or File object.
|
|
* PropKit. PropKit can load properties file from CLASSPATH or File object.
|
|
|
*/
|
|
*/
|
|
|
public class PropKit {
|
|
public class PropKit {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private static String envKey = "app.env";
|
|
|
|
|
+
|
|
|
private static Prop prop = null;
|
|
private static Prop prop = null;
|
|
|
private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>();
|
|
private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private PropKit() {}
|
|
private PropKit() {}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Use the first found properties file
|
|
|
|
|
|
|
+ * 设置环境 key,PropKit 将通过该 key 获取环境 value。envKey 默认值为 "app.env"
|
|
|
*/
|
|
*/
|
|
|
- public static Prop useFirstFound(String... fileNames) {
|
|
|
|
|
- for (String fn : fileNames) {
|
|
|
|
|
- try {
|
|
|
|
|
- return use(fn, Const.DEFAULT_ENCODING);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- continue ;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- throw new IllegalArgumentException("没有配置文件可被使用");
|
|
|
|
|
|
|
+ public static void setEnvKey(String envKey) {
|
|
|
|
|
+ PropKit.envKey = envKey;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getEnvKey() {
|
|
|
|
|
+ return PropKit.envKey;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getEnv() {
|
|
|
|
|
+ return getProp().get(envKey);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Use the properties file. It will loading the properties file if not loading.
|
|
* Use the properties file. It will loading the properties file if not loading.
|
|
|
* @see #use(String, String)
|
|
* @see #use(String, String)
|
|
@@ -52,7 +54,7 @@ public class PropKit {
|
|
|
public static Prop use(String fileName) {
|
|
public static Prop use(String fileName) {
|
|
|
return use(fileName, Const.DEFAULT_ENCODING);
|
|
return use(fileName, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Use the properties file. It will loading the properties file if not loading.
|
|
* Use the properties file. It will loading the properties file if not loading.
|
|
|
* <p>
|
|
* <p>
|
|
@@ -61,12 +63,12 @@ public class PropKit {
|
|
|
* PropKit.use("other_config.txt", "UTF-8");<br><br>
|
|
* PropKit.use("other_config.txt", "UTF-8");<br><br>
|
|
|
* String userName = PropKit.get("userName");<br>
|
|
* String userName = PropKit.get("userName");<br>
|
|
|
* String password = PropKit.get("password");<br><br>
|
|
* String password = PropKit.get("password");<br><br>
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* userName = PropKit.use("other_config.txt").get("userName");<br>
|
|
* userName = PropKit.use("other_config.txt").get("userName");<br>
|
|
|
* password = PropKit.use("other_config.txt").get("password");<br><br>
|
|
* password = PropKit.use("other_config.txt").get("password");<br><br>
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
|
|
* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param fileName the properties file's name in classpath or the sub directory of classpath
|
|
* @param fileName the properties file's name in classpath or the sub directory of classpath
|
|
|
* @param encoding the encoding
|
|
* @param encoding the encoding
|
|
|
*/
|
|
*/
|
|
@@ -77,6 +79,7 @@ public class PropKit {
|
|
|
result = map.get(fileName);
|
|
result = map.get(fileName);
|
|
|
if (result == null) {
|
|
if (result == null) {
|
|
|
result = new Prop(fileName, encoding);
|
|
result = new Prop(fileName, encoding);
|
|
|
|
|
+ handleEnv(result, fileName);
|
|
|
map.put(fileName, result);
|
|
map.put(fileName, result);
|
|
|
if (PropKit.prop == null) {
|
|
if (PropKit.prop == null) {
|
|
|
PropKit.prop = result;
|
|
PropKit.prop = result;
|
|
@@ -86,7 +89,23 @@ public class PropKit {
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据环境配置切换配置文件,便于项目在 dev、pro 等环境下部署
|
|
|
|
|
+ * 例如:
|
|
|
|
|
+ * 1: 假定 config.txt 中存在配置 app.env = pro
|
|
|
|
|
+ * 2: PropKit.use("config.txt") 则会加载 config-pro.txt 中的配置
|
|
|
|
|
+ */
|
|
|
|
|
+ private static void handleEnv(Prop result, String fileName) {
|
|
|
|
|
+ String env = result.get(envKey);
|
|
|
|
|
+ if (StrKit.notBlank(env)) {
|
|
|
|
|
+ int index = fileName.lastIndexOf('.');
|
|
|
|
|
+ String envConfigName = fileName.substring(0, index) + "-" + env + fileName.substring(index);
|
|
|
|
|
+ Prop envConfig = new Prop(envConfigName);
|
|
|
|
|
+ result.append(envConfig); // 追加环境配置
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Use the properties file bye File object. It will loading the properties file if not loading.
|
|
* Use the properties file bye File object. It will loading the properties file if not loading.
|
|
|
* @see #use(File, String)
|
|
* @see #use(File, String)
|
|
@@ -94,14 +113,14 @@ public class PropKit {
|
|
|
public static Prop use(File file) {
|
|
public static Prop use(File file) {
|
|
|
return use(file, Const.DEFAULT_ENCODING);
|
|
return use(file, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Use the properties file bye File object. It will loading the properties file if not loading.
|
|
* Use the properties file bye File object. It will loading the properties file if not loading.
|
|
|
* <p>
|
|
* <p>
|
|
|
* Example:<br>
|
|
* Example:<br>
|
|
|
* PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");<br>
|
|
* PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");<br>
|
|
|
* Strig userName = PropKit.use("my_config.txt").get("userName");
|
|
* Strig userName = PropKit.use("my_config.txt").get("userName");
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param file the properties File object
|
|
* @param file the properties File object
|
|
|
* @param encoding the encoding
|
|
* @param encoding the encoding
|
|
|
*/
|
|
*/
|
|
@@ -112,6 +131,7 @@ public class PropKit {
|
|
|
result = map.get(file.getName());
|
|
result = map.get(file.getName());
|
|
|
if (result == null) {
|
|
if (result == null) {
|
|
|
result = new Prop(file, encoding);
|
|
result = new Prop(file, encoding);
|
|
|
|
|
+ handleEnv(result, file.getName());
|
|
|
map.put(file.getName(), result);
|
|
map.put(file.getName(), result);
|
|
|
if (PropKit.prop == null) {
|
|
if (PropKit.prop == null) {
|
|
|
PropKit.prop = result;
|
|
PropKit.prop = result;
|
|
@@ -121,7 +141,7 @@ public class PropKit {
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop useless(String fileName) {
|
|
public static Prop useless(String fileName) {
|
|
|
Prop previous = map.remove(fileName);
|
|
Prop previous = map.remove(fileName);
|
|
|
if (PropKit.prop == previous) {
|
|
if (PropKit.prop == previous) {
|
|
@@ -129,12 +149,12 @@ public class PropKit {
|
|
|
}
|
|
}
|
|
|
return previous;
|
|
return previous;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static void clear() {
|
|
public static void clear() {
|
|
|
prop = null;
|
|
prop = null;
|
|
|
map.clear();
|
|
map.clear();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop append(Prop prop) {
|
|
public static Prop append(Prop prop) {
|
|
|
synchronized (PropKit.class) {
|
|
synchronized (PropKit.class) {
|
|
|
if (PropKit.prop != null) {
|
|
if (PropKit.prop != null) {
|
|
@@ -145,15 +165,15 @@ public class PropKit {
|
|
|
return PropKit.prop;
|
|
return PropKit.prop;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop append(String fileName, String encoding) {
|
|
public static Prop append(String fileName, String encoding) {
|
|
|
return append(new Prop(fileName, encoding));
|
|
return append(new Prop(fileName, encoding));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop append(String fileName) {
|
|
public static Prop append(String fileName) {
|
|
|
return append(fileName, Const.DEFAULT_ENCODING);
|
|
return append(fileName, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop appendIfExists(String fileName, String encoding) {
|
|
public static Prop appendIfExists(String fileName, String encoding) {
|
|
|
try {
|
|
try {
|
|
|
return append(new Prop(fileName, encoding));
|
|
return append(new Prop(fileName, encoding));
|
|
@@ -161,81 +181,94 @@ public class PropKit {
|
|
|
return PropKit.prop;
|
|
return PropKit.prop;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop appendIfExists(String fileName) {
|
|
public static Prop appendIfExists(String fileName) {
|
|
|
return appendIfExists(fileName, Const.DEFAULT_ENCODING);
|
|
return appendIfExists(fileName, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop append(File file, String encoding) {
|
|
public static Prop append(File file, String encoding) {
|
|
|
return append(new Prop(file, encoding));
|
|
return append(new Prop(file, encoding));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop append(File file) {
|
|
public static Prop append(File file) {
|
|
|
return append(file, Const.DEFAULT_ENCODING);
|
|
return append(file, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop appendIfExists(File file, String encoding) {
|
|
public static Prop appendIfExists(File file, String encoding) {
|
|
|
if (file.exists()) {
|
|
if (file.exists()) {
|
|
|
append(new Prop(file, encoding));
|
|
append(new Prop(file, encoding));
|
|
|
}
|
|
}
|
|
|
return PropKit.prop;
|
|
return PropKit.prop;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop appendIfExists(File file) {
|
|
public static Prop appendIfExists(File file) {
|
|
|
return appendIfExists(file, Const.DEFAULT_ENCODING);
|
|
return appendIfExists(file, Const.DEFAULT_ENCODING);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Use the first found properties file
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Prop useFirstFound(String... fileNames) {
|
|
|
|
|
+ for (String fn : fileNames) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return use(fn, Const.DEFAULT_ENCODING);
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ throw new IllegalArgumentException("没有配置文件可被使用");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static Prop getProp() {
|
|
public static Prop getProp() {
|
|
|
if (prop == null) {
|
|
if (prop == null) {
|
|
|
throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");
|
|
throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");
|
|
|
}
|
|
}
|
|
|
return prop;
|
|
return prop;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Prop getProp(String fileName) {
|
|
public static Prop getProp(String fileName) {
|
|
|
return map.get(fileName);
|
|
return map.get(fileName);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String get(String key) {
|
|
public static String get(String key) {
|
|
|
return getProp().get(key);
|
|
return getProp().get(key);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String get(String key, String defaultValue) {
|
|
public static String get(String key, String defaultValue) {
|
|
|
return getProp().get(key, defaultValue);
|
|
return getProp().get(key, defaultValue);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Integer getInt(String key) {
|
|
public static Integer getInt(String key) {
|
|
|
return getProp().getInt(key);
|
|
return getProp().getInt(key);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Integer getInt(String key, Integer defaultValue) {
|
|
public static Integer getInt(String key, Integer defaultValue) {
|
|
|
return getProp().getInt(key, defaultValue);
|
|
return getProp().getInt(key, defaultValue);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Long getLong(String key) {
|
|
public static Long getLong(String key) {
|
|
|
return getProp().getLong(key);
|
|
return getProp().getLong(key);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Long getLong(String key, Long defaultValue) {
|
|
public static Long getLong(String key, Long defaultValue) {
|
|
|
return getProp().getLong(key, defaultValue);
|
|
return getProp().getLong(key, defaultValue);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Double getDouble(String key) {
|
|
public static Double getDouble(String key) {
|
|
|
return getProp().getDouble(key);
|
|
return getProp().getDouble(key);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Double getDouble(String key, Double defaultValue) {
|
|
public static Double getDouble(String key, Double defaultValue) {
|
|
|
return getProp().getDouble(key, defaultValue);
|
|
return getProp().getDouble(key, defaultValue);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Boolean getBoolean(String key) {
|
|
public static Boolean getBoolean(String key) {
|
|
|
return getProp().getBoolean(key);
|
|
return getProp().getBoolean(key);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static Boolean getBoolean(String key, Boolean defaultValue) {
|
|
public static Boolean getBoolean(String key, Boolean defaultValue) {
|
|
|
return getProp().getBoolean(key, defaultValue);
|
|
return getProp().getBoolean(key, defaultValue);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static boolean containsKey(String key) {
|
|
public static boolean containsKey(String key) {
|
|
|
return getProp().containsKey(key);
|
|
return getProp().containsKey(key);
|
|
|
}
|
|
}
|