|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.utils; |
|
8 |
| |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.IOException; |
|
11 |
| import java.net.URL; |
|
12 |
| import java.io.InputStream; |
|
13 |
| import java.util.Properties; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| public class ConfigurationUtils { |
|
23 |
| |
|
24 |
0
| private ConfigurationUtils() {
|
|
25 |
| } |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
62
| public static File getFileFromInputStream(String fileName) {
|
|
34 |
62
| ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
35 |
62
| URL url = cl.getResource(fileName);
|
|
36 |
62
| if (url != null) {
|
|
37 |
62
| File file = new File(url.getFile());
|
|
38 |
62
| return file;
|
|
39 |
| } |
|
40 |
| else { |
|
41 |
0
| return null;
|
|
42 |
| } |
|
43 |
| } |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
1
| public static File getFile( String filename ) throws IOException {
|
|
52 |
1
| ResourceLocator locator = new ResourceLocator( filename );
|
|
53 |
1
| return locator.getFile();
|
|
54 |
| } |
|
55 |
| |
|
56 |
2
| public static String getConfigProperty(String name) {
|
|
57 |
2
| return getConfigProperty(name,null);
|
|
58 |
| } |
|
59 |
| |
|
60 |
39
| public static String getConfigProperty(String name,String defaultValue) {
|
|
61 |
39
| String val = System.getProperty(name);
|
|
62 |
39
| if ( val == null ) {
|
|
63 |
39
| try {
|
|
64 |
39
| ResourceLocator locator = new ResourceLocator("jconfig.properties");
|
|
65 |
39
| InputStream is = locator.getInputStream();
|
|
66 |
| |
|
67 |
39
| if ( is != null ) {
|
|
68 |
39
| Properties jcfProperties = new Properties();
|
|
69 |
39
| jcfProperties.load( is );
|
|
70 |
39
| val = jcfProperties.getProperty(name);
|
|
71 |
| } |
|
72 |
| } catch (IOException e) { |
|
73 |
| } |
|
74 |
| } |
|
75 |
39
| if ( val == null ) {
|
|
76 |
39
| val = defaultValue;
|
|
77 |
| } |
|
78 |
39
| return val;
|
|
79 |
| } |
|
80 |
| } |