Различные способы указать файл с настройками
Файл с настройками ищется по убывающей последовательности: - указан через параметр asys.props.file; - указан в системном окружении ASYS_PROPS; - находится в той же папке, откуда запустили ASys. Если файл не был найден, берется из по-умолчанию из classpath.
This commit is contained in:
@@ -4,20 +4,69 @@
|
||||
*/
|
||||
package asys.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Main {
|
||||
private static final Logger logger = LoggerFactory.getLogger("Launcher");
|
||||
|
||||
public static void main(String[] args) {
|
||||
Properties asysProperties = new Properties();
|
||||
Core asysCore = new Core();
|
||||
asysCore.setProperties(getProperties());
|
||||
asysCore.start();
|
||||
}
|
||||
|
||||
private static Properties getProperties() {
|
||||
String pathToProps;
|
||||
Properties properties = new Properties();
|
||||
|
||||
// Указан в строке запуска
|
||||
pathToProps = System.getProperty("asys.props.file");
|
||||
if (pathToProps != null && !pathToProps.trim().isEmpty()) {
|
||||
try {
|
||||
properties.load(new FileReader(pathToProps));
|
||||
logger.debug("asys.props.file = {}", pathToProps);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
// Указан в системном окружении
|
||||
pathToProps = System.getenv("ASYS_PROPS");
|
||||
if (pathToProps != null && !pathToProps.trim().isEmpty()) {
|
||||
try {
|
||||
properties.load(new FileReader(pathToProps));
|
||||
logger.debug("ASYS_PROPS = {}", pathToProps);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
// Находится в той же рабочей папке
|
||||
File asysPropsFile = new File("asys.properties");
|
||||
if (asysPropsFile.exists() && !asysPropsFile.isDirectory() &&
|
||||
asysPropsFile.canRead() && asysPropsFile.length() > 0) {
|
||||
try {
|
||||
properties.load(new FileInputStream(asysPropsFile));
|
||||
logger.debug("File = {}", asysPropsFile.getCanonicalPath());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
// Берем файл по умолчанию, из classpath
|
||||
try {
|
||||
asysProperties.load(Main.class.getResourceAsStream("/asys.properties"));
|
||||
properties.load(Main.class.getResourceAsStream("/asys.properties"));
|
||||
logger.debug("Use default properties file");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Core asysCore = new Core();
|
||||
asysCore.setProperties(asysProperties);
|
||||
asysCore.start();
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user