Skip to content

Commit 51e0bd6

Browse files
rlunarRoberto Luna Rojas
authored andcommitted
Read properties definition from a separate config file, and rename example file to reflect database-agnostic functionality
--------- Co-authored-by: Roberto Luna Rojas <rberoj+sns@amazon.com>
1 parent 699c5e5 commit 51e0bd6

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DB_CONNECTION_STRING=jdbc:aws-wrapper:postgresql://localhost:5432/dbname
2+
CACHE_RW_SERVER_ADDR=localhost:6379
3+
CACHE_RO_SERVER_ADDR=localhost:6380
4+
DB_USERNAME=postgres
5+
DB_PASSWORD=admin

examples/AWSDriverExample/src/main/java/software/amazon/PgConnectionWithCacheExample.java renamed to examples/AWSDriverExample/src/main/java/software/amazon/DatabaseConnectionWithCacheExample.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package software.amazon;
22

3+
import software.amazon.util.EnvLoader;
34
import java.sql.*;
45
import java.util.*;
56

6-
public class PgConnectionWithCacheExample {
7+
public class DatabaseConnectionWithCacheExample {
78

8-
private static final String CONNECTION_STRING = "jdbc:aws-wrapper:postgresql://dev-dsk-quchen-2a-3a165932.us-west-2.amazon.com:5432/postgres";
9-
private static final String CACHE_RW_SERVER_ADDR = "dev-dsk-quchen-2a-3a165932.us-west-2.amazon.com:6379";
10-
private static final String CACHE_RO_SERVER_ADDR = "dev-dsk-quchen-2a-3a165932.us-west-2.amazon.com:6380";
11-
private static final String USERNAME = "postgres";
12-
private static final String PASSWORD = "admin";
9+
private static final EnvLoader env = new EnvLoader();
10+
11+
private static final String DB_CONNECTION_STRING = env.get("DB_CONNECTION_STRING");
12+
private static final String CACHE_RW_SERVER_ADDR = env.get("CACHE_RW_SERVER_ADDR");
13+
private static final String CACHE_RO_SERVER_ADDR = env.get("CACHE_RO_SERVER_ADDR");
14+
private static final String USERNAME = env.get("DB_USERNAME");
15+
private static final String PASSWORD = env.get("DB_PASSWORD");
16+
private static final String USE_SSL = env.get("USE_SSL");
1317

1418
public static void main(String[] args) throws SQLException {
1519
final Properties properties = new Properties();
@@ -22,14 +26,15 @@ public static void main(String[] args) throws SQLException {
2226
properties.setProperty("wrapperPlugins", "dataRemoteCache");
2327
properties.setProperty("cacheEndpointAddrRw", CACHE_RW_SERVER_ADDR);
2428
properties.setProperty("cacheEndpointAddrRo", CACHE_RO_SERVER_ADDR);
29+
properties.setProperty("cacheUseSSL", USE_SSL); // "true" or "false"
2530
properties.setProperty("wrapperLogUnclosedConnections", "true");
2631
String queryStr = "select * from cinemas";
2732
String queryStr2 = "SELECT * from cinemas";
2833

2934
for (int i = 0 ; i < 5; i++) {
3035
// Create a new database connection and issue queries to it
3136
try {
32-
Connection conn = DriverManager.getConnection(CONNECTION_STRING, properties);
37+
Connection conn = DriverManager.getConnection(DB_CONNECTION_STRING, properties);
3338
Statement stmt = conn.createStatement();
3439
ResultSet rs = stmt.executeQuery(queryStr);
3540
ResultSet rs2 = stmt.executeQuery(queryStr2);
@@ -40,5 +45,4 @@ public static void main(String[] args) throws SQLException {
4045
}
4146
}
4247
}
43-
4448
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package software.amazon.util;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* A simple utility class to load environment variables from a .env file.
14+
*/
15+
public class EnvLoader {
16+
private final Map<String, String> envVars = new HashMap<>();
17+
18+
/**
19+
* Loads environment variables from a .env file in the current directory.
20+
*/
21+
public EnvLoader() {
22+
this(Paths.get(".env"));
23+
}
24+
25+
/**
26+
* Loads environment variables from the specified file path.
27+
*
28+
* @param envPath Path to the .env file
29+
*/
30+
public EnvLoader(Path envPath) {
31+
if (Files.exists(envPath)) {
32+
try (BufferedReader reader = new BufferedReader(new FileReader(envPath.toFile()))) {
33+
String line;
34+
while ((line = reader.readLine()) != null) {
35+
parseLine(line);
36+
}
37+
} catch (IOException e) {
38+
System.err.println("Error reading .env file: " + e.getMessage());
39+
}
40+
}
41+
}
42+
43+
private void parseLine(String line) {
44+
line = line.trim();
45+
// Skip empty lines and comments
46+
if (line.isEmpty() || line.startsWith("#")) {
47+
return;
48+
}
49+
50+
// Split on the first equals sign
51+
int delimiterPos = line.indexOf('=');
52+
if (delimiterPos > 0) {
53+
String key = line.substring(0, delimiterPos).trim();
54+
String value = line.substring(delimiterPos + 1).trim();
55+
56+
// Remove quotes if present
57+
if ((value.startsWith("\"") && value.endsWith("\"")) ||
58+
(value.startsWith("'") && value.endsWith("'"))) {
59+
value = value.substring(1, value.length() - 1);
60+
}
61+
62+
envVars.put(key, value);
63+
}
64+
}
65+
66+
/**
67+
* Gets the value of an environment variable.
68+
*
69+
* @param key The name of the environment variable
70+
* @return The value of the environment variable, or null if not found
71+
*/
72+
public String get(String key) {
73+
// First check the loaded .env file
74+
String value = envVars.get(key);
75+
76+
// If not found, check system environment variables
77+
if (value == null) {
78+
value = System.getenv(key);
79+
}
80+
81+
return value;
82+
}
83+
}

0 commit comments

Comments
 (0)