import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
@SuppressWarnings("unused")
public class ReadPropertiesUtil {
static final Logger log = LoggerFactory.getLogger(ManifestUtil.class);
private ReadPropertiesUtil() {
throw new IllegalStateException("Utility class");
}
private static Properties loadProperties(String filePath) {
Properties properties = null;
try {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
return null;
} else {
properties = new Properties();
try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
properties.load(bf);
}
}
} catch (Exception e) {
log.error("ReadPropertiesUtil.getProperties.err:{}", e.getMessage());
}
return properties;
}
public static Properties getProperties(String filePath) {
Properties properties = loadProperties(filePath);
if (properties == null) {
return new Properties();
} else {
return properties;
}
}
public static List<Properties> getAllProperties(List<Properties> useList, String... filePaths) {
List<Properties> properties = useList == null ? new LinkedList<>() : useList;
if (filePaths != null) {
Properties one;
for (String filePath : filePaths) {
one = loadProperties(filePath);
if (one != null) {
properties.add(one);
}
}
}
return properties;
}
public static List<Properties> getAllProperties(String... filePaths) {
return getAllProperties(new LinkedList<>(), filePaths);
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
@SuppressWarnings("unused")
public class FincmpConfig {
static final Logger log = LoggerFactory.getLogger(ManifestUtil.class);
static final String FLUSH_STATE_FILE_PATH = "config/flush_properties_state";
static final String[] LOAD_PROPERTIES_PATHS = new String[]{
"config/fincmp.properties"
};
static final List<Properties> ps = ReadPropertiesUtil.getAllProperties(
LOAD_PROPERTIES_PATHS
);
static final AtomicBoolean lock = new AtomicBoolean(false);
private FincmpConfig() {
throw new IllegalStateException("Utility class");
}
private static void lock() {
while (lock.get()) {
Thread nowThread = Thread.currentThread();
log.info(String.format("Thread[%s][%s]flushProperties.lock...", nowThread.getId(), nowThread.getName()));
}
}
private static void flushProperties() {
if (new File(FLUSH_STATE_FILE_PATH).exists()) {
try {
lock.set(true);
ps.clear();
ReadPropertiesUtil.getAllProperties(ps, LOAD_PROPERTIES_PATHS);
} catch (Exception e) {
lock.set(false);
} finally {
lock.set(false);
}
}
}
private static void lockFlush() {
lock();
flushProperties();
}
public static String getValue(String key) {
lockFlush();
for (Properties p : ps) {
String v = p.getProperty(key);
if (v != null) {
return v;
}
}
return "";
}
public static boolean getBool(String key) {
lockFlush();
return Boolean.parseBoolean(getValue(key));
}
}