001/* Copyright (C) 2014 konik.io 002 * 003 * This file is part of the Konik library. 004 * 005 * The Konik library is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * The Konik library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package io.konik; 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.util.Map.Entry; 023import java.util.Properties; 024import java.util.logging.Logger; 025 026import static java.util.logging.Level.CONFIG; 027 028/** 029 * The Global Konik Configuration. 030 * 031 * Try to load Konik Configuration from file `io.konik.configuration.properties`. 032 * System properties provided with the +-Dio.konik*+ or ```System.setProperties("io.konik*")`` will override the file 033 * content. 034 * 035 */ 036public enum Configuration { 037 038 /** The singleton configuration instance. */ 039 INSTANCE; 040 041 private static final Logger LOG = Logger.getLogger(Configuration.class.getName()); 042 private final Properties properties; 043 044 Configuration() { 045 properties = new Properties(); 046 loadPropertiesFromFile(); 047 overwriteWithSystemProperties(); 048 } 049 050 void reload(){ 051 loadPropertiesFromFile(); 052 overwriteWithSystemProperties(); 053 } 054 055 private void loadPropertiesFromFile() { 056 String fileName = Configuration.class.getName().toLowerCase(); 057 InputStream propertiesStream = this.getClass().getResourceAsStream("/" + fileName + ".properties"); 058 if (propertiesStream != null) { 059 try { 060 properties.load(propertiesStream); 061 } catch (IOException e) { 062 LOG.log(CONFIG, "could not load properties file" + fileName + " from classpath", e); 063 } 064 } 065 } 066 067 private void overwriteWithSystemProperties() { 068 for (Entry<Object, Object> sysProperty : System.getProperties().entrySet()) { 069 if (sysProperty.getKey() instanceof String && ((String) sysProperty.getKey()).startsWith("io.konik")) { 070 properties.put(sysProperty.getKey(), sysProperty.getValue()); 071 } 072 } 073 } 074 075 /** 076 * Searches for the property with the specified key in this property list. 077 * If the key is not found in this property list, the default property list, 078 * and its defaults, recursively, are then checked. The method returns 079 * <code>null</code> if the property is not found. 080 * 081 * @param key the property key. 082 * @return the value in this property list with the specified key value. 083 * @see Configuration#getProperty(String, String) 084 */ 085 public String getProperty(String key) { 086 return properties.getProperty(key); 087 } 088 089 /** 090 * Searches for the property with the specified key in this property list. 091 * If the key is not found in this property list, the default property list, 092 * and its defaults, recursively, are then checked. The method returns the 093 * default value argument if the property is not found. 094 * 095 * @param key the hashtable key. 096 * @param defaultValue a default value. 097 * 098 * @return the value in this property list with the specified key value. 099 * @see Configuration#getProperty(String) 100 */ 101 public String getProperty(String key, String defaultValue) { 102 return properties.getProperty(key, defaultValue); 103 } 104 105 /** 106 * Indicate of Konik should strip the trailing zeros in all amounts. 107 * 108 * 109 * @return true if strip trailing zeros is active (default is false) 110 */ 111 public boolean stripTrailingZeros() { 112 return Boolean.parseBoolean(getProperty("io.konik.stripTrailingZeros", "false")); 113 } 114 115 @Override 116 public String toString() { 117 return "Konik Configuration dump\n" + properties.toString(); 118 } 119}