ApplicationProperties.java (1801B)
1 package com.wimdupont.config; 2 3 import java.io.IOException; 4 import java.io.InputStream; 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 couchdbUrl; 12 private final String dictionaryClientUrl; 13 private final String wordCsvDir; 14 15 public String getDictionaryClientUrl() { 16 return dictionaryClientUrl; 17 } 18 19 public String getCouchdbUrl() { 20 return couchdbUrl; 21 } 22 23 public String getWordCsvDir() { 24 return wordCsvDir; 25 } 26 27 private ApplicationProperties() { 28 Properties properties = loadProperties(); 29 couchdbUrl = properties.getProperty("couchdb.connection-url"); 30 dictionaryClientUrl = properties.getProperty("dictionary.client.connection-url"); 31 wordCsvDir = properties.getProperty("csv.dir"); 32 if (couchdbUrl == null | dictionaryClientUrl == null | wordCsvDir == null) 33 throw new RuntimeException("Properties missing."); 34 } 35 36 public static ApplicationProperties getInstance() { 37 if (instance == null) { 38 instance = new ApplicationProperties(); 39 } 40 41 return instance; 42 } 43 44 private Properties loadProperties() { 45 final Properties properties = new Properties(); 46 InputStream inputStream = getClass().getResourceAsStream("/application.properties"); 47 try { 48 if (inputStream == null) { 49 throw new IOException(); 50 } 51 properties.load(inputStream); 52 } catch (IOException e) { 53 throw new MissingResourceException("Missing application properties", Properties.class.getSimpleName(), "application.properties"); 54 } 55 return properties; 56 } 57 }