From ef5cc01429d3cca2f38efdc21233ea1ccf9ea596 Mon Sep 17 00:00:00 2001 From: Helio Chissini de Castro Date: Tue, 23 Jan 2024 13:30:17 +0100 Subject: [PATCH] fix(database): Restore reading environment database vars Signed-off-by: Helio Chissini de Castro --- .../datahandler/common/DatabaseSettings.java | 34 ++++++++++--------- .../couchdb/DatabaseTestProperties.java | 7 +++- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/DatabaseSettings.java b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/DatabaseSettings.java index dae27f28a3..80e41e3dc6 100644 --- a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/DatabaseSettings.java +++ b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/DatabaseSettings.java @@ -11,6 +11,7 @@ import java.net.MalformedURLException; import java.net.URL; +import java.util.Optional; import java.util.Properties; import java.util.function.Supplier; @@ -49,17 +50,22 @@ public class DatabaseSettings { public static final int LUCENE_SEARCH_LIMIT; public static final boolean LUCENE_LEADING_WILDCARD; - private static final String COUCH_DB_USERNAME; - private static final String COUCH_DB_PASSWORD; + private static final Optional COUCH_DB_USERNAME; + private static final Optional COUCH_DB_PASSWORD; static { Properties props = CommonUtils.loadProperties(DatabaseSettings.class, PROPERTIES_FILE_PATH); - COUCH_DB_URL = props.getProperty("couchdb.url", "http://localhost:5984"); - COUCH_DB_LUCENE_URL = props.getProperty("couchdb.lucene.url", "http://localhost:8080/couchdb-lucene"); + // Try ENV if set first + COUCH_DB_URL = System.getenv("COUCHDB_URL") != null ? System.getenv("COUCHDB_URL") + : props.getProperty("couchdb.url", "http://localhost:5984"); + COUCH_DB_USERNAME = Optional.ofNullable(System.getenv("COUCHDB_USER") != null ? System.getenv("COUCHDB_USER") + : props.getProperty("couchdb.user", "")); + COUCH_DB_PASSWORD = Optional + .ofNullable(System.getenv("COUCHDB_PASSWORD") != null ? System.getenv("COUCHDB_PASSWORD") + : props.getProperty("couchdb.password", "")); COUCH_DB_DATABASE = props.getProperty("couchdb.database", "sw360db"); - COUCH_DB_USERNAME = props.getProperty("couchdb.user", ""); - COUCH_DB_PASSWORD = props.getProperty("couchdb.password", ""); + COUCH_DB_LUCENE_URL = props.getProperty("couchdb.lucene.url", "http://localhost:8080/couchdb-lucene"); COUCH_DB_ATTACHMENTS = props.getProperty("couchdb.attachments", "sw360attachments"); COUCH_DB_CHANGE_LOGS = props.getProperty("couchdb.change_logs", "sw360changelogs"); COUCH_DB_CONFIG = props.getProperty("couchdb.config", "sw360config"); @@ -77,11 +83,9 @@ public static Supplier getConfiguredHttpClient() throws MalformedURL if (!COUCH_DB_CACHE) { httpClientBuilder.caching(false); } - if (!"".equals(COUCH_DB_USERNAME)) { - httpClientBuilder.username(COUCH_DB_USERNAME); - } - if (!"".equals(COUCH_DB_PASSWORD)) { - httpClientBuilder.password(COUCH_DB_PASSWORD); + if (COUCH_DB_USERNAME.isPresent() && COUCH_DB_PASSWORD.isPresent()) { + httpClientBuilder.username(COUCH_DB_USERNAME.get()); + httpClientBuilder.password(COUCH_DB_PASSWORD.get()); } return httpClientBuilder::build; } @@ -98,11 +102,9 @@ public static Supplier getConfiguredClient() { } try { clientBuilder = ClientBuilder.url(new URL(COUCH_DB_URL)).gsonBuilder(gson); - if (!"".equals(COUCH_DB_USERNAME)) { - clientBuilder.username(COUCH_DB_USERNAME); - } - if (!"".equals(COUCH_DB_PASSWORD)) { - clientBuilder.password(COUCH_DB_PASSWORD); + if (COUCH_DB_USERNAME.isPresent() && COUCH_DB_PASSWORD.isPresent()) { + clientBuilder.username(COUCH_DB_USERNAME.get()); + clientBuilder.password(COUCH_DB_PASSWORD.get()); } } catch (MalformedURLException e) { log.error("Error creating client", e); diff --git a/libraries/datahandler/src/test/java/org/eclipse/sw360/datahandler/couchdb/DatabaseTestProperties.java b/libraries/datahandler/src/test/java/org/eclipse/sw360/datahandler/couchdb/DatabaseTestProperties.java index f82063e1f2..06c71990cd 100644 --- a/libraries/datahandler/src/test/java/org/eclipse/sw360/datahandler/couchdb/DatabaseTestProperties.java +++ b/libraries/datahandler/src/test/java/org/eclipse/sw360/datahandler/couchdb/DatabaseTestProperties.java @@ -29,6 +29,7 @@ public class DatabaseTestProperties { private static final String COUCH_DB_URL; public static final String COUCH_DB_DATABASE; + public static final boolean COUCH_DB_CACHE; private static final Optional COUCH_DB_USERNAME; private static final Optional COUCH_DB_PASSWORD; @@ -40,11 +41,15 @@ public class DatabaseTestProperties { COUCH_DB_DATABASE = props.getProperty("couchdb.database", "sw360_test_db"); COUCH_DB_USERNAME = Optional.ofNullable(props.getProperty("couchdb.user", "")); COUCH_DB_PASSWORD = Optional.ofNullable(props.getProperty("couchdb.password", "")); + COUCH_DB_CACHE = Boolean.parseBoolean(props.getProperty("couchdb.cache", "true")); } public static HttpClient getConfiguredHttpClient() throws MalformedURLException { StdHttpClient.Builder httpClientBuilder = new StdHttpClient.Builder().url(COUCH_DB_URL); - if(COUCH_DB_USERNAME.isPresent() && COUCH_DB_PASSWORD.isPresent()) { + if (!COUCH_DB_CACHE) { + httpClientBuilder.caching(false); + } + if (COUCH_DB_USERNAME.isPresent() && COUCH_DB_PASSWORD.isPresent()) { httpClientBuilder.username(COUCH_DB_USERNAME.get()); httpClientBuilder.password(COUCH_DB_PASSWORD.get()); }