Browse Source

add PropsUtil

Looly 6 years ago
parent
commit
4c202733e5

+ 6 - 6
hutool-setting/src/main/java/cn/hutool/setting/Setting.java

@@ -609,6 +609,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 	 *
 	 * @param m Map
 	 */
+	@SuppressWarnings("NullableProblems")
 	@Override
 	public void putAll(Map<? extends String, ? extends String> m) {
 		this.groupedMap.putAll(DEFAULT_GROUP, m);
@@ -627,6 +628,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 	 *
 	 * @return 默认分组(空分组)中的所有键列表
 	 */
+	@SuppressWarnings("NullableProblems")
 	@Override
 	public Set<String> keySet() {
 		return this.groupedMap.keySet(DEFAULT_GROUP);
@@ -637,6 +639,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 	 *
 	 * @return 默认分组(空分组)中的所有值列表
 	 */
+	@SuppressWarnings("NullableProblems")
 	@Override
 	public Collection<String> values() {
 		return this.groupedMap.values(DEFAULT_GROUP);
@@ -647,6 +650,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 	 *
 	 * @return 默认分组(空分组)中的所有键值对列表
 	 */
+	@SuppressWarnings("NullableProblems")
 	@Override
 	public Set<Entry<String, String>> entrySet() {
 		return this.groupedMap.entrySet(DEFAULT_GROUP);
@@ -657,7 +661,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 		final int prime = 31;
 		int result = 1;
 		result = prime * result + ((charset == null) ? 0 : charset.hashCode());
-		result = prime * result + ((groupedMap == null) ? 0 : groupedMap.hashCode());
+		result = prime * result + groupedMap.hashCode();
 		result = prime * result + (isUseVariable ? 1231 : 1237);
 		result = prime * result + ((settingUrl == null) ? 0 : settingUrl.hashCode());
 		return result;
@@ -682,11 +686,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
 		} else if (false == charset.equals(other.charset)) {
 			return false;
 		}
-		if (groupedMap == null) {
-			if (other.groupedMap != null) {
-				return false;
-			}
-		} else if (false == groupedMap.equals(other.groupedMap)) {
+		if (false == groupedMap.equals(other.groupedMap)) {
 			return false;
 		}
 		if (isUseVariable != other.isUseVariable) {

+ 32 - 9
hutool-setting/src/main/java/cn/hutool/setting/SettingUtil.java

@@ -1,27 +1,29 @@
 package cn.hutool.setting;
 
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.resource.NoResourceException;
 import cn.hutool.core.util.StrUtil;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 /**
  * Setting工具类<br>
  * 提供静态方法获取配置文件
- * 
- * @author looly
  *
+ * @author looly
  */
 public class SettingUtil {
-	/** 配置文件缓存 */
+	/**
+	 * 配置文件缓存
+	 */
 	private static Map<String, Setting> settingMap = new ConcurrentHashMap<>();
 	private static final Object lock = new Object();
 
 	/**
 	 * 获取当前环境下的配置文件<br>
 	 * name可以为不包括扩展名的文件名(默认.setting为结尾),也可以是文件名全称
-	 * 
+	 *
 	 * @param name 文件名,如果没有扩展名,默认为.setting
 	 * @return 当前环境下配置文件
 	 */
@@ -33,8 +35,8 @@ public class SettingUtil {
 				if (null == setting) {
 					String filePath = name;
 					String extName = FileUtil.extName(filePath);
-					if(StrUtil.isEmpty(extName)) {
-						filePath  = filePath + "." + Setting.EXT_NAME;
+					if (StrUtil.isEmpty(extName)) {
+						filePath = filePath + "." + Setting.EXT_NAME;
 					}
 					setting = new Setting(filePath, true);
 					settingMap.put(name, setting);
@@ -43,4 +45,25 @@ public class SettingUtil {
 		}
 		return setting;
 	}
+
+	/**
+	 * 获取给定路径找到的第一个配置文件<br>
+	 * * name可以为不包括扩展名的文件名(默认.setting为结尾),也可以是文件名全称
+	 *
+	 * @param names 文件名,如果没有扩展名,默认为.setting
+	 *
+	 * @return 当前环境下配置文件
+	 * @since 5.1.3
+	 */
+	public static Setting getFirstFound(String... names) {
+		Setting setting;
+		for (String name : names) {
+			try {
+				return get(name);
+			} catch (NoResourceException e) {
+				//ignore
+			}
+		}
+		return null;
+	}
 }

+ 5 - 0
hutool-setting/src/main/java/cn/hutool/setting/dialect/Props.java

@@ -48,6 +48,11 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
 	private static final long serialVersionUID = 1935981579709590740L;
 	private final static Log log = LogFactory.get();
 
+	/**
+	 * 默认配置文件扩展名
+	 */
+	public final static String EXT_NAME = "properties";
+
 	// ----------------------------------------------------------------------- 私有属性 start
 	/** 属性文件的URL */
 	private URL propertiesFileUrl;

+ 70 - 0
hutool-setting/src/main/java/cn/hutool/setting/dialect/PropsUtil.java

@@ -0,0 +1,70 @@
+package cn.hutool.setting.dialect;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.resource.NoResourceException;
+import cn.hutool.core.util.StrUtil;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Props工具类<br>
+ * 提供静态方法获取配置文件
+ *
+ * @author looly
+ * @since 5.1.3
+ */
+public class PropsUtil {
+
+	/**
+	 * 配置文件缓存
+	 */
+	private static Map<String, Props> propsMap = new ConcurrentHashMap<>();
+	private static final Object lock = new Object();
+
+	/**
+	 * 获取当前环境下的配置文件<br>
+	 * name可以为不包括扩展名的文件名(默认.properties),也可以是文件名全称
+	 *
+	 * @param name 文件名,如果没有扩展名,默认为.properties
+	 * @return 当前环境下配置文件
+	 */
+	public static Props get(String name) {
+		Props props = propsMap.get(name);
+		if (null == props) {
+			synchronized (lock) {
+				props = propsMap.get(name);
+				if (null == props) {
+					String filePath = name;
+					String extName = FileUtil.extName(filePath);
+					if (StrUtil.isEmpty(extName)) {
+						filePath = filePath + "." + Props.EXT_NAME;
+					}
+					props = new Props(filePath);
+					propsMap.put(name, props);
+				}
+			}
+		}
+		return props;
+	}
+
+	/**
+	 * 获取给定路径找到的第一个配置文件<br>
+	 * * name可以为不包括扩展名的文件名(默认.setting为结尾),也可以是文件名全称
+	 *
+	 * @param names 文件名,如果没有扩展名,默认为.setting
+	 *
+	 * @return 当前环境下配置文件
+	 */
+	public static Props getFirstFound(String... names) {
+		Props props;
+		for (String name : names) {
+			try {
+				return get(name);
+			} catch (NoResourceException e) {
+				//ignore
+			}
+		}
+		return null;
+	}
+}

+ 22 - 0
hutool-setting/src/test/java/cn/hutool/setting/test/PropsUtilTest.java

@@ -0,0 +1,22 @@
+package cn.hutool.setting.test;
+
+import cn.hutool.setting.dialect.PropsUtil;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Objects;
+
+public class PropsUtilTest {
+	
+	@Test
+	public void getTest() {
+		String driver = PropsUtil.get("test").getStr("driver");
+		Assert.assertEquals("com.mysql.jdbc.Driver", driver);
+	}
+
+	@Test
+	public void getFirstFoundTest() {
+		String driver = Objects.requireNonNull(PropsUtil.getFirstFound("test2", "test")).getStr("driver");
+		Assert.assertEquals("com.mysql.jdbc.Driver", driver);
+	}
+}

+ 6 - 0
hutool-setting/src/test/java/cn/hutool/setting/test/SettingUtilTest.java

@@ -12,4 +12,10 @@ public class SettingUtilTest {
 		String driver = SettingUtil.get("test").get("demo", "driver");
 		Assert.assertEquals("com.mysql.jdbc.Driver", driver);
 	}
+
+	@Test
+	public void getFirstFoundTest() {
+		String driver = SettingUtil.getFirstFound("test2", "test").get("demo", "driver");
+		Assert.assertEquals("com.mysql.jdbc.Driver", driver);
+	}
 }