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