|
| 1 | +package com.github.tadayosi.torchserve.client; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.Properties; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +public class Configuration { |
| 11 | + |
| 12 | + private static final Logger LOG = LoggerFactory.getLogger(Configuration.class); |
| 13 | + |
| 14 | + public static final String TSC4J_PROPERTIES = "tsc4j.properties"; |
| 15 | + public static final String TSC4J_PREFIX = "tsc4j."; |
| 16 | + |
| 17 | + public static final String INFERENCE_KEY = "inference.key"; |
| 18 | + public static final String INFERENCE_ADDRESS = "inference.address"; |
| 19 | + public static final String INFERENCE_PORT = "inference.port"; |
| 20 | + |
| 21 | + public static final String MANAGEMENT_KEY = "management.key"; |
| 22 | + public static final String MANAGEMENT_ADDRESS = "management.address"; |
| 23 | + public static final String MANAGEMENT_PORT = "management.port"; |
| 24 | + |
| 25 | + public static final String METRICS_ADDRESS = "metrics.address"; |
| 26 | + public static final String METRICS_PORT = "metrics.port"; |
| 27 | + |
| 28 | + private Optional<String> inferenceKey; |
| 29 | + private Optional<String> inferenceAddress; |
| 30 | + private Optional<Integer> inferencePort; |
| 31 | + |
| 32 | + private Optional<String> managementKey; |
| 33 | + private Optional<String> managementAddress; |
| 34 | + private Optional<Integer> managementPort; |
| 35 | + |
| 36 | + private Optional<String> metricsAddress; |
| 37 | + private Optional<Integer> metricsPort; |
| 38 | + |
| 39 | + private Configuration() { |
| 40 | + Properties props = loadProperties(); |
| 41 | + |
| 42 | + this.inferenceKey = loadProperty(INFERENCE_KEY, props); |
| 43 | + this.inferenceAddress = loadProperty(INFERENCE_ADDRESS, props); |
| 44 | + this.inferencePort = loadProperty(INFERENCE_PORT, props).map(Integer::parseInt); |
| 45 | + this.managementKey = loadProperty(MANAGEMENT_KEY, props); |
| 46 | + this.managementAddress = loadProperty(MANAGEMENT_ADDRESS, props); |
| 47 | + this.managementPort = loadProperty(MANAGEMENT_PORT, props).map(Integer::parseInt); |
| 48 | + this.metricsAddress = loadProperty(METRICS_ADDRESS, props); |
| 49 | + this.metricsPort = loadProperty(METRICS_PORT, props).map(Integer::parseInt); |
| 50 | + } |
| 51 | + |
| 52 | + static Properties loadProperties() { |
| 53 | + Properties properties = new Properties(); |
| 54 | + try { |
| 55 | + InputStream is = Configuration.class.getClassLoader().getResourceAsStream(TSC4J_PROPERTIES); |
| 56 | + properties.load(is); |
| 57 | + } catch (Exception e) { |
| 58 | + // Ignore |
| 59 | + LOG.debug("Failed to load properties file: {}", e.getMessage()); |
| 60 | + } |
| 61 | + return properties; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Order of precedence: System properties > environment variables > properties file |
| 66 | + */ |
| 67 | + static Optional<String> loadProperty(String key, Properties properties) { |
| 68 | + String tsc4jKey = TSC4J_PREFIX + key; |
| 69 | + return Optional.ofNullable(System.getProperty(tsc4jKey)) |
| 70 | + .or(() -> Optional.ofNullable(System.getenv(tsc4jKey.toUpperCase().replace(".", "_")))) |
| 71 | + .or(() -> Optional.ofNullable(properties.getProperty(key))); |
| 72 | + } |
| 73 | + |
| 74 | + public static Configuration load() { |
| 75 | + return new Configuration(); |
| 76 | + } |
| 77 | + |
| 78 | + public Optional<String> getInferenceKey() { |
| 79 | + return inferenceKey; |
| 80 | + } |
| 81 | + |
| 82 | + public Optional<String> getInferenceAddress() { |
| 83 | + return inferenceAddress; |
| 84 | + } |
| 85 | + |
| 86 | + public Optional<Integer> getInferencePort() { |
| 87 | + return inferencePort; |
| 88 | + } |
| 89 | + |
| 90 | + public Optional<String> getManagementKey() { |
| 91 | + return managementKey; |
| 92 | + } |
| 93 | + |
| 94 | + public Optional<String> getManagementAddress() { |
| 95 | + return managementAddress; |
| 96 | + } |
| 97 | + |
| 98 | + public Optional<Integer> getManagementPort() { |
| 99 | + return managementPort; |
| 100 | + } |
| 101 | + |
| 102 | + public Optional<String> getMetricsAddress() { |
| 103 | + return metricsAddress; |
| 104 | + } |
| 105 | + |
| 106 | + public Optional<Integer> getMetricsPort() { |
| 107 | + return metricsPort; |
| 108 | + } |
| 109 | +} |
0 commit comments