refactor
This commit is contained in:
parent
2209854f92
commit
41a78b282f
@ -1,96 +1,162 @@
|
||||
package cz.trask.apioperator.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
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
|
||||
private String sourceRegistrationApiUrl;
|
||||
private String sourcePublisherApiUrl;
|
||||
private String sourceDevportalApiUrl;
|
||||
private String sourcePublisherTokenUrl;
|
||||
private String sourceWso2User;
|
||||
private String targetRegistrationApiUrl;
|
||||
private String targetPublisherApiUrl;
|
||||
private String targetDevportalApiUrl;
|
||||
private String targetPublisherTokenUrl;
|
||||
private String targetWso2User;
|
||||
private String truststorePath;
|
||||
private String truststorePassword;
|
||||
private String publisherUrlPattern;
|
||||
private String devportalUrlPattern;
|
||||
private String apicurioApiUrl;
|
||||
private String defaultApiGroup;
|
||||
private int maxThreads;
|
||||
// SOURCE
|
||||
public static final String PROP_SOURCE_REGISTRATION_API_URL = "SOURCE_REGISTRATION_API_URL";
|
||||
public static final String PROP_SOURCE_PUBLISHER_API_URL = "SOURCE_PUBLISHER_API_URL";
|
||||
public static final String PROP_SOURCE_DEVPORTAL_API_URL = "SOURCE_DEVPORTAL_API_URL";
|
||||
public static final String PROP_SOURCE_PUBLISHER_TOKEN_URL = "SOURCE_PUBLISHER_TOKEN_URL";
|
||||
public static final String PROP_SOURCE_WSO2_USER = "SOURCE_WSO2_USER";
|
||||
|
||||
// TARGET
|
||||
public static final String PROP_TARGET_REGISTRATION_API_URL = "TARGET_REGISTRATION_API_URL";
|
||||
public static final String PROP_TARGET_PUBLISHER_API_URL = "TARGET_PUBLISHER_API_URL";
|
||||
public static final String PROP_TARGET_DEVPORTAL_API_URL = "TARGET_DEVPORTAL_API_URL";
|
||||
public static final String PROP_TARGET_PUBLISHER_TOKEN_URL = "TARGET_PUBLISHER_TOKEN_URL";
|
||||
public static final String PROP_TARGET_WSO2_USER = "TARGET_WSO2_USER";
|
||||
|
||||
// TRUSTSTORE
|
||||
public static final String PROP_TRUSTSTORE_PATH = "TRUSTSTORE_PATH";
|
||||
public static final String PROP_TRUSTSTORE_PASSWORD = "TRUSTSTORE_PASSWORD";
|
||||
|
||||
// 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 – lazy‑initialization‑on-demand holder */
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
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;
|
||||
|
||||
try {
|
||||
File f = new File(PROPERTY_FILENAME);
|
||||
log.info("External property file location: " + f.getAbsolutePath());
|
||||
in = new FileInputStream(f);
|
||||
log.info("External property file found.");
|
||||
File extFile = new File(PROPERTY_FILENAME);
|
||||
if (extFile.exists() && extFile.isFile()) {
|
||||
log.info("External property file found: {}", extFile.getAbsolutePath());
|
||||
in = Files.newInputStream(extFile.toPath());
|
||||
} else {
|
||||
log.debug("External property file not found.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info("External property file not found.");
|
||||
log.warn("Error while accessing external property file", e);
|
||||
}
|
||||
|
||||
if (in == null) {
|
||||
try {
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
in = loader.getResourceAsStream(PROPERTY_FILENAME);
|
||||
log.info("Internal property file found.");
|
||||
} catch (Exception ex) {
|
||||
log.info("Internal property file not found.");
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
in = cl.getResourceAsStream(PROPERTY_FILENAME);
|
||||
if (in != null) {
|
||||
log.info("Internal property file found on classpath.");
|
||||
} 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 {
|
||||
properties.load(in);
|
||||
try (InputStream input = 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) {
|
||||
log.error("Cannot load property file.", e);
|
||||
}
|
||||
|
||||
sourceRegistrationApiUrl = properties.getProperty("SOURCE_REGISTRATION_API_URL");
|
||||
sourcePublisherApiUrl = properties.getProperty("SOURCE_PUBLISHER_API_URL");
|
||||
sourceDevportalApiUrl = properties.getProperty("SOURCE_DEVPORTAL_API_URL");
|
||||
sourcePublisherTokenUrl = properties.getProperty("SOURCE_PUBLISHER_TOKEN_URL");
|
||||
sourceWso2User = properties.getProperty("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"));
|
||||
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);
|
||||
|
||||
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() {
|
||||
if (instance == null) {
|
||||
if (instance == null) {
|
||||
instance = new ConfigManager();
|
||||
/** Lazily‑initialized singleton instance. */
|
||||
private static final class Holder {
|
||||
static final ConfigManager INSTANCE = new ConfigManager();
|
||||
}
|
||||
|
||||
public static ConfigManager getInstance() {
|
||||
return Holder.INSTANCE;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* PUBLIC GETTERS */
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
public String getSourceRegistrationApiUrl() {
|
||||
return sourceRegistrationApiUrl;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user