1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.samplu.common;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28
29
30
31
32
33 public class PropertiesUtils {
34
35
36 public static Properties loadPropertiesWithSystemOverrides(InputStream inputStream) throws IOException {
37 Properties props = PropertiesUtils.loadProperties(inputStream);
38 PropertiesUtils.systemPropertiesOverride(props);
39 return props;
40 }
41
42 public static Properties loadPropertiesWithSystemOverridesAndNumberedPropertiesToList(InputStream inputStream) throws IOException {
43 Properties props = PropertiesUtils.loadProperties(inputStream);
44 PropertiesUtils.systemPropertiesOverride(props);
45 PropertiesUtils.transformNumberedPropertiesToList(props);
46 return props;
47 }
48
49 public static Properties loadProperties(InputStream inputStream) throws IOException {
50 Properties props = new Properties();
51
52 if(inputStream != null) {
53 props.load(inputStream);
54 }
55
56 return props;
57 }
58
59 public static String removeNumber(final String numberedKey) {
60 String unnumberedKey = numberedKey;
61 int firstNumberIndex = unnumberedKey.length() - 1;
62 while (Character.isDigit(unnumberedKey.charAt(firstNumberIndex))) {
63 firstNumberIndex--;
64 }
65 unnumberedKey = unnumberedKey.substring(0, firstNumberIndex + 1);
66
67 return unnumberedKey;
68 }
69
70 public static void systemPropertiesOverride(Properties props) {
71 PropertiesUtils.systemPropertiesOverride(props, null);
72 }
73
74
75
76
77
78
79 public static void systemPropertiesOverride(Properties props, String key) {
80 Enumeration<?> names = props.propertyNames();
81 Object nameObject;
82 String name;
83 while (names.hasMoreElements()) {
84
85 nameObject = names.nextElement();
86 if (nameObject instanceof String) {
87
88 name = (String)nameObject;
89 if (key == null || key.isEmpty()) {
90 props.setProperty(name, System.getProperty(name, props.getProperty(name)));
91 } else {
92 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
93 }
94 }
95 }
96 }
97
98 public static void transformNumberedPropertiesToList(Properties props) {
99 String key = null;
100 String unnumberedKey = null;
101 List<String> keyList = null;
102 List<String> removeKeys = new LinkedList<String>();
103
104
105 Iterator keys = props.keySet().iterator();
106 Map<String, List<String>> keysLists = new HashMap<String, List<String>>();
107 while (keys.hasNext()) {
108 key = (String)keys.next();
109 if (Character.isDigit(key.charAt(key.length()-1))) {
110 unnumberedKey = removeNumber(key);
111 if (keysLists.get(unnumberedKey) == null) {
112 keyList = new ArrayList<String>();
113 keyList.add(props.getProperty(key));
114 keysLists.put(unnumberedKey, keyList);
115 removeKeys.add(key);
116 } else {
117 keyList = keysLists.get(unnumberedKey);
118 keyList.add(props.getProperty(key));
119 keysLists.put(unnumberedKey, keyList);
120 removeKeys.add(key);
121 }
122 }
123 }
124
125
126 Iterator removeKey = removeKeys.iterator();
127 while (removeKey.hasNext()) {
128 key = (String)removeKey.next();
129 props.remove(key);
130 }
131
132
133 Iterator newKeys = keysLists.keySet().iterator();
134 String newKey = null;
135 while (newKeys.hasNext()) {
136 newKey = (String)newKeys.next();
137 props.put(newKey + "s", keysLists.get(newKey));
138 }
139 }
140 }