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