Merge pull request #2 from gmbrax/feature/Database-Connector
Feature/database connector
This commit is contained in:
commit
ed2f197c60
|
|
@ -44,3 +44,11 @@ config-local.properties
|
|||
pipes/
|
||||
*.log
|
||||
/.idea/
|
||||
|
||||
Configuration files with sensitive credentials
|
||||
# These files contain database passwords and API keys and should never be committed
|
||||
src/main/resources/config.properties
|
||||
src/main/resources/application.properties
|
||||
|
||||
# Allow example configuration files to be committed
|
||||
!src/main/resources/*.properties.example
|
||||
|
|
@ -7,9 +7,12 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.mediamanager.service.database.DatabaseManager;
|
||||
|
||||
public class MediaManagerApplication {
|
||||
private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class);
|
||||
private static Properties config;
|
||||
private static DatabaseManager databaseManager;
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.info("Starting MediaManager Core Application...");
|
||||
|
|
@ -17,8 +20,10 @@ public class MediaManagerApplication {
|
|||
try {
|
||||
// Load configuration
|
||||
loadConfiguration();
|
||||
databaseManager = new DatabaseManager(config);
|
||||
databaseManager.init();
|
||||
|
||||
// TODO: Initialize database connection
|
||||
|
||||
// TODO: Initialize IPC server with named pipes
|
||||
// TODO: Start application services
|
||||
|
||||
|
|
@ -28,6 +33,9 @@ public class MediaManagerApplication {
|
|||
// Keep application running
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
logger.info("Shutting down MediaManager Core...");
|
||||
if (databaseManager != null) {
|
||||
databaseManager.close();
|
||||
|
||||
// TODO: Cleanup resources
|
||||
|
||||
logger.info("MediaManager Core shutdown successfully");
|
||||
|
|
@ -35,6 +43,7 @@ public class MediaManagerApplication {
|
|||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
|
||||
}
|
||||
}));
|
||||
logger.info("Application is running");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,98 @@
|
|||
package com.mediamanager.service.database;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
public class DatabaseManager {
|
||||
private final Properties configuration;
|
||||
private Connection connection;
|
||||
private static final Logger logger = LogManager.getLogger(DatabaseManager.class);
|
||||
public DatabaseManager(Properties config) {
|
||||
this.configuration = config;
|
||||
logger.debug("DatabaseManager created with configuration:");
|
||||
|
||||
}
|
||||
|
||||
public void init() throws Exception {
|
||||
logger.info("Initializing database connection...");
|
||||
validateConfiguration();
|
||||
|
||||
String databaseType = configuration.getProperty("database.type");
|
||||
String databaseUrl = configuration.getProperty("database.url");
|
||||
String databaseUsername = configuration.getProperty("database.username");
|
||||
String databasePassword = configuration.getProperty("database.password");
|
||||
String databasePort = configuration.getProperty("database.port");
|
||||
String databaseName = configuration.getProperty("database.name");
|
||||
|
||||
|
||||
String connectionString = String.format("jdbc:postgresql://%s:%s/%s", databaseUrl, databasePort, databaseName);
|
||||
logger.debug("Attempting to connect to: {}", connectionString);
|
||||
try {
|
||||
connection = DriverManager.getConnection(connectionString, databaseUsername, databasePassword);
|
||||
logger.info("Database connection established successfully");
|
||||
|
||||
performSanityChecks();
|
||||
logger.info("Database sanity checks passed successfully");
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to connect to database", e);
|
||||
throw new Exception("Database connection failed: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
private void performSanityChecks() throws SQLException {
|
||||
logger.debug("Performing sanity checks...");
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
stmt.execute("SELECT 1");
|
||||
|
||||
}
|
||||
String databaseProductName = connection.getMetaData().getDatabaseProductName();
|
||||
String databaseProductVersion = connection.getMetaData().getDatabaseProductVersion();
|
||||
logger.info("Connected to database: {} v{}", databaseProductName, databaseProductVersion);
|
||||
}
|
||||
private void validateConfiguration() throws Exception {
|
||||
String[] requiredProperties = {
|
||||
"database.url",
|
||||
"database.username",
|
||||
"database.password",
|
||||
"database.port",
|
||||
"database.name"
|
||||
};
|
||||
|
||||
for (String property : requiredProperties) {
|
||||
if (configuration.getProperty(property) == null ||
|
||||
configuration.getProperty(property).trim().isEmpty()) {
|
||||
throw new Exception("Required database configuration missing: " + property);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Database configuration validated successfully");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
logger.info("Database connection closed successfully");
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to close database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,3 +21,4 @@ hibernate.format_sql=true
|
|||
ipc.pipe.name=mediamanager-pipe
|
||||
ipc.pipe.path=/tmp/mediamanager
|
||||
ipc.buffer.size=8192
|
||||
|
||||
Loading…
Reference in New Issue