From 742e848040cdf3fcfdc028879d83f233bdf0d97c Mon Sep 17 00:00:00 2001 From: Radek Davidek Date: Wed, 1 Oct 2025 16:53:27 +0200 Subject: [PATCH] refactor config --- .../cz/trask/apioperator/AbstractProcess.java | 21 ++++--- .../apioperator/config/ConfigManager.java | 58 ++++++++++++------- .../cz/trask/apioperator/impl/Import.java | 3 - 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/main/java/cz/trask/apioperator/AbstractProcess.java b/src/main/java/cz/trask/apioperator/AbstractProcess.java index 9723791..3eaa80b 100644 --- a/src/main/java/cz/trask/apioperator/AbstractProcess.java +++ b/src/main/java/cz/trask/apioperator/AbstractProcess.java @@ -32,6 +32,8 @@ public abstract class AbstractProcess { protected Gson gson; + protected ConfigManager config = ConfigManager.getInstance(); + protected AbstractProcess() { gson = new GsonBuilder().create(); @@ -45,18 +47,21 @@ public abstract class AbstractProcess { }); } - protected static String getTrustStorePath() { - String path = System.getProperty("user.dir") + File.separatorChar - + ConfigManager.getInstance().getTruststorePath(); - if (!new File(path).canRead()) - return null; + protected String getTrustStorePath() { + String path = config.getTruststorePath(); + if (!new File(path).canRead()) { + path = System.getProperty("user.dir") + File.separatorChar + config.getTruststorePath(); + if (!new File(path).canRead()) { + return null; + } + } return path; } - protected static void setTrustStoreCredentials() { + protected void setTrustStoreCredentials() { log.info(getTrustStorePath()); System.setProperty("javax.net.ssl.trustStore", getTrustStorePath()); - System.setProperty("javax.net.ssl.trustStorePassword", ConfigManager.getInstance().getTruststorePassword()); + System.setProperty("javax.net.ssl.trustStorePassword", config.getTruststorePassword()); } /** @@ -264,7 +269,7 @@ public abstract class AbstractProcess { return resp; } - + /** * Retrieve the list of APIs by name. * diff --git a/src/main/java/cz/trask/apioperator/config/ConfigManager.java b/src/main/java/cz/trask/apioperator/config/ConfigManager.java index 37eee4a..f4ee597 100644 --- a/src/main/java/cz/trask/apioperator/config/ConfigManager.java +++ b/src/main/java/cz/trask/apioperator/config/ConfigManager.java @@ -74,12 +74,16 @@ public final class ConfigManager { private final int maxThreads; + private static volatile ConfigManager INSTANCE; + + /** Všechny načtené hodnoty. */ + private final Properties props = new Properties(); + /* -------------------------------------------------------------------- */ /* SINGLETON – lazy‑initialization‑on-demand holder */ /* -------------------------------------------------------------------- */ private ConfigManager() { - Properties props = new Properties(); log.info("Loading property file '{}'", PROPERTY_FILENAME); @@ -121,37 +125,47 @@ public final class ConfigManager { log.error("Cannot load property file.", e); } - sourceRegistrationApiUrl = props.getProperty(PROP_SOURCE_REGISTRATION_API_URL); - sourcePublisherApiUrl = props.getProperty(PROP_SOURCE_PUBLISHER_API_URL); - sourceDevportalApiUrl = props.getProperty(PROP_SOURCE_DEVPORTAL_API_URL); - sourcePublisherTokenUrl = props.getProperty(PROP_SOURCE_PUBLISHER_TOKEN_URL); - sourceWso2User = props.getProperty(PROP_SOURCE_WSO2_USER); + sourceRegistrationApiUrl = getRequired(PROP_SOURCE_REGISTRATION_API_URL); + sourcePublisherApiUrl = getRequired(PROP_SOURCE_PUBLISHER_API_URL); + sourceDevportalApiUrl = getRequired(PROP_SOURCE_DEVPORTAL_API_URL); + sourcePublisherTokenUrl = getRequired(PROP_SOURCE_PUBLISHER_TOKEN_URL); + sourceWso2User = getRequired(PROP_SOURCE_WSO2_USER); - targetRegistrationApiUrl = props.getProperty(PROP_TARGET_REGISTRATION_API_URL); - targetPublisherApiUrl = props.getProperty(PROP_TARGET_PUBLISHER_API_URL); - targetDevportalApiUrl = props.getProperty(PROP_TARGET_DEVPORTAL_API_URL); - targetPublisherTokenUrl = props.getProperty(PROP_TARGET_PUBLISHER_TOKEN_URL); - targetWso2User = props.getProperty(PROP_TARGET_WSO2_USER); + targetRegistrationApiUrl = getRequired(PROP_TARGET_REGISTRATION_API_URL); + targetPublisherApiUrl = getRequired(PROP_TARGET_PUBLISHER_API_URL); + targetDevportalApiUrl = getRequired(PROP_TARGET_DEVPORTAL_API_URL); + targetPublisherTokenUrl = getRequired(PROP_TARGET_PUBLISHER_TOKEN_URL); + targetWso2User = getRequired(PROP_TARGET_WSO2_USER); - truststorePath = props.getProperty(PROP_TRUSTSTORE_PATH); - truststorePassword = props.getProperty(PROP_TRUSTSTORE_PASSWORD); + truststorePath = getRequired(PROP_TRUSTSTORE_PATH); + truststorePassword = getRequired(PROP_TRUSTSTORE_PASSWORD); - publisherUrlPattern = props.getProperty(PROP_PUBLISHER_URL_PATTERN); - devportalUrlPattern = props.getProperty(PROP_DEVPORTAL_URL_PATTERN); + publisherUrlPattern = getRequired(PROP_PUBLISHER_URL_PATTERN); + devportalUrlPattern = getRequired(PROP_DEVPORTAL_URL_PATTERN); - apicurioApiUrl = props.getProperty(PROP_APICURIO_API_URL); - defaultApiGroup = props.getProperty(PROP_DEFAULT_API_GROUP); + apicurioApiUrl = getRequired(PROP_APICURIO_API_URL); + defaultApiGroup = getRequired(PROP_DEFAULT_API_GROUP); maxThreads = Integer.parseInt(props.getProperty(PROP_MAX_THREADS, "10")); } - /** Lazily‑initialized singleton instance. */ - private static final class Holder { - static final ConfigManager INSTANCE = new ConfigManager(); + public static ConfigManager getInstance() { + if (INSTANCE == null) { + synchronized (ConfigManager.class) { + if (INSTANCE == null) { + INSTANCE = new ConfigManager(); + } + } + } + return INSTANCE; } - public static ConfigManager getInstance() { - return Holder.INSTANCE; + private String getRequired(String key) { + String value = props.getProperty(key); + if (value == null) { + throw new IllegalStateException("Missing required property: " + key); + } + return value.trim(); } /* -------------------------------------------------------------------- */ diff --git a/src/main/java/cz/trask/apioperator/impl/Import.java b/src/main/java/cz/trask/apioperator/impl/Import.java index eda33ed..9723827 100644 --- a/src/main/java/cz/trask/apioperator/impl/Import.java +++ b/src/main/java/cz/trask/apioperator/impl/Import.java @@ -17,7 +17,6 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import cz.trask.apioperator.AbstractProcess; -import cz.trask.apioperator.config.ConfigManager; import cz.trask.apioperator.model.APIInfo; import cz.trask.apioperator.model.APIList; import cz.trask.apioperator.model.HttpResponse; @@ -44,11 +43,9 @@ public class Import extends AbstractProcess { StartParameters sp; RegistryClient client; static int i = 1; - private ConfigManager config; public Import(StartParameters sp) throws Exception { this.sp = sp; - this.config = ConfigManager.getInstance(); client = RegistryClientFactory.create(config.getApicurioApiUrl()); }