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