mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
Update SimulationConfig.java
Modification to open properties file.
This commit is contained in:
@@ -27,9 +27,43 @@ public class SimulationConfig {
|
|||||||
*/
|
*/
|
||||||
public SimulationConfig(String filePath) throws IOException {
|
public SimulationConfig(String filePath) throws IOException {
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
try (InputStream input = new FileInputStream(filePath)) {
|
/**Tenta carregar diretamente a partir do sistema de ficheiros, se o ficheiro não existir
|
||||||
properties.load(input);
|
* (por exemplo quando executado a partir do classpath/jar),
|
||||||
|
* faz fallback para carregar a partir do classpath usando o ClassLoader.
|
||||||
|
*/
|
||||||
|
IOException lastException = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
try (InputStream input = new FileInputStream(filePath)) {
|
||||||
|
properties.load(input);
|
||||||
|
return; // carregado com sucesso a partir do caminho fornecido
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
lastException = e;
|
||||||
|
//tenta carregar a partir do classpath sem prefixos comuns
|
||||||
|
String resourcePath = filePath;
|
||||||
|
//Remove prefixos que apontam para src/main/resources quando presentes
|
||||||
|
resourcePath = resourcePath.replace("src/main/resources/", "").replace("src\\main\\resources\\", "");
|
||||||
|
//Remove prefixo classpath: se fornecido
|
||||||
|
if (resourcePath.startsWith("classpath:")) {
|
||||||
|
resourcePath = resourcePath.substring("classpath:".length());
|
||||||
|
if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
|
||||||
|
if (resourceStream == null) {
|
||||||
|
//como último recurso, tentar com um leading slash
|
||||||
|
resourceStream = SimulationConfig.class.getResourceAsStream('/' + resourcePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resourceStream != null) {
|
||||||
|
try (InputStream input = resourceStream) {
|
||||||
|
properties.load(input);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (lastException != null) throw lastException;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Network configurations ---
|
// --- Network configurations ---
|
||||||
|
|||||||
Reference in New Issue
Block a user