-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raw code for connect PSQL from LibShared
- Loading branch information
1 parent
71f5e9e
commit 77eb8f7
Showing
4 changed files
with
112 additions
and
19 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
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
81 changes: 63 additions & 18 deletions
81
JeMPI_Apps/JeMPI_LibShared/src/main/java/org/jembi/jempi/shared/config/Config.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 |
---|---|---|
@@ -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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
JeMPI_Apps/JeMPI_LibShared/src/main/java/org/jembi/jempi/shared/config/PostgresConfig.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,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); | ||
} | ||
} |