This commit is contained in:
Radek Davidek 2025-10-01 15:13:48 +02:00
parent 2209854f92
commit 41a78b282f

View File

@ -1,96 +1,162 @@
package cz.trask.apioperator.config; package cz.trask.apioperator.config;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.util.Properties; import java.util.Properties;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class ConfigManager { public final class ConfigManager {
private static Logger log = LogManager.getLogger(ConfigManager.class); /* -------------------------------------------------------------------- */
/* LOGIC & CONSTANTS */
/* -------------------------------------------------------------------- */
private static ConfigManager instance = null; private static final Logger log = LogManager.getLogger(ConfigManager.class);
private final static String PROPERTY_FILENAME = "api-operator.properties"; private static final String PROPERTY_FILENAME = "api-operator.properties";
// Property fields // SOURCE
private String sourceRegistrationApiUrl; public static final String PROP_SOURCE_REGISTRATION_API_URL = "SOURCE_REGISTRATION_API_URL";
private String sourcePublisherApiUrl; public static final String PROP_SOURCE_PUBLISHER_API_URL = "SOURCE_PUBLISHER_API_URL";
private String sourceDevportalApiUrl; public static final String PROP_SOURCE_DEVPORTAL_API_URL = "SOURCE_DEVPORTAL_API_URL";
private String sourcePublisherTokenUrl; public static final String PROP_SOURCE_PUBLISHER_TOKEN_URL = "SOURCE_PUBLISHER_TOKEN_URL";
private String sourceWso2User; public static final String PROP_SOURCE_WSO2_USER = "SOURCE_WSO2_USER";
private String targetRegistrationApiUrl;
private String targetPublisherApiUrl; // TARGET
private String targetDevportalApiUrl; public static final String PROP_TARGET_REGISTRATION_API_URL = "TARGET_REGISTRATION_API_URL";
private String targetPublisherTokenUrl; public static final String PROP_TARGET_PUBLISHER_API_URL = "TARGET_PUBLISHER_API_URL";
private String targetWso2User; public static final String PROP_TARGET_DEVPORTAL_API_URL = "TARGET_DEVPORTAL_API_URL";
private String truststorePath; public static final String PROP_TARGET_PUBLISHER_TOKEN_URL = "TARGET_PUBLISHER_TOKEN_URL";
private String truststorePassword; public static final String PROP_TARGET_WSO2_USER = "TARGET_WSO2_USER";
private String publisherUrlPattern;
private String devportalUrlPattern; // TRUSTSTORE
private String apicurioApiUrl; public static final String PROP_TRUSTSTORE_PATH = "TRUSTSTORE_PATH";
private String defaultApiGroup; public static final String PROP_TRUSTSTORE_PASSWORD = "TRUSTSTORE_PASSWORD";
private int maxThreads;
// URL PATTERNS
public static final String PROP_PUBLISHER_URL_PATTERN = "PUBLISHER_URL_PATTERN";
public static final String PROP_DEVPORTAL_URL_PATTERN = "DEVPORTAL_URL_PATTERN";
// APICURIO & GROUP
public static final String PROP_APICURIO_API_URL = "APICURIO_API_URL";
public static final String PROP_DEFAULT_API_GROUP = "DEFAULT_API_GROUP";
// MAX THREADS
public static final String PROP_MAX_THREADS = "MAX_THREADS";
/* -------------------------------------------------------------------- */
/* CONFIGURATION FIELDS */
/* -------------------------------------------------------------------- */
private final String sourceRegistrationApiUrl;
private final String sourcePublisherApiUrl;
private final String sourceDevportalApiUrl;
private final String sourcePublisherTokenUrl;
private final String sourceWso2User;
private final String targetRegistrationApiUrl;
private final String targetPublisherApiUrl;
private final String targetDevportalApiUrl;
private final String targetPublisherTokenUrl;
private final String targetWso2User;
private final String truststorePath;
private final String truststorePassword;
private final String publisherUrlPattern;
private final String devportalUrlPattern;
private final String apicurioApiUrl;
private final String defaultApiGroup;
private final int maxThreads;
/* -------------------------------------------------------------------- */
/* SINGLETON lazyinitializationon-demand holder */
/* -------------------------------------------------------------------- */
private ConfigManager() { private ConfigManager() {
Properties properties = new Properties(); Properties props = new Properties();
log.info("Loading property file '{}'", PROPERTY_FILENAME);
log.info("Loading property file '" + PROPERTY_FILENAME + "'");
InputStream in = null; InputStream in = null;
try { try {
File f = new File(PROPERTY_FILENAME); File extFile = new File(PROPERTY_FILENAME);
log.info("External property file location: " + f.getAbsolutePath()); if (extFile.exists() && extFile.isFile()) {
in = new FileInputStream(f); log.info("External property file found: {}", extFile.getAbsolutePath());
log.info("External property file found."); in = Files.newInputStream(extFile.toPath());
} else {
log.debug("External property file not found.");
}
} catch (Exception e) { } catch (Exception e) {
log.info("External property file not found."); log.warn("Error while accessing external property file", e);
}
if (in == null) {
try { try {
ClassLoader loader = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
in = loader.getResourceAsStream(PROPERTY_FILENAME); in = cl.getResourceAsStream(PROPERTY_FILENAME);
log.info("Internal property file found."); if (in != null) {
} catch (Exception ex) { log.info("Internal property file found on classpath.");
log.info("Internal property file not found."); } else {
log.warn("Property file '{}' not found on classpath.", PROPERTY_FILENAME);
}
} catch (Exception e) {
log.error("Unable to load property file from classpath", e);
} }
} }
try { try (InputStream input = in) {
properties.load(in); if (input != null) {
props.load(input);
log.info("Property file loaded successfully.");
} else {
log.warn("No property source available; proceeding with defaults where possible.");
}
} catch (Exception e) { } catch (Exception e) {
log.error("Cannot load property file.", e); log.error("Cannot load property file.", e);
} }
sourceRegistrationApiUrl = properties.getProperty("SOURCE_REGISTRATION_API_URL"); sourceRegistrationApiUrl = props.getProperty(PROP_SOURCE_REGISTRATION_API_URL);
sourcePublisherApiUrl = properties.getProperty("SOURCE_PUBLISHER_API_URL"); sourcePublisherApiUrl = props.getProperty(PROP_SOURCE_PUBLISHER_API_URL);
sourceDevportalApiUrl = properties.getProperty("SOURCE_DEVPORTAL_API_URL"); sourceDevportalApiUrl = props.getProperty(PROP_SOURCE_DEVPORTAL_API_URL);
sourcePublisherTokenUrl = properties.getProperty("SOURCE_PUBLISHER_TOKEN_URL"); sourcePublisherTokenUrl = props.getProperty(PROP_SOURCE_PUBLISHER_TOKEN_URL);
sourceWso2User = properties.getProperty("SOURCE_WSO2_USER"); sourceWso2User = props.getProperty(PROP_SOURCE_WSO2_USER);
targetRegistrationApiUrl = properties.getProperty("TARGET_REGISTRATION_API_URL");
targetPublisherApiUrl = properties.getProperty("TARGET_PUBLISHER_API_URL");
targetDevportalApiUrl = properties.getProperty("TARGET_DEVPORTAL_API_URL");
targetPublisherTokenUrl = properties.getProperty("TARGET_PUBLISHER_TOKEN_URL");
targetWso2User = properties.getProperty("TARGET_WSO2_USER");
truststorePath = properties.getProperty("TRUSTSTORE_PATH");
truststorePassword = properties.getProperty("TRUSTSTORE_PASSWORD");
publisherUrlPattern = properties.getProperty("PUBLISHER_URL_PATTERN");
devportalUrlPattern = properties.getProperty("DEVPORTAL_URL_PATTERN");
apicurioApiUrl = properties.getProperty("APICURIO_API_URL");
defaultApiGroup = properties.getProperty("DEFAULT_API_GROUP");
maxThreads = Integer.parseInt(properties.getProperty("MAX_THREADS", "10"));
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);
truststorePath = props.getProperty(PROP_TRUSTSTORE_PATH);
truststorePassword = props.getProperty(PROP_TRUSTSTORE_PASSWORD);
publisherUrlPattern = props.getProperty(PROP_PUBLISHER_URL_PATTERN);
devportalUrlPattern = props.getProperty(PROP_DEVPORTAL_URL_PATTERN);
apicurioApiUrl = props.getProperty(PROP_APICURIO_API_URL);
defaultApiGroup = props.getProperty(PROP_DEFAULT_API_GROUP);
maxThreads = Integer.parseInt(props.getProperty(PROP_MAX_THREADS, "10"));
} }
public static synchronized ConfigManager getInstance() { /** Lazilyinitialized singleton instance. */
if (instance == null) { private static final class Holder {
if (instance == null) { static final ConfigManager INSTANCE = new ConfigManager();
instance = new ConfigManager();
} }
public static ConfigManager getInstance() {
return Holder.INSTANCE;
} }
return instance;
} /* -------------------------------------------------------------------- */
/* PUBLIC GETTERS */
/* -------------------------------------------------------------------- */
public String getSourceRegistrationApiUrl() { public String getSourceRegistrationApiUrl() {
return sourceRegistrationApiUrl; return sourceRegistrationApiUrl;