-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration is only initialized once (#9)
* Hot load configuration file - Detect configuration file modification - Failsafe new configuration loading, old one will remain if issue occurs in new one - Application stops if initial configuration loading fails * Unit test and improvements - Add unit tests for configuration loading - Improve code lisibility - Fix small bugs
- Loading branch information
Showing
7 changed files
with
343 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,7 @@ build/ | |
### VS Code ### | ||
.vscode/ | ||
**script** | ||
*config.json | ||
*config.json | ||
|
||
### Logs ### | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/smart/home/pc/daemon/service/ConfigurationLoaderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.smart.home.pc.daemon.service; | ||
|
||
import com.smart.home.pc.daemon.dto.Config; | ||
|
||
public interface ConfigurationLoaderService { | ||
|
||
public Config loadConfiguration(String configurationPath); | ||
|
||
public boolean isConfigurationUpdated(String configurationPath); | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/smart/home/pc/daemon/service/impl/ConfigurationLoaderServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.smart.home.pc.daemon.service.impl; | ||
|
||
import java.util.Date; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import com.smart.home.pc.daemon.dto.Config; | ||
import com.smart.home.pc.daemon.service.ConfigurationLoaderService; | ||
import com.smart.home.pc.daemon.util.FileHelper; | ||
|
||
public class ConfigurationLoaderServiceImpl implements ConfigurationLoaderService { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(ConfigurationLoaderServiceImpl.class); | ||
|
||
private Date configurationFileLastModifiedDate; | ||
|
||
private Config currentUserConfiguration; | ||
|
||
private FileHelper fileHelper = new FileHelper(); | ||
|
||
public ConfigurationLoaderServiceImpl() { | ||
|
||
} | ||
|
||
public ConfigurationLoaderServiceImpl(String configurationPath) { | ||
loadConfiguration(configurationPath); | ||
} | ||
|
||
@Override | ||
public Config loadConfiguration(String configurationPath) { | ||
Date configurationPathLastModificationDate = fileHelper.getFileLastModificationDate(configurationPath); | ||
// Configuration is found and is a file. | ||
if (configurationPathLastModificationDate != null) { | ||
Config loadedConfiguration = fileHelper.getConfigurationFromPath(configurationPath); | ||
if (loadedConfiguration != null) { | ||
configurationFileLastModifiedDate = configurationPathLastModificationDate; | ||
currentUserConfiguration = loadedConfiguration; | ||
return currentUserConfiguration; | ||
} else { | ||
LOGGER.error("Issue loading configuration file: " + configurationPath); | ||
} | ||
} else { | ||
// Configuration file not found or null | ||
LOGGER.error("The provided configuration path was not found: " + configurationPath); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isConfigurationUpdated(String configurationPath) { | ||
if (configurationPath != null) { | ||
Date currentConfigDate = fileHelper.getFileLastModificationDate(configurationPath); | ||
if (currentConfigDate == null || currentConfigDate.equals(configurationFileLastModifiedDate)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public Date getConfigurationLoadDate() { | ||
return configurationFileLastModifiedDate; | ||
} | ||
|
||
public void setConfigurationLoadDate(Date configurationLoadDate) { | ||
this.configurationFileLastModifiedDate = configurationLoadDate; | ||
} | ||
|
||
public Config getCurrentUserConfiguration() { | ||
return currentUserConfiguration; | ||
} | ||
|
||
public void setCurrentUserConfiguration(Config currentUserConfiguration) { | ||
this.currentUserConfiguration = currentUserConfiguration; | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/smart/home/pc/daemon/util/FileHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.smart.home.pc.daemon.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Date; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import com.google.gson.Gson; | ||
import com.smart.home.pc.daemon.dto.Config; | ||
|
||
/** | ||
* Helper class to manage file actions | ||
* | ||
* @author Ramzi | ||
* | ||
*/ | ||
public class FileHelper { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(FileHelper.class); | ||
|
||
private Gson gson = new Gson(); | ||
|
||
/** | ||
* Get last modification date for the given file path | ||
* | ||
* @param filePath, String containing the absolute path of the file | ||
* @return last modification date or null | ||
*/ | ||
public Date getFileLastModificationDate(String fileAbsPath) { | ||
if (StringUtils.isEmpty(fileAbsPath)) { | ||
return null; | ||
} | ||
|
||
File configFile = new File(fileAbsPath); | ||
if (configFile.exists()) { | ||
return new Date(configFile.lastModified()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Reads the configuration file as a Json and returns the corresponding object | ||
* | ||
* @param configFilePath | ||
* @return | ||
*/ | ||
public Config getConfigurationFromPath(String configurationPath) { | ||
Path configFilePath = Paths.get(configurationPath); | ||
Config userConfiguration = null; | ||
|
||
Reader reader; | ||
try { | ||
reader = Files.newBufferedReader(configFilePath); | ||
|
||
// convert JSON file to map | ||
userConfiguration = gson.fromJson(reader, Config.class); | ||
reader.close(); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
LOGGER.error("Exception while reading configuration file please check file format: " + configFilePath); | ||
} | ||
|
||
if (userConfiguration == null) { | ||
LOGGER.error("Configuration file is either empty or malformed"); | ||
} | ||
|
||
return userConfiguration; | ||
} | ||
|
||
} |
Oops, something went wrong.