Skip to content

Commit

Permalink
Raw code for connect PSQL from LibShared
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantpatil1214 committed Oct 22, 2024
1 parent 71f5e9e commit 77eb8f7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.AppConfig;
import org.jembi.jempi.libapi.BackEnd;

import org.jembi.jempi.shared.config.Config;
import java.util.UUID;

public final class API {
Expand All @@ -23,6 +23,13 @@ private API() {

public static void main(final String[] args) {
try {
// PostgresConfig postgresConfig = new PostgresConfig("localhost", 5432, "jempi", "postgres", "postgres");
Config.create(AppConfig.POSTGRESQL_IP,
AppConfig.POSTGRESQL_PORT,
AppConfig.POSTGRESQL_CONFIGURATION_DB,
AppConfig.POSTGRESQL_USER,
AppConfig.POSTGRESQL_PASSWORD);

new API().run();
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jembi.jempi.libmpi.LibMPI;
import org.jembi.jempi.libmpi.MpiGeneralError;
import org.jembi.jempi.libmpi.MpiServiceError;
import org.jembi.jempi.shared.config.Config;
import org.jembi.jempi.shared.models.*;
import org.jembi.jempi.shared.models.ConfigurationModel.Configuration;
import org.jembi.jempi.shared.models.dashboard.NotificationStats;
Expand Down Expand Up @@ -91,6 +92,7 @@ private BackEnd(
psqlAuditTrail = new PsqlAuditTrail(sqlIP, sqlPort, sqlAuditDb, sqlUser, sqlPassword);
openMPI(kafkaBootstrapServers, kafkaClientId, debugLevel);
psqlClient = new PsqlClient(sqlIP, sqlPort, sqlConfigurationDb, sqlUser, sqlPassword);
Config.create(sqlIP, sqlPort, sqlConfigurationDb, sqlUser, sqlPassword);
this.postgresClientDao = PostgresClientDaoImpl.create(sqlIP, sqlPort, sqlConfigurationDb, sqlUser, sqlPassword);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,87 @@
package org.jembi.jempi.shared.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.shared.config.input.JsonConfig;
import org.jembi.jempi.shared.utils.AppUtils;

import java.io.File;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.fasterxml.jackson.core.JsonProcessingException;

public final class Config {

public static final String CONFIG_FILE = "config.json";
private static final Logger LOGGER = LogManager.getLogger(Config.class);
public static final JsonConfig JSON_CONFIG;
public static final FieldsConfig FIELDS_CONFIG;
public static final InputInterfaceConfig INPUT_INTERFACE_CONFIG;
public static final ApiConfig API_CONFIG;
public static final LinkerConfig LINKER_CONFIG;
public static final DGraphConfig DGRAPH_CONFIG;

private static String pgIp;
private static int pgPort;
private static String pgDb;
private static String pgUser;
private static String pgPassword;
static {
final var separator = FileSystems.getDefault().getSeparator();
final String configDir = System.getenv("SYSTEM_CONFIG_DIRS");
Path filePath = Paths.get(""); // Start with an empty path
// Create ubuntuFilePath
Path ubuntuFilePath = new File(String.format("%sapp%sconf_system%s%s", separator, separator, separator, CONFIG_FILE)).toPath();
// Check if ubuntuFilePath exists
if (Files.exists(ubuntuFilePath)) {
filePath = ubuntuFilePath;
} else {
// If ubuntuFilePath does not exist, assign the alternative path for windows
filePath = Paths.get(configDir, CONFIG_FILE);
}
JSON_CONFIG = JsonConfig.fromJson(String.valueOf(filePath));
JSON_CONFIG = retrieveConfigFromDatabase();
FIELDS_CONFIG = new FieldsConfig(JSON_CONFIG);
INPUT_INTERFACE_CONFIG = new InputInterfaceConfig(JSON_CONFIG);
API_CONFIG = new ApiConfig(JSON_CONFIG);
LINKER_CONFIG = new LinkerConfig(JSON_CONFIG);
DGRAPH_CONFIG = new DGraphConfig(JSON_CONFIG);
}

private Config() {
private Config(
final String ip,
final int port,
final String db,
final String user,
final String password) {
pgIp = ip;
pgPort = port;
pgDb = db;
pgUser = user;
pgPassword = password;
}

public static Config create(
final String ip,
final int port,
final String db,
final String user,
final String password) {
return new Config(ip, port, db, user, password);
}

private static JsonConfig retrieveConfigFromDatabase() {
String port = String.valueOf(pgPort);
LOGGER.info(".............................................................");
LOGGER.info("Retrieved configuration from database: {} {} {} {} {}", port, pgIp, pgDb, pgUser, pgPassword);
LOGGER.info(".............................................................");
// Database connection details (should be externalized in production)
String url = "jdbc:postgresql://" + pgIp + ":" + port + "/" + pgDb;
String jsonConfig = null;
JsonConfig jsonConfigs = null;
try (Connection conn = DriverManager.getConnection(url, pgUser, pgPassword);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT json FROM configuration WHERE key = 'config'")) {

if (rs.next()) {
jsonConfig = rs.getString("json");
jsonConfigs = AppUtils.OBJECT_MAPPER.readValue(jsonConfig, JsonConfig.class);
LOGGER.info(".............................................................");
LOGGER.info("Retrieved configuration from database: {}", jsonConfigs);
LOGGER.info(".............................................................");
} else {
throw new RuntimeException("Configuration not found in the database");
}
} catch (SQLException | JsonProcessingException e) {
throw new RuntimeException("Error retrieving configuration from database", e);
}
return jsonConfigs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jembi.jempi.shared.config;

public class PostgresConfig {
private final String ip;
private final int port;
private final String db;
private final String user;
private final String password;

public PostgresConfig(final String ip, final int port, final String db, final String user, final String password) {
this.ip = ip;
this.port = port;
this.db = db;
this.user = user;
this.password = password;
}

// Existing getters...

/**
* Generates the JDBC URL for connecting to the PostgreSQL database.
*
* @return A string representing the JDBC URL.
*/
public String getJdbcUrl() {
return String.format("jdbc:postgresql://%s:%d/%s", ip, port, db);
}

// Add a static factory method to create PostgresConfig from environment variables
public static PostgresConfig fromEnv() {
String ip = System.getenv("POSTGRES_IP");
int port = Integer.parseInt(System.getenv("POSTGRES_PORT"));
String db = System.getenv("POSTGRES_DB");
String user = System.getenv("POSTGRES_USER");
String password = System.getenv("POSTGRES_PASSWORD");

return new PostgresConfig(ip, port, db, user, password);
}
}

0 comments on commit 77eb8f7

Please sign in to comment.