|
1 |
| package net.sf.crispy.properties; |
|
2 |
| |
|
3 |
| import java.io.FileInputStream; |
|
4 |
| import java.net.URL; |
|
5 |
| import java.util.Properties; |
|
6 |
| |
|
7 |
| import net.sf.crispy.PropertiesLoader; |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| public class ClassPathPropertiesLoader implements PropertiesLoader { |
|
16 |
| |
|
17 |
| private String fileName = null; |
|
18 |
| |
|
19 |
3
| public ClassPathPropertiesLoader(String pvFileName) {
|
|
20 |
3
| fileName = pvFileName;
|
|
21 |
| } |
|
22 |
| |
|
23 |
4
| public Properties load() {
|
|
24 |
4
| if (fileName == null) {
|
|
25 |
1
| throw new IllegalArgumentException("The file name for the ClassPathPropertiesLoader is null.");
|
|
26 |
| } |
|
27 |
2
| if (!(fileName.startsWith("/"))) { fileName = "/" + fileName; }
|
|
28 |
3
| Properties lvProperties = new Properties();
|
|
29 |
3
| try {
|
|
30 |
3
| URL lvUrl = ClassPathPropertiesLoader.class.getResource(fileName);
|
|
31 |
3
| if (lvUrl == null) {
|
|
32 |
1
| throw new IllegalArgumentException("The file name: " + fileName + " was not found in the class path.");
|
|
33 |
| } |
|
34 |
2
| String lvPath = lvUrl.getPath();
|
|
35 |
2
| lvProperties.load(new FileInputStream(lvPath));
|
|
36 |
| } catch (Exception e) { |
|
37 |
1
| throw new PropertiesLoadException("Error in load-method:", e);
|
|
38 |
| } |
|
39 |
2
| return lvProperties;
|
|
40 |
| } |
|
41 |
| |
|
42 |
| } |