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; |
47 | |
|
48 | |
import java.io.FileNotFoundException; |
49 | |
import java.io.IOException; |
50 | |
import java.util.List; |
51 | |
import java.util.Properties; |
52 | |
import java.util.Vector; |
53 | |
|
54 | |
import org.melati.login.AccessHandler; |
55 | |
import org.melati.poem.PoemLocale; |
56 | |
import org.melati.poem.util.EnumUtils; |
57 | |
import org.melati.servlet.FormDataAdaptorFactory; |
58 | |
import org.melati.template.ClassNameTempletLoader; |
59 | |
import org.melati.template.ServletTemplateEngine; |
60 | |
import org.melati.template.SimpleDateAdaptor; |
61 | |
import org.melati.template.TemplateEngine; |
62 | |
import org.melati.template.TempletLoader; |
63 | |
import org.melati.template.YMDDateAdaptor; |
64 | |
import org.melati.template.YMDHMSTimestampAdaptor; |
65 | |
import org.melati.util.ConfigException; |
66 | |
import org.melati.util.HttpHeader; |
67 | |
import org.melati.util.PropertiesUtils; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public class MelatiConfig { |
74 | |
|
75 | 330 | private Properties configuration = null; |
76 | 330 | private String propertiesPrefix = "org.melati.MelatiConfig"; |
77 | |
|
78 | 330 | private AccessHandler accessHandler = null; |
79 | 330 | private FormDataAdaptorFactory fdaFactory = null; |
80 | 330 | private TempletLoader templetLoader = null; |
81 | 330 | private TemplateEngine templateEngine = null; |
82 | 1 | private static PoemLocale poemLocale = null; |
83 | 330 | private Vector<String> preferredCharsets = null; |
84 | 330 | private String javascriptLibraryURL = null; |
85 | 330 | private String staticURL = null; |
86 | 330 | private String templatePath = null; |
87 | 1 | private static String loginPageServletClassName = "org.melati.login.Login"; |
88 | 1 | private static String logoutPageServletClassName = "org.melati.login.Logout"; |
89 | |
|
90 | 1 | private static String realPath = null; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 296 | public MelatiConfig() { |
97 | 296 | propertiesPrefix = getClass().getCanonicalName(); |
98 | |
try { |
99 | 296 | configuration = PropertiesUtils.fromResource(getClass(), propertiesPrefix + ".properties"); |
100 | |
} |
101 | 0 | catch (FileNotFoundException e) { |
102 | 0 | configuration = new Properties(); |
103 | |
|
104 | |
|
105 | |
} |
106 | 0 | catch (IOException e) { |
107 | 0 | throw new ConfigException("The file " + propertiesPrefix + ".properties" + |
108 | |
" could not be read.", e); |
109 | 296 | } |
110 | 296 | init(propertiesPrefix); |
111 | 296 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | 3 | public MelatiConfig(String propertiesFilename) { |
121 | 3 | configuration = getProperties(propertiesFilename); |
122 | 2 | init(propertiesFilename); |
123 | 1 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | 31 | public MelatiConfig(Properties properties) { |
130 | 31 | configuration = properties; |
131 | 31 | init(propertiesPrefix); |
132 | 31 | } |
133 | |
|
134 | |
|
135 | |
public static Properties getProperties() { |
136 | 30 | return getProperties(MelatiConfig.class.getCanonicalName()); |
137 | |
} |
138 | |
public static Properties getProperties(String propertiesName) { |
139 | |
try { |
140 | 33 | return PropertiesUtils.fromResource(MelatiConfig.class, propertiesName + ".properties"); |
141 | |
} |
142 | 1 | catch (FileNotFoundException e) { |
143 | 1 | throw new ConfigException("The file " + propertiesName + "properties" + |
144 | |
" could not be found." + |
145 | |
" Is it in your CLASSPATH?", e); |
146 | |
} |
147 | 0 | catch (IOException e) { |
148 | 0 | throw new ConfigException("The file " + propertiesName + ".properties" + |
149 | |
" could not be read.", e); |
150 | |
} |
151 | |
|
152 | |
} |
153 | |
@SuppressWarnings("unchecked") |
154 | |
void init(String propertiesNameIn) { |
155 | 329 | this.propertiesPrefix = propertiesNameIn; |
156 | 329 | String pref = propertiesPrefix + "."; |
157 | |
|
158 | 329 | String accessHandlerProp = pref + "accessHandler"; |
159 | 329 | String fdaFactoryProp = pref + "formDataAdaptorFactory"; |
160 | 329 | String templetLoaderProp = pref + "templetLoader"; |
161 | 329 | String templateEngineProp = pref + "templateEngine"; |
162 | 329 | String templatePathProp = pref + "templatePath"; |
163 | 329 | String javascriptLibraryURLProp = pref + "javascriptLibraryURL"; |
164 | 329 | String staticURLProp = pref + "staticURL"; |
165 | 329 | String melatiLocaleProp = pref + "locale"; |
166 | 329 | String preferredCharsetsProp = pref + "preferredCharsets"; |
167 | 329 | String loginPageServletClassNameProp = pref + "loginPageServletClassName"; |
168 | 329 | String logoutPageServletClassNameProp = pref + "logoutPageServletClassName"; |
169 | |
|
170 | |
try { |
171 | 658 | setAccessHandler((AccessHandler)PropertiesUtils. |
172 | 329 | instanceOfNamedClass( |
173 | |
configuration, |
174 | |
accessHandlerProp, |
175 | |
"org.melati.login.AccessHandler", |
176 | |
"org.melati.login.OpenAccessHandler")); |
177 | |
|
178 | 658 | setFdaFactory((FormDataAdaptorFactory)PropertiesUtils. |
179 | 329 | instanceOfNamedClass( |
180 | |
configuration, |
181 | |
fdaFactoryProp, |
182 | |
"org.melati.servlet.FormDataAdaptorFactory", |
183 | |
"org.melati.servlet.MemoryFormDataAdaptorFactory")); |
184 | |
|
185 | 329 | String templetLoaderClassName = (String)configuration.get(templetLoaderProp); |
186 | 329 | if(templetLoaderClassName == null || |
187 | 0 | templetLoaderClassName.equals("org.melati.template.ClassNameTempletLoader")) { |
188 | 329 | setTempletLoader(ClassNameTempletLoader.getInstance()); |
189 | |
} else |
190 | 0 | setTempletLoader((TempletLoader)PropertiesUtils. |
191 | 0 | instanceOfNamedClass( |
192 | |
configuration, |
193 | |
templetLoaderProp, |
194 | |
"org.melati.template.TempletLoader", |
195 | |
"org.melati.template.ClassNameTempletLoader")); |
196 | |
|
197 | 658 | setTemplateEngine((TemplateEngine)PropertiesUtils. |
198 | 329 | instanceOfNamedClass( |
199 | |
configuration, |
200 | |
templateEngineProp, |
201 | |
"org.melati.template.TemplateEngine", |
202 | |
"org.melati.template.NoTemplateEngine")); |
203 | |
|
204 | 329 | String languageTag = PropertiesUtils.getOrDefault(configuration, |
205 | |
melatiLocaleProp, |
206 | |
"en-gb"); |
207 | |
|
208 | 329 | setPoemiLocale(PoemLocale.fromLanguageTag(languageTag)); |
209 | 329 | if (poemLocale == null) |
210 | 1 | throw new ConfigException(languageTag + |
211 | |
" is not a valid language tag for " + |
212 | |
melatiLocaleProp); |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | 656 | setPreferredCharsets( |
219 | 328 | EnumUtils.vectorOf( |
220 | 328 | new HttpHeader(PropertiesUtils.getOrDefault( |
221 | |
configuration, |
222 | |
preferredCharsetsProp, |
223 | 328 | "ISO-8859-1, UTF-8, UTF-16")).wordIterator())); |
224 | |
|
225 | 328 | setJavascriptLibraryURL(PropertiesUtils.getOrDefault( |
226 | |
configuration, |
227 | |
javascriptLibraryURLProp, |
228 | |
"/melati-static/admin/")); |
229 | |
|
230 | 328 | setStaticURL(PropertiesUtils.getOrDefault( |
231 | |
configuration, |
232 | |
staticURLProp, |
233 | |
"/melati-static/" |
234 | |
)); |
235 | |
|
236 | 328 | setTemplatePath(PropertiesUtils.getOrDefault(configuration, |
237 | |
templatePathProp, ".")); |
238 | |
|
239 | 328 | setLoginPageServletClassName(PropertiesUtils.getOrDefault(configuration, |
240 | |
loginPageServletClassNameProp, loginPageServletClassName)); |
241 | |
|
242 | 328 | setLogoutPageServletClassName(PropertiesUtils.getOrDefault(configuration, |
243 | |
logoutPageServletClassNameProp, logoutPageServletClassName)); |
244 | |
} |
245 | 1 | catch (Exception e) { |
246 | 1 | throw new ConfigException("Melati could not be configured", e); |
247 | 328 | } |
248 | |
|
249 | 328 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
public ServletTemplateEngine getServletTemplateEngine() { |
255 | 18 | return (ServletTemplateEngine)templateEngine; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public TemplateEngine getTemplateEngine() { |
262 | 290 | return templateEngine; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public void setTemplateEngine(TemplateEngine templateEngine) { |
272 | 483 | this.templateEngine = templateEngine; |
273 | 483 | } |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
public AccessHandler getAccessHandler() { |
279 | 1156 | return accessHandler; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public void setAccessHandler(AccessHandler accessHandler) { |
289 | 345 | this.accessHandler = accessHandler; |
290 | 345 | } |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public TempletLoader getTempletLoader() { |
296 | 552 | return templetLoader; |
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public void setTempletLoader(TempletLoader templetLoader) { |
306 | 329 | this.templetLoader = templetLoader; |
307 | 329 | } |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public FormDataAdaptorFactory getFormDataAdaptorFactory() { |
313 | 55 | return fdaFactory; |
314 | |
} |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public void setFormDataAdaptorFactory(FormDataAdaptorFactory fdaf) { |
323 | 13 | fdaFactory = fdaf; |
324 | 13 | } |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public String getJavascriptLibraryURL() { |
330 | 112 | return javascriptLibraryURL; |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public void setJavascriptLibraryURL(String url) { |
340 | 328 | this.javascriptLibraryURL = url; |
341 | 328 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
public String getStaticURL() { |
349 | 577 | return staticURL; |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
public void setStaticURL(String url) { |
359 | 328 | this.staticURL = url; |
360 | 328 | } |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
public String getTemplatePath() { |
366 | 1 | return templatePath; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
public void setTemplatePath(String templatePath) { |
374 | 328 | this.templatePath = templatePath; |
375 | 328 | } |
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
public static String getLogoutPageServletClassName() { |
381 | 19 | return logoutPageServletClassName; |
382 | |
} |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
public static void setLogoutPageServletClassName( |
389 | |
String logoutPageServletClassName) { |
390 | 328 | MelatiConfig.logoutPageServletClassName = logoutPageServletClassName; |
391 | 328 | } |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
public static String getLoginPageServletClassName() { |
397 | 15 | return loginPageServletClassName; |
398 | |
} |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
public static void setLoginPageServletClassName( |
405 | |
String loginPageServletClassName) { |
406 | 328 | MelatiConfig.loginPageServletClassName = loginPageServletClassName; |
407 | 328 | } |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
public static PoemLocale getPoemLocale() { |
413 | 69 | if (poemLocale == null) |
414 | 0 | poemLocale = PoemLocale.HERE; |
415 | 69 | return poemLocale; |
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
public void setPoemiLocale(PoemLocale poemLocale) { |
423 | 329 | MelatiConfig.poemLocale = poemLocale; |
424 | 329 | } |
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
public List<String> getPreferredCharsets() { |
433 | 568 | return preferredCharsets; |
434 | |
} |
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
public void setPreferredCharsets(Vector<String> preferredCharsets) { |
441 | 328 | this.preferredCharsets = preferredCharsets; |
442 | 328 | } |
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
public FormDataAdaptorFactory getFdaFactory() { |
448 | 1 | return fdaFactory; |
449 | |
} |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
public void setFdaFactory(FormDataAdaptorFactory fdaFactory) { |
456 | 329 | this.fdaFactory = fdaFactory; |
457 | 329 | } |
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
public static YMDDateAdaptor getYMDDateAdaptor() { |
473 | 13 | return YMDDateAdaptor.it; |
474 | |
} |
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
public static YMDHMSTimestampAdaptor getYMDHMSTimestampAdaptor() { |
486 | 13 | return YMDHMSTimestampAdaptor.getIt(); |
487 | |
} |
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
public static SimpleDateAdaptor getSimpleDateAdaptor() { |
495 | 3 | return SimpleDateAdaptor.it; |
496 | |
} |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
public void setRealPath(String realPathP) { |
502 | 38 | realPath = realPathP; |
503 | 38 | } |
504 | |
|
505 | |
|
506 | |
|
507 | |
public String getRealPath() { |
508 | 5 | return realPath; |
509 | |
} |
510 | |
|
511 | |
} |