1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.util;
47
48 import java.util.Hashtable;
49 import java.util.Properties;
50 import java.io.File;
51 import java.io.InputStream;
52 import java.io.FileInputStream;
53 import java.io.IOException;
54 import java.io.FileNotFoundException;
55
56
57
58
59 public final class PropertiesUtils {
60
61 private PropertiesUtils() {}
62
63
64
65
66
67
68
69
70 public static Properties fromFile(File path) throws IOException {
71 InputStream data = new FileInputStream(path);
72 Properties them = new Properties();
73 try {
74 them.load(data);
75 } catch (IOException e) {
76 throw new IOException("Corrupt properties file `" + path + "': " +
77 e.getMessage());
78 }
79
80 return them;
81 }
82
83 public static Properties fromResource(Class<?> clazz)
84 throws IOException {
85 String name = clazz.getCanonicalName() + ".properties";
86 return fromResource(clazz, name);
87 }
88
89
90
91
92
93
94
95
96
97
98 public static Properties fromResource(Class<?> clazz, String name)
99 throws IOException {
100 InputStream is = clazz.getResourceAsStream(name);
101
102 if (is == null)
103 throw new FileNotFoundException(name + ": is it in CLASSPATH?");
104
105 Properties them = new Properties();
106 try {
107 them.load(is);
108 } catch (IOException e) {
109 throw new IOException("Corrupt properties file `" + name + "': " +
110 e.getMessage());
111 }
112
113 return them;
114 }
115
116
117
118
119
120
121
122
123
124 public static String getOrDie(Properties properties, String propertyName)
125 throws NoSuchPropertyException {
126 String value = properties.getProperty(propertyName);
127 if (value == null)
128 throw new NoSuchPropertyException(properties, propertyName);
129 return value;
130 }
131
132
133
134
135
136
137
138
139
140 public static String getOrDefault(Properties properties,
141 String propertyName, String def) {
142 String value = properties.getProperty(propertyName);
143 if (value == null) return def;
144 return value;
145 }
146
147
148
149
150
151
152
153
154
155
156 public static int getOrDie_int(Properties properties, String propertyName)
157 throws NoSuchPropertyException, FormatPropertyException {
158 String string = getOrDie(properties, propertyName);
159 try {
160 return Integer.parseInt(string);
161 }
162 catch (NumberFormatException e) {
163 throw new FormatPropertyException(properties, propertyName, string,
164 "an integer", e);
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177 public static int getOrDefault_int(Properties properties,
178 String propertyName, int def)
179 throws FormatPropertyException {
180 String string = getOrDefault(properties, propertyName, ""+def);
181 try {
182 return Integer.parseInt(string);
183 }
184 catch (NumberFormatException e) {
185 throw new FormatPropertyException(properties, propertyName, string,
186 "an integer", e);
187 }
188 }
189
190 private static Hashtable<String, Object> instantiatedClassesCache = new Hashtable<String, Object>();
191
192
193
194
195
196
197
198
199
200 public static Object instanceOfNamedClass(String className, String interfaceClassName)
201 throws InstantiationPropertyException {
202 Object cached = instantiatedClassesCache.get(className);
203 if (cached != null)
204 return cached;
205 try {
206 Class<?> clazz = Class.forName(className);
207 if (interfaceClassName != null) {
208 Class<?> interfaceClass = Class.forName(interfaceClassName);
209 if (!interfaceClass.isAssignableFrom(clazz))
210 throw new ClassCastException(
211 clazz + " is not descended from " + interfaceClass);
212 }
213 Object it = clazz.newInstance();
214 instantiatedClassesCache.put(className, it);
215 return it;
216 } catch (Exception e) {
217 throw new InstantiationPropertyException(className, e);
218 }
219 }
220
221
222
223
224
225
226
227
228
229
230
231 public static Object instanceOfNamedClass(Properties properties,
232 String propertyName,
233 String interfaceClassName,
234 String defaultName)
235 throws InstantiationPropertyException {
236 String className = (String)properties.get(propertyName);
237 return instanceOfNamedClass(className == null ? defaultName : className, interfaceClassName);
238 }
239 }