ApplicationProperties.java (1603B)
1 package com.wimdupont.service; 2 3 import java.io.IOException; 4 import java.security.InvalidParameterException; 5 import java.util.MissingResourceException; 6 import java.util.Properties; 7 8 public class ApplicationProperties { 9 10 private static ApplicationProperties instance; 11 private final String dirPath; 12 private final String secretFile; 13 14 public String getDirPath() { 15 return dirPath; 16 } 17 18 public String getSecretFile() { 19 return secretFile; 20 } 21 22 public static ApplicationProperties getInstance() { 23 if (instance == null) { 24 instance = new ApplicationProperties(); 25 } 26 27 return instance; 28 } 29 30 private ApplicationProperties(){ 31 var properties = loadProperties(); 32 dirPath = properties.getProperty("dir.path"); 33 secretFile = properties.getProperty("secret.file"); 34 35 if (dirPath == null | secretFile == null) { 36 throw new InvalidParameterException("Properties missing."); 37 } 38 } 39 40 private Properties loadProperties() { 41 final var properties = new Properties(); 42 var inputStream = getClass().getResourceAsStream("/application.properties"); 43 try { 44 if (inputStream == null) { 45 throw new IOException(); 46 } 47 properties.load(inputStream); 48 } catch (IOException e) { 49 throw new MissingResourceException( 50 "Missing application properties", 51 Properties.class.getSimpleName(), 52 "application.properties"); 53 } 54 return properties; 55 } 56 }