Add database configuration via command-line arguments
A new configuration setup now allows specifying a path for database properties via command-line arguments during application startup. This update also changes the call signature for `DatabaseConnection.initialize()` method to accept a path argument. Similarly, methods in `PropertiesLoader`, `PropertiesWriter`, and `MailSettings` classes were also updated to use the specified path when working with properties files. The `TestForm` class's tests were updated accordingly to handle these changes.
This commit is contained in:
@@ -10,9 +10,15 @@ import java.sql.SQLException;
|
||||
@SpringBootApplication(exclude = ValidationAutoConfiguration.class)
|
||||
public class Main {
|
||||
|
||||
private static String path;
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
path = args[0];
|
||||
SpringApplication.run(Main.class, args);
|
||||
DatabaseConnection.initialize();
|
||||
DatabaseConnection.initialize(args[0]);
|
||||
}
|
||||
|
||||
public static String getConfigPath() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.alttd.forms.properties.PropertiesWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
@@ -26,6 +27,13 @@ public class DatabaseConnection {
|
||||
Database.createTables();
|
||||
}
|
||||
|
||||
public DatabaseConnection(String path) throws SQLException {
|
||||
instance = this;
|
||||
loadProperties(path);
|
||||
instance.openConnection();
|
||||
Database.createTables();
|
||||
}
|
||||
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
instance.openConnection();
|
||||
@@ -36,15 +44,21 @@ public class DatabaseConnection {
|
||||
return instance.connection;
|
||||
}
|
||||
|
||||
public static void initialize(String path) throws SQLException {
|
||||
if (instance != null)
|
||||
return;
|
||||
instance = new DatabaseConnection(path);
|
||||
}
|
||||
|
||||
public static void initialize() throws SQLException {
|
||||
if (instance != null)
|
||||
return;
|
||||
instance = new DatabaseConnection();
|
||||
}
|
||||
|
||||
private void loadProperties() {
|
||||
private void loadProperties(String path) {
|
||||
String fileName = "database.properties";
|
||||
Optional<Properties> optionalProperties = PropertiesLoader.loadProperties(fileName);
|
||||
Optional<Properties> optionalProperties = PropertiesLoader.loadProperties(path, fileName);
|
||||
if (optionalProperties.isPresent()) {
|
||||
properties = optionalProperties.get();
|
||||
return;
|
||||
@@ -58,7 +72,11 @@ public class DatabaseConnection {
|
||||
properties.setProperty("user", "root");
|
||||
properties.setProperty("password", "root");
|
||||
properties.setProperty("parameters", "autoReconnect=true&useSSL=false");
|
||||
PropertiesWriter.writeProperties(properties, fileName);
|
||||
PropertiesWriter.writeProperties(properties, path, fileName);
|
||||
}
|
||||
|
||||
private void loadProperties() {
|
||||
loadProperties("");
|
||||
}
|
||||
|
||||
public void openConnection() throws SQLException {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alttd.forms.mail;
|
||||
|
||||
import com.alttd.forms.Main;
|
||||
import com.alttd.forms.properties.PropertiesLoader;
|
||||
import com.alttd.forms.properties.PropertiesWriter;
|
||||
|
||||
@@ -20,8 +21,9 @@ public class MailSettings {
|
||||
if (mailProperties != null)
|
||||
return mailProperties;
|
||||
|
||||
String path = Main.getConfigPath();
|
||||
String fileName = "mail_settings.properties";
|
||||
Optional<Properties> properties = PropertiesLoader.loadProperties(fileName);
|
||||
Optional<Properties> properties = PropertiesLoader.loadProperties(path, fileName);
|
||||
if (properties.isPresent()) {
|
||||
mailProperties = properties.get();
|
||||
return mailProperties;
|
||||
@@ -33,7 +35,7 @@ public class MailSettings {
|
||||
mailProperties.put("mail.smtp.auth", "true");
|
||||
mailProperties.put("mail.smtp.socketFactory.port", "465");
|
||||
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
PropertiesWriter.writeProperties(mailProperties, fileName);
|
||||
PropertiesWriter.writeProperties(mailProperties, path, fileName);
|
||||
return mailProperties;
|
||||
}
|
||||
|
||||
@@ -59,8 +61,10 @@ public class MailSettings {
|
||||
return createPasswordAuthentication(accountDetails);
|
||||
}
|
||||
|
||||
String path = Main.getConfigPath();
|
||||
|
||||
String fileName = "mail_account.properties";
|
||||
Optional<Properties> properties = PropertiesLoader.loadProperties(fileName);
|
||||
Optional<Properties> properties = PropertiesLoader.loadProperties(path, fileName);
|
||||
if (properties.isPresent()) {
|
||||
accountDetails = properties.get();
|
||||
return createPasswordAuthentication(accountDetails);
|
||||
@@ -70,7 +74,7 @@ public class MailSettings {
|
||||
accountDetails.put("username", "[email protected]");
|
||||
accountDetails.put("password", "testpassword");
|
||||
|
||||
PropertiesWriter.writeProperties(accountDetails, fileName);
|
||||
PropertiesWriter.writeProperties(accountDetails, path, fileName);
|
||||
return createPasswordAuthentication(accountDetails);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -13,15 +15,21 @@ public class PropertiesLoader {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
|
||||
|
||||
public static Optional<Properties> loadProperties(String fileName) {
|
||||
public static Optional<Properties> loadProperties(String path, String fileName) {
|
||||
Properties prop = new Properties();
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Unable to find jar path to load properties (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
File file;
|
||||
if (path.isEmpty()) {
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Unable to find jar path to load properties (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
}
|
||||
file = new File(currentJarPath.get(), fileName);
|
||||
} else {
|
||||
file = Path.of(URI.create("file://" + path + File.separator + fileName)).toFile();
|
||||
}
|
||||
|
||||
File file = new File(currentJarPath.get(), fileName);
|
||||
|
||||
if (!file.exists()) {
|
||||
logger.warn("Tried to load properties file that doesnt exists (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
|
||||
@@ -6,6 +6,8 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -13,13 +15,19 @@ public class PropertiesWriter {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PropertiesWriter.class);
|
||||
|
||||
public static void writeProperties(Properties prop, String fileName) {
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Failed to get current jar directory");
|
||||
return;
|
||||
public static void writeProperties(Properties prop, String path, String fileName) {
|
||||
File file;
|
||||
if (path.isEmpty()) {
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Unable to find jar path to write properties (" + fileName + ")");
|
||||
return;
|
||||
}
|
||||
file = new File(currentJarPath.get(), fileName);
|
||||
} else {
|
||||
file = Path.of(URI.create("file://" + path + File.separator + fileName)).toFile();
|
||||
}
|
||||
File file = new File(currentJarPath.get(), fileName);
|
||||
|
||||
if (file.exists()) {
|
||||
logger.error("File already exists (" + fileName + ")");
|
||||
return;
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
logging.level.com.alttd.forms=warn
|
||||
logging.level.com.alttd.forms=warn
|
||||
server.port=8002
|
||||
@@ -15,7 +15,7 @@ public class TestForm {
|
||||
|
||||
@BeforeAll
|
||||
public static void testInsert() {
|
||||
Assertions.assertDoesNotThrow(DatabaseConnection::initialize);
|
||||
Assertions.assertDoesNotThrow(() -> DatabaseConnection.initialize());
|
||||
StoreFormQuery storeFormQuery = new StoreFormQuery();
|
||||
ContactFormData contactFormData = new ContactFormData("akastijn", "[email protected]", "This is a test question.");
|
||||
storeFormQuery.storeFormForVerificationCode("[email protected]", contactFormData).
|
||||
@@ -28,7 +28,7 @@ public class TestForm {
|
||||
|
||||
@Test
|
||||
public void testRetrieveForm() {
|
||||
Assertions.assertDoesNotThrow(DatabaseConnection::initialize);
|
||||
Assertions.assertDoesNotThrow(() -> DatabaseConnection.initialize());
|
||||
new FormQuery().getFormForCode(String.valueOf(code), "[email protected]").thenAccept(result -> {
|
||||
Assertions.assertTrue(result.failReason().isEmpty());
|
||||
Optional<Form> optionalForm = result.form();
|
||||
|
||||
Reference in New Issue
Block a user