1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application;
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.util.Properties;
28 import java.util.logging.ConsoleHandler;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32
33
34
35
36
37
38
39
40 public final class ConfigurationLoader {
41
42 private ConfigurationLoader() {
43 super();
44 }
45
46
47
48
49
50
51
52
53
54 public static ClassLoader findClassLoader(Class c) {
55 Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
56 ClassLoader cl=null;
57
58 if (c == null) {
59 c= ConfigurationLoader.class;
60 }
61
62 try {
63 cl= c.getClassLoader();
64 } catch (Exception e) {
65 l.log(Level.FINEST,"Failed to load class loader for '"+c.getName()+"'",e);
66 }
67
68 if (cl!=null) {
69 return cl;
70 }
71
72 try {
73 cl= ClassLoader.getSystemClassLoader();
74 } catch (Exception e) {
75 l.log(Level.FINEST,"Failed to load system class loader for '"+c.getName()+"'",e);
76 }
77
78 return cl;
79 }
80
81
82
83
84
85
86
87 public static Properties loadConfiguration(Class c, String name) {
88 return loadConfigurationFile(c,name+".properties");
89 }
90
91
92
93
94
95
96
97
98 public static Properties loadConfiguration(String name) {
99 return loadConfigurationFile(null,name+".properties");
100 }
101
102
103
104
105
106
107
108
109
110 public static Properties loadConfigurationFile(Class c, String propFileName) {
111 Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
112
113 Properties p= new Properties();
114
115
116
117
118
119 ClassLoader cl = findClassLoader(c);
120 if (cl!=null) {
121
122 URL url= cl.getResource(".");
123 InputStream is=null;
124 File f= null;
125 try {
126 f= new File(url.toURI());
127 f= new File(f,propFileName);
128 if (f.exists()) {
129 is= new BufferedInputStream(new FileInputStream(f));
130 p.load(is);
131 is.close();
132 l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from file '"+f.getAbsolutePath()+"'.");
133 } else {
134 l.log(Level.FINEST,"Configuration '"+propFileName+"' does not exist as file '"+f.getAbsolutePath()+"'.");
135 }
136 } catch (Exception e) {
137 l.log(Level.FINEST,"Failed to load '"+propFileName+"' from file '"+f+"'.",e);
138 } finally {
139 try {
140 if (is!=null) is.close();
141 } catch (Exception e) {
142
143 }
144 }
145
146 is= cl.getResourceAsStream(propFileName);
147 l.log(Level.FINE,"Configuration '"+propFileName+"' not found on classpath.");
148 if (is!=null) {
149 try {
150 p.load(is);
151 l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from classpath.");
152 is.close();
153 } catch (Exception e) {
154 l.log(Level.FINEST,"Failed to load '"+propFileName+"' from classpath.",e);
155 } finally {
156 try {
157 if (is!=null) is.close();
158 } catch (Exception e) {
159
160 }
161 }
162 }
163 }
164
165
166
167
168 File f= new File(System.getProperty("user.dir"));
169 InputStream is=null;
170 try {
171 if (f.exists()) {
172 f= new File(f,propFileName);
173 if (f.exists()) {
174 is= new BufferedInputStream(new FileInputStream(f));
175 p.load(is);
176 is.close();
177 l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from file '"+f.getAbsolutePath()+"'.");
178 }
179 } else {
180 l.log(Level.FINEST,"Configuration '"+propFileName+"' does not exist as file '"+f.getAbsolutePath()+"'.");
181 }
182 } catch (Exception e) {
183 l.log(Level.FINEST,"Failed to load '"+propFileName+"' from file '"+f.getAbsolutePath()+"'.",e);
184 } finally {
185 try {
186 if (is!=null) is.close();
187 } catch (Exception e) {
188
189 }
190 }
191
192
193 return p;
194 }
195
196 public static void main(String[] args) {
197 Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
198 l.setLevel(Level.ALL);
199 ConsoleHandler c= new ConsoleHandler();
200 c.setLevel(Level.ALL);
201 l.addHandler(c);
202
203 Properties p= ConfigurationLoader.loadConfiguration("project");
204 c.flush();
205 p.list(System.out);
206
207 }
208
209 }