commit a0673eaa944125a46fb9d741d51389b54e1ab254
parent 1fcd4b1bfccbab54eb31e3849054f761893c35e1
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 23 Dec 2023 21:10:09 +0100
properties as singleton
Diffstat:
2 files changed, 56 insertions(+), 25 deletions(-)
diff --git a/src/main/java/com/wimdupont/service/ApplicationProperties.java b/src/main/java/com/wimdupont/service/ApplicationProperties.java
@@ -0,0 +1,56 @@
+package com.wimdupont.service;
+
+import java.io.IOException;
+import java.security.InvalidParameterException;
+import java.util.MissingResourceException;
+import java.util.Properties;
+
+public class PropertiesLoader {
+
+    private static PropertiesLoader instance;
+    private final String dirPath;
+    private final String secretFile;
+
+    public String getDirPath() {
+        return dirPath;
+    }
+
+    public String getSecretFile() {
+        return secretFile;
+    }
+
+    public static PropertiesLoader getInstance() {
+        if (instance == null) {
+            instance = new PropertiesLoader();
+        }
+
+        return instance;
+    }
+
+    private PropertiesLoader(){
+        var properties = loadProperties();
+        dirPath = properties.getProperty("dir.path");
+        secretFile = properties.getProperty("secret.file");
+
+        if (dirPath == null | secretFile == null) {
+            throw new InvalidParameterException("Properties missing.");
+        }
+    }
+
+    private Properties loadProperties() {
+        final var properties = new Properties();
+        var inputStream = getClass().getResourceAsStream("/application.properties");
+        try {
+            if (inputStream == null) {
+                throw new IOException();
+            }
+            properties.load(inputStream);
+        } catch (IOException e) {
+            throw new MissingResourceException(
+                    "Missing application properties",
+                    Properties.class.getSimpleName(),
+                    "application.properties");
+        }
+        return properties;
+    }
+}
diff --git a/src/main/java/com/wimdupont/service/PropertiesLoader.java b/src/main/java/com/wimdupont/service/PropertiesLoader.java
@@ -1,25 +0,0 @@
-package com.wimdupont.service;
-
-import java.io.IOException;
-import java.util.MissingResourceException;
-import java.util.Properties;
-
-public class PropertiesLoader {
-
-    public Properties loadProperties() {
-        final var properties = new Properties();
-        var inputStream = getClass().getResourceAsStream("/application.properties");
-        try {
-            if (inputStream == null) {
-                throw new IOException();
-            }
-            properties.load(inputStream);
-        } catch (IOException e) {
-            throw new MissingResourceException(
-                    "Missing application properties",
-                    Properties.class.getSimpleName(),
-                    "application.properties");
-        }
-        return properties;
-    }
-}