Skip to content

Commit

Permalink
fix(database): Restore reading environment database vars
Browse files Browse the repository at this point in the history
Signed-off-by: Helio Chissini de Castro <[email protected]>
  • Loading branch information
heliocastro committed Feb 7, 2024
1 parent 87f0c32 commit ef5cc01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> COUCH_DB_USERNAME;
private static final Optional<String> 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");
Expand All @@ -77,11 +83,9 @@ public static Supplier<HttpClient> 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;
}
Expand All @@ -98,11 +102,9 @@ public static Supplier<CloudantClient> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> COUCH_DB_USERNAME;
private static final Optional<String> COUCH_DB_PASSWORD;
Expand All @@ -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());
}
Expand Down

0 comments on commit ef5cc01

Please sign in to comment.