Files
eight-track/src/main/java/net/locusworks/discord/eighttrack/services/ConfigurationService.java
2019-10-15 00:19:48 -05:00

234 lines
8.1 KiB
Java

/**
*
* Project: Eight Track, File: ConfigurationService.java
*
* Copyright 2019-2019 Locusworks LLC.
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
* any means and imposes fines up to $25,000 for violation. No part of this material
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
* modified, duplicated, adapted or translated into another program language in any
* form or by any means, electronic, mechanical, photocopying, recording, or
* otherwise, without the prior written permission from Locusworks. Locusworks
* affirms that Eight-Track(R) software and data is subject to United States
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
*
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
package net.locusworks.discord.eighttrack.services;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.locusworks.crypto.configuration.PropertiesManager;
import net.locusworks.discord.eighttrack.enums.Configuration;
import net.locusworks.logger.ApplicationLogger;
import net.locusworks.logger.ApplicationLoggerFactory;
@Service
public class ConfigurationService {
private Properties configuration;
private Properties defaults = null;
private Path eightTrackConf = null;
private long lastModified = 0;
private static final String PROPERTIES_FILE = "eighttrack.properties";
private static ApplicationLogger logger = ApplicationLoggerFactory.getLogger(ConfigurationService.class);
@Autowired
private AESService aesService;
/**
* Initialize the configuration service
* This happens during server start up
* @throws IOException If the file cannot be read
* @throws Exception any other exception thrown
*/
@PostConstruct
private void init() throws IOException, Exception {
// load defaults when the webapp loads
try {
defaults = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
} catch (IOException ex) {
logger.error(String.format("Failed to load default %s: %s", PROPERTIES_FILE, ex.getMessage()));
throw ex;
}
// create portalConf File object
eightTrackConf = Paths.get("conf").resolve(PROPERTIES_FILE);
if (!Files.exists(eightTrackConf.getParent())) {
Files.createDirectories(eightTrackConf.getParent());
}
// initial config load
loadConf();
}
/**
* Load the configuration file
* @throws IOException
*/
private void loadConf() throws IOException {
// load the active config file
// ignore read error, we can continue with an empty configuration map
// and all default items will be added below and the file created
logger.info("Loading config file: " + eightTrackConf);
try {
configuration = PropertiesManager.loadConfiguration(eightTrackConf);
} catch (Exception e) {
logger.info("Config file: " + eightTrackConf + " will be created from template");
configuration = new Properties();
}
Map<String, String> results = PropertiesManager.addConfiguration(configuration, defaults);
boolean changed = !results.isEmpty();
if (!results.isEmpty()) {
logger.info(results, new StringBuilder("Added new configuration items:%n"));
}
results = PropertiesManager.removeConfiguration(configuration, defaults);
changed |= !results.isEmpty();
if (!results.isEmpty()) {
logger.info(results, new StringBuilder("Removed unused configuration items:%n"));
}
if (changed) {
PropertiesManager.saveConfiguration(configuration, eightTrackConf, "Eight Track properties file");
}
lastModified = Files.getLastModifiedTime(eightTrackConf).toMillis();
}
public String getLogLevel() {
return configuration.getProperty(Configuration.LOG_LEVEL.getValue());
}
/**
* Save the configuration values to file
* @param confs Configuration property values to save
* @throws Exception general exception
*/
public void saveToConf(Properties confs) throws Exception {
PropertiesManager.saveConfiguration(confs, eightTrackConf, "Eight Track properties file");
logger.info("Saved config file: " + eightTrackConf + ", " + confs.size() + " entries");
loadConf();
}
/**
* Get the database root user name.
* It first checks the catalina.properties file for the root user
* then checks the patchrepo.properties file
* @return root database username
*/
public String getDatabaseRootUsername() {
return configuration.getProperty(Configuration.DB_ROOT_USER.getValue());
}
/**
* Get the database root user password
* It first checks the catalina.properties file for the password
* then checks the patchrepo.properties file.
* The password must be encrypted in both locations
* @return root database password
*/
public String getDatabaseRootPassword() {
String dbPasswd = configuration.getProperty(Configuration.DB_ROOT_PASSWORD.getValue());
try {
return aesService.decrypt(dbPasswd);
} catch (Exception ex) {
logger.error("Unable to get db root password " + ex.getMessage());
return null;
}
}
/**
* Get the standard database user name
* @return portal database username
*/
public String getDatabaseUsername() {
return configuration.getProperty(Configuration.DB_USER.getValue());
}
/**
* Get the standard database password.
* The password is encrypted in the properties file
* @return patchrepo database password
*/
public String getDatabasePassword() {
String dbPasswd = configuration.getProperty(Configuration.DB_PASSWORD.getValue());
try {
return aesService.decrypt(dbPasswd);
} catch (Exception ex) {
logger.error("Unable to get db password " + ex.getMessage());
return null;
}
}
/**
* Get the database host url
* @return database host url
*/
public String getDatabaseHost() {
return configuration.getProperty(Configuration.DB_HOST.getValue());
}
/**
* Get the database host port
* @return the database port
*/
public Integer getDatabasePort() {
return Integer.parseInt(configuration.getProperty(Configuration.DB_PORT.getValue()));
}
/**
* Get the discord token
* @return discord token
*/
public String getDiscordToken() {
String token = configuration.getProperty(Configuration.DISCORD_TOKEN.getValue(), "");
return token;
}
public Path getMusicDirectory() {
String dir = System.getProperty("MUSIC_DIR") == null ? System.getenv("MUSIC_DIR") : System.getProperty("MUSIC_DIR");
if (StringUtils.isBlank(dir)) {
dir = configuration.getProperty(Configuration.MUSIC_DIR.getValue(), "");
}
return Paths.get(dir);
}
/**
* Get the last time the configuration file was modified
* @return last modified
*/
public long getLastModified() {
return lastModified;
}
}