Browse Source

SecurityProperty

deason 4 years ago
parent
commit
a404fee892

+ 71 - 0
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/security/SecurityProperty.java

@@ -0,0 +1,71 @@
+package cn.com.qmth.examcloud.web.security;
+
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Properties;
+
+public class SecurityProperty {
+
+    private static final Logger log = LoggerFactory.getLogger(SecurityProperty.class);
+
+    private static final Properties PROPS = new Properties();
+
+    public static synchronized void loadProperties(String propertyPath) {
+        if (StringUtils.isEmpty(propertyPath)) {
+            return;
+        }
+
+        InputStream is = SecurityProperty.class.getClassLoader().getResourceAsStream(propertyPath);
+        if (is == null) {
+            log.warn("{} is not exist!", propertyPath);
+            return;
+        }
+
+        try (InputStreamReader isr = new InputStreamReader(is, "UTF-8");
+             BufferedReader br = new BufferedReader(isr);) {
+
+            Properties curProperties = new Properties();
+            curProperties.load(br);
+
+            if (curProperties.size() > 0) {
+                PROPS.putAll(curProperties);
+                log.info("Security loadProperties size is {}", PROPS.size());
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                is.close();
+            } catch (IOException e) {
+                // ignore...
+            }
+        }
+    }
+
+    public static String getProperty(String key) {
+        return PROPS.getProperty(key);
+    }
+
+    public static String getProperty(String key, String defaultVale) {
+        return PROPS.getProperty(key, defaultVale);
+    }
+
+    public static Boolean getBooleanProperty(String key, Boolean defaultVale) {
+        String value = getProperty(key);
+        if ("true".equals(value)) {
+            return true;
+        }
+        if ("false".equals(value)) {
+            return false;
+        }
+        return defaultVale;
+    }
+
+}