1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.util.properties; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.InputStream; |
20 | |
import java.net.URL; |
21 | |
import java.util.Properties; |
22 | |
|
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.kuali.rice.krad.exception.PropertiesException; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 6 | public class FilePropertySource implements PropertySource { |
34 | 1 | private static Log log = LogFactory.getLog(FilePropertySource.class); |
35 | |
|
36 | |
|
37 | |
private String fileName; |
38 | |
private boolean allowOverrides; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public void setFileName(String fileName) { |
46 | 4 | this.fileName = fileName; |
47 | 4 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public String getFileName() { |
53 | 12 | return this.fileName; |
54 | |
} |
55 | |
|
56 | |
public boolean isAllowOverrides() { |
57 | 0 | return this.allowOverrides; |
58 | |
} |
59 | |
|
60 | |
public void setAllowOverrides(boolean allowOverrides) { |
61 | 0 | this.allowOverrides = allowOverrides; |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Properties loadProperties() { |
71 | 6 | if (StringUtils.isBlank(getFileName())) { |
72 | 3 | throw new IllegalStateException("invalid (blank) fileName"); |
73 | |
} |
74 | |
|
75 | 3 | Properties properties = new Properties(); |
76 | |
|
77 | 3 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
78 | 3 | URL url = loader.getResource(getFileName()); |
79 | 3 | if (url == null) { |
80 | 3 | throw new PropertiesException("unable to locate properties file '" + getFileName() + "'"); |
81 | |
} |
82 | |
|
83 | 0 | InputStream in = null; |
84 | |
|
85 | |
try { |
86 | 0 | in = url.openStream(); |
87 | 0 | properties.load(in); |
88 | |
} |
89 | 0 | catch (IOException e) { |
90 | 0 | throw new PropertiesException("error loading from properties file '" + getFileName() + "'", e); |
91 | |
} |
92 | |
finally { |
93 | 0 | if (in != null) { |
94 | |
try { |
95 | 0 | in.close(); |
96 | |
} |
97 | 0 | catch (IOException e) { |
98 | 0 | log.error("caught exception closing InputStream: " + e); |
99 | 0 | } |
100 | |
|
101 | |
} |
102 | |
} |
103 | |
|
104 | 0 | return properties; |
105 | |
} |
106 | |
|
107 | |
} |