1 package org.kuali.ole.docstore.util;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Properties;
8
9 import org.kuali.ole.utility.Constants;
10 import org.kuali.ole.utility.ResourceUtil;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14
15
16
17
18 public class PropertyUtil {
19 private static final Logger LOG = LoggerFactory.getLogger(PropertyUtil.class);
20 ResourceUtil resourceUtil = new ResourceUtil();
21 private static PropertyUtil propertyUtil = new PropertyUtil();
22 private static final String DEFAULT_VALUE_PREFIX = Constants.DEFAULT_VALUE_PREFIX;
23 private static final String DEFAULT_ENVIRONMENT = Constants.DEFAULT_ENVIRONMENT;
24 private static final String ENVIRONMENT_SYSTEM_PROPERTY = Constants.ENVIRONMENT_SYSTEM_PROPERTY;
25 private static final String DOC_STORE_PROPERTIES_FILE_NAME = "documentstore.properties";
26 private static final String DOC_STORE_PROPERTIES_DEFAULT_LOCATION = "classpath:" + DOC_STORE_PROPERTIES_FILE_NAME;
27 private static final String DOC_STORE_PROPERTIES_HOME_ENV = "docstore.properties.home";
28
29 private String environment = resourceUtil.getSystemProperty(ENVIRONMENT_SYSTEM_PROPERTY, DEFAULT_ENVIRONMENT);
30
31 public static PropertyUtil getPropertyUtil() {
32 return propertyUtil;
33 }
34
35 private Properties props;
36
37 private PropertyUtil() {
38 try {
39 LOG.info(ENVIRONMENT_SYSTEM_PROPERTY + "=" + environment);
40 Properties defaultProperties = resourceUtil.getProperties(DOC_STORE_PROPERTIES_DEFAULT_LOCATION);
41 Properties systemProperties = resourceUtil.getPropertiesFromSystemProperty(DocStoreEnvUtil.DOC_STORE_PROPERTIES_FILE_SYS_PROP);
42 props = new Properties();
43 props.putAll(defaultProperties);
44 props.putAll(systemProperties);
45 List<String> propertyNames = new ArrayList<String>(props.stringPropertyNames());
46 Collections.sort(propertyNames);
47 for (String key : propertyNames) {
48 boolean display = key.startsWith(DEFAULT_VALUE_PREFIX) || key.startsWith(environment);
49 if (display) {
50 LOG.info(key + "=" + props.getProperty(key));
51 }
52 }
53 } catch (IOException e) {
54 throw new IllegalStateException("Exception loading properties", e);
55 }
56 }
57
58 public String getProperty(String key) {
59 return resourceUtil.getProperty(props, DEFAULT_VALUE_PREFIX, environment, key);
60 }
61
62 }