001 package com.khubla.pragmatach.framework.configuration;
002
003 import java.io.InputStream;
004 import java.util.Enumeration;
005 import java.util.HashMap;
006 import java.util.Map;
007 import java.util.Properties;
008
009 import com.khubla.pragmatach.framework.api.PragmatachException;
010
011 /**
012 * @author tome
013 */
014 public class PropertiesFileConfigurationImpl extends BaseConfiguration {
015 /**
016 * file
017 */
018 private static final String CONFIGURATION_FILE = "/pragmatach.properties";
019 /**
020 * properties
021 */
022 private static Properties properties;
023
024 @Override
025 public Map<String, String> getAll() throws PragmatachException {
026 readProperties();
027 final Map<String, String> ret = new HashMap<String, String>();
028 final Enumeration<Object> enumer = properties.keys();
029 while (enumer.hasMoreElements()) {
030 final String k = (String) enumer.nextElement();
031 ret.put(k, properties.getProperty(k));
032 }
033 return ret;
034 }
035
036 @Override
037 public Object getObject(String name) throws PragmatachException {
038 return resolveObject(properties.getProperty(name));
039 }
040
041 @Override
042 public String getParameter(String name) throws PragmatachException {
043 readProperties();
044 return resolveString(properties.getProperty(name));
045 }
046
047 /**
048 * read properties
049 */
050 private void readProperties() {
051 try {
052 if (null == properties) {
053 properties = new Properties();
054 InputStream is = null;
055 try {
056 is = PropertiesFileConfigurationImpl.class
057 .getResourceAsStream(CONFIGURATION_FILE);
058 if (null != is) {
059 properties.load(is);
060 }
061 } finally {
062 if (null != is) {
063 is.close();
064 }
065 }
066 }
067 } catch (final Exception e) {
068 e.printStackTrace();
069 }
070 }
071 }